lib/library.js
function(table, params, unique) {
        unique = unique || [];

        let columns = _.keys(params);
        let whereStatements = _.map(unique, function(c) { return c + '=:' + c});
        let updatedColumns = _.map(_.filter(columns, function(c) { return unique.indexOf(c) }), function(c) { return c + '=:' + c });
        let insertHolders = _.map(columns, function(k) { return ':' + k; });

        run('insert or ignore into ' + table + ' (' + columns.join(', ') + ') values (' + insertHolders.join(', ') + ')', params);
        run('update or fail ' + table + ' set ' + updatedColumns.join(', ') + ' where ' + whereStatements.join(' and '), params);

        save();

        // TRICKY: in this module we are strictly dealing with inserts or *full* updates.
        // Therefore we can rely on the existence of the unique columns to retrieve the id
        let result = query('select id from ' + table + ' where ' + whereStatements.join(' and ') + ' order by id asc limit 1', params);
        return firstId(result);
    }
function(table, params, unique) {
        unique = unique || [];

        let columns = _.keys(params);
        let whereStatements = _.map(unique, function(c) { return c + '=:' + c});
        let updatedColumns = _.map(_.filter(columns, function(c) { return unique.indexOf(c) }), function(c) { return c + '=:' + c });
        let insertHolders = _.map(columns, function(k) { return ':' + k; });

        run('insert or ignore into ' + table + ' (' + columns.join(', ') + ') values (' + insertHolders.join(', ') + ')', params);
        run('update or fail ' + table + ' set ' + updatedColumns.join(', ') + ' where ' + whereStatements.join(' and '), params);

        save();

        // TRICKY: in this module we are strictly dealing with inserts or *full* updates.
        // Therefore we can rely on the existence of the unique columns to retrieve the id
        let result = query('select id from ' + table + ' where ' + whereStatements.join(' and ') + ' order by id asc limit 1', params);
        return firstId(result);
    }
function ( table , params , unique ) { unique = unique || [ ] ; let columns = _ . keys ( params ) ; let whereStatements = _ . map ( unique , function ( c ) { return c + '=:' + c } ) ; let updatedColumns = _ . map ( _ . filter ( columns , function ( c ) { return unique . indexOf ( c ) } ) , function ( c ) { return c + '=:' + c } ) ; let insertHolders = _ . map ( columns , function ( k ) { return ':' + k ; } ) ; run ( 'insert or ignore into ' + table + ' (' + columns . join ( ', ' ) + ') values (' + insertHolders . join ( ', ' ) + ')' , params ) ; run ( 'update or fail ' + table + ' set ' + updatedColumns . join ( ', ' ) + ' where ' + whereStatements . join ( ' and ' ) , params ) ; save ( ) ; // TRICKY: in this module we are strictly dealing with inserts or *full* updates. // Therefore we can rely on the existence of the unique columns to retrieve the id let result = query ( 'select id from ' + table + ' where ' + whereStatements . join ( ' and ' ) + ' order by id asc limit 1' , params ) ; return firstId ( result ) ; }
A utility to perform insert+update operations.
Insert failures are ignored.
Update failures are thrown.

@param table {string}
@param params {{}} keys must be valid column names
@param unique {[]} a list of unique columns on this table. This should be a subset of params
@returns {int} the id of the inserted/updated row or -1
A utility to perform insert + update operations . Insert failures are ignored . Update failures are thrown .