-
Notifications
You must be signed in to change notification settings - Fork 72
/
update.js
64 lines (57 loc) · 1.94 KB
/
update.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/**
* Update Behaviors
*/
var helpers = require('../lib/update-helpers');
var utils = require('../lib/utils');
/**
* Increment column
* Example:
* { $inc: { clicks: 1 } }
* @param {Object} Hash whose keys are the columns to inc and values are how much it will inc
*/
helpers.add('$inc', function(value, values, collection) {
return Object.keys(value).map(function (key) {
return utils.quoteObject(key) + ' = ' + utils.quoteObject(key, collection) + ' + $' + values.push(value[key]);
}).join(', ');
});
/**
* Decrement column
* Example:
* { $dec: { clicks: 1 } }
* @param {Object} Hash whose keys are the columns to dec and values are how much it will inc
*/
helpers.add('$dec', function(value, values, collection){
var output = "";
for (var key in value){
output += utils.quoteObject(key) + ' = ' + utils.quoteObject(key, collection) + ' - $' + values.push(value[key]);
}
return output;
});
/**
* Creates a custom helper on the fly, much like in the conditional helper system.
* Example:
* $custom: {
* images: ['$1::jsonb || images', JSON.stringify([image])],
* number_of_images: 'jsonb_array_length(images) + 1',
* }
* @param {Object} Hash whose keys are the columns to be assigned to the custom helper
*/
helpers.add('$custom', function(value, values) {
return Object.keys(value)
.map(function(key) {
var localToGlobalValuesIndices = {};
var newValue = !Array.isArray(value[key])
? value[key]
: value[key][0].replace(/\$\d+/g, function(match) {
var localIndex = match.slice(1);
var globalIndex =
localIndex in localToGlobalValuesIndices
? localToGlobalValuesIndices[localIndex]
: values.push(value[key][localIndex]);
localToGlobalValuesIndices[localIndex] = globalIndex;
return '$' + globalIndex;
});
return utils.quoteObject(key) + ' = ' + newValue;
})
.join(', ');
});