|
1 |
| -var util = require('./util.js') |
| 1 | +const _ = require('lodash') |
| 2 | +const util = require('./util.js') |
2 | 3 |
|
3 |
| -const specialKeys = ['$currentDate', '$inc', '$min', '$max', '$mul', '$rename', '$set', '$setOnInsert', '$unset', '$push', '$pull'] |
4 |
| - |
5 |
| -function convertOp(input, op, data, fieldName) { |
6 |
| - const pathText = Object.keys(data)[0]; |
7 |
| - const value = data[pathText]; |
8 |
| - delete data[pathText]; |
| 4 | +function convertOp(input, op, data, fieldName, upsert) { |
| 5 | + const pathText = Object.keys(data)[0] |
| 6 | + const value = data[pathText] |
| 7 | + delete data[pathText] |
9 | 8 | if (Object.keys(data).length > 0) {
|
10 |
| - input = convertOp(input, op, data, fieldName) |
| 9 | + input = convertOp(input, op, data, fieldName, upsert) |
11 | 10 | }
|
12 |
| - const path = pathText.split('.'); |
13 |
| - const pgPath = util.toPostgresPath(path); |
| 11 | + const path = pathText.split('.') |
| 12 | + const pgPath = util.toPostgresPath(path) |
14 | 13 | const pgQueryPath = util.pathToText([fieldName].concat(path), false)
|
15 | 14 | const pgQueryPathStr = util.pathToText([fieldName].concat(path), true)
|
| 15 | + const prevNumericVal = upsert ? '0' : util.toNumeric(pgQueryPathStr) |
16 | 16 | switch (op) {
|
17 | 17 | case '$set':
|
18 |
| - if (path.pop() === '_id') { |
| 18 | + // Create the necessary top level keys since jsonb_set will not create them automatically. |
| 19 | + if (path.length > 1) { |
| 20 | + for (let i = 0; i < path.length - 1; i++) { |
| 21 | + const parentPath = util.toPostgresPath([path[i]]) |
| 22 | + if (!input.includes(parentPath)) { |
| 23 | + const parentValue = upsert ? '\'{}\'::jsonb' : `COALESCE(${util.pathToText([fieldName].concat(path.slice(0, i + 1)))}, '{}'::jsonb)` |
| 24 | + input = 'jsonb_set(' + input + ',' + parentPath + ',' + parentValue + ')' |
| 25 | + } |
| 26 | + } |
| 27 | + } |
| 28 | + if (_.last(path) === '_id' && !upsert) { |
19 | 29 | throw new Error('Mod on _id not allowed')
|
20 | 30 | }
|
21 | 31 | return 'jsonb_set(' + input + ',' + pgPath + ',' + util.quote2(value) + ')'
|
22 |
| - break; |
23 | 32 | case '$unset':
|
24 |
| - return input + ' #- ' + pgPath; |
| 33 | + return input + ' #- ' + pgPath |
25 | 34 | case '$inc':
|
26 |
| - return 'jsonb_set(' + input + ',' + pgPath + ',to_jsonb(' + util.toNumeric(pgQueryPathStr) + '+' + value + '))' |
| 35 | + return 'jsonb_set(' + input + ',' + pgPath + ',to_jsonb(' + prevNumericVal + '+' + value + '))' |
27 | 36 | case '$mul':
|
28 |
| - return 'jsonb_set(' + input + ',' + pgPath + ',to_jsonb(' + util.toNumeric(pgQueryPathStr) + '*' + value + '))' |
| 37 | + return 'jsonb_set(' + input + ',' + pgPath + ',to_jsonb(' + prevNumericVal + '*' + value + '))' |
29 | 38 | case '$min':
|
30 |
| - return 'jsonb_set(' + input + ',' + pgPath + ',to_jsonb(LEAST(' + util.toNumeric(pgQueryPathStr) + ',' + value + ')))' |
| 39 | + return 'jsonb_set(' + input + ',' + pgPath + ',to_jsonb(LEAST(' + prevNumericVal + ',' + value + ')))' |
31 | 40 | case '$max':
|
32 |
| - return 'jsonb_set(' + input + ',' + pgPath + ',to_jsonb(GREATEST(' + util.toNumeric(pgQueryPathStr) + ',' + value + ')))' |
| 41 | + return 'jsonb_set(' + input + ',' + pgPath + ',to_jsonb(GREATEST(' + prevNumericVal + ',' + value + ')))' |
33 | 42 | case '$rename':
|
34 | 43 | const pgNewPath = util.toPostgresPath(value.split('.'))
|
35 |
| - return 'jsonb_set(' + input + ',' + pgNewPath + ',' + pgQueryPath + ') #- ' + pgPath; |
| 44 | + return 'jsonb_set(' + input + ',' + pgNewPath + ',' + pgQueryPath + ') #- ' + pgPath |
36 | 45 | case '$pull':
|
37 | 46 | return 'array_remove(ARRAY(SELECT value FROM jsonb_array_elements(' + pgQueryPath + ')),' + util.quote2(value) + ')'
|
38 | 47 | case '$push':
|
39 |
| - const v = util.quote2(value); |
| 48 | + const v2 = util.quote2(value) |
| 49 | + if (upsert) { |
| 50 | + const newArray = 'jsonb_build_array(' + v2 + ')' |
| 51 | + return 'jsonb_set(' + input + ',' + pgPath + ',' + newArray + ')' |
| 52 | + } |
| 53 | + const updatedArray2 = 'to_jsonb(array_append(ARRAY(SELECT value FROM jsonb_array_elements(' + pgQueryPath + ')),' + v2 + '))' |
| 54 | + return 'jsonb_set(' + input + ',' + pgPath + ',' + updatedArray2 + ')' |
| 55 | + case '$addToSet': |
| 56 | + const v = util.quote2(value) |
| 57 | + if (upsert) { |
| 58 | + const newArray = 'jsonb_build_array(' + v + ')' |
| 59 | + return 'jsonb_set(' + input + ',' + pgPath + ',' + newArray + ')' |
| 60 | + } |
40 | 61 | const updatedArray = 'to_jsonb(array_append(ARRAY(SELECT value FROM jsonb_array_elements(' + pgQueryPath + ') WHERE value != ' + v + '),' + v + '))'
|
41 | 62 | return 'jsonb_set(' + input + ',' + pgPath + ',' + updatedArray + ')'
|
42 | 63 | }
|
43 | 64 | }
|
44 | 65 |
|
45 |
| -var convert = function (fieldName, update) { |
46 |
| - var specialCount = Object.keys(update).filter(function(n) { |
47 |
| - return specialKeys.includes(n) |
48 |
| - }).length; |
| 66 | +var convert = function (fieldName, update, upsert) { |
| 67 | + var specialCount = util.countUpdateSpecialKeys(update) |
49 | 68 | if (specialCount === 0) {
|
50 | 69 | return '\'' + JSON.stringify(update) + '\'::jsonb'
|
51 | 70 | }
|
52 |
| - var output = fieldName |
53 |
| - Object.keys(update).forEach(function(key) { |
54 |
| - if (!specialKeys.includes(key)) { |
| 71 | + var output = upsert ? '\'{}\'::jsonb' : fieldName |
| 72 | + let keys = Object.keys(update) |
| 73 | + // $set needs to happen first |
| 74 | + if (keys.includes('$set')) { |
| 75 | + keys = ['$set'].concat(_.pull(keys, '$set')) |
| 76 | + } |
| 77 | + keys.forEach(function(key) { |
| 78 | + if (!util.updateSpecialKeys.includes(key)) { |
55 | 79 | throw new Error('The <update> document must contain only update operator expressions.')
|
56 | 80 | }
|
57 |
| - output = convertOp(output, key, update[key], fieldName) |
| 81 | + output = convertOp(output, key, _.cloneDeep(update[key]), fieldName, upsert) |
58 | 82 | })
|
59 | 83 | return output
|
60 | 84 | }
|
|
0 commit comments