Update helpers are used in updates and values. They're only used in the values helper if the query type is update.
They're a lot simpler than conditional helpers. No cascading. The amount of helpers provided for updates out of the box is currently lacking. There's a ton of cool stuff that could be done, so submit a pull request!
Check out the Playground.
Format: col = col + val
Increment column by value.
Example:
{
type: 'update'
, table: 'users'
, where: {
id: 7
}
, updates: {
$inc: { hp: 5 }
}
}
update "users"
set "hp" = "users"."hp" + $1
where "users"."id" = $2
Format: col = col - val
Decrement column by value.
Example:
{
type: 'update'
, table: 'users'
, where: {
id: 7
}
, updates: {
$dec { hp: 5 }
}
}
update "users"
set "hp" = "users"."hp" - $1
where "users"."id" = $2
Update helpers use the standard MoSQL helper interface, so it's just like adding other helpers.
Alias for mosql.updateHelpers.add
.
Registers a new update helper.
Callbacks arguments are: callback( value, values, table, query )
Arguments:
- Value - The value to be used for update.
- Values - The values array. All values not escaped by surrounding '$' signs are pushed to the values array for parameterized queries.
- Table - The table associated to the column
- Query - This is the whole MoSQL query object passed in by the user.
Example:
var mosql = require('mongo-sql');
/**
* Increment column
* Example:
* { $inc: { clicks: 1 } }
* @param {Object} Hash whose keys are the columns to inc and values are how much it will inc
*/
mosql.registerUpdateHelper('$inc', function(value, values, collection){
return Object.keys( value ).map( function( key ){
return [
// Quote the column without the table
mosql.utils.quoteObject( key )
, '='
// Quote column with the table
, mosql.utils.quoteObject( key, table )
// Push the value into the values array
, '+ $' + values.push( value[ key ] )
].join(' ');
}).join(' ');
});
Returns a boolean denoting whether or not a update helper exists.
Returns the update helper interface: { fn, options }
.