forked from thomas4019/mongo-query-to-postgres-jsonb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate.js
103 lines (91 loc) · 5.22 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
var assert = require('chai').assert
var mongoQueryPostgres = require('../index')
var convertUpdate = mongoQueryPostgres.convertUpdate
describe('update: ', function() {
describe('replace doc', function () {
it('simple replacement', function() {
assert.equal(convertUpdate('data', { field: 'value' }), '\'{"field":"value"}\'::jsonb')
})
it('cannot mix update operators and keys', function() {
assert.throws(() => convertUpdate('data', { field: 'value', $set: { a: 'b' } }), 'The <update> document must contain only update operator expressions.')
})
})
describe('escaping', function () {
it('simple quotes', function() {
assert.equal(convertUpdate('data', { $set: { message:'I\'m a string' } }), 'jsonb_set(data,\'{message}\',\'"I\'\'m a string"\')')
})
it('nested quotes', function() {
assert.equal(convertUpdate('data', { $set: { prop_1: { message:'I\'m a string' } } }), 'jsonb_set(data,\'{prop_1}\',\'{"message":"I\'\'m a string"}\'::jsonb)')
})
})
describe('operators', function () {
it('$set', function() {
assert.equal(convertUpdate('data', { $set: { field: 'value' } }), 'jsonb_set(data,\'{field}\',\'"value"\')')
})
it('$set multiple', function() {
assert.equal(convertUpdate('data', { $set: { field: 'value', second: 2 } }), 'jsonb_set(jsonb_set(data,\'{field}\',\'"value"\'),\'{second}\',\'2\'::jsonb)')
})
it('$set deep', function() {
assert.equal(convertUpdate('data', { $set: { 'a.b': 2 } }), 'jsonb_set(jsonb_set(data,\'{a}\',COALESCE(data->\'a\', \'{}\'::jsonb)),\'{a,b}\',\'2\'::jsonb)')
})
it('$set deep 2', function() {
assert.equal(convertUpdate('data', { $set: { 'a.b.c': 2 } }), 'jsonb_set(jsonb_set(jsonb_set(data,\'{a}\',COALESCE(data->\'a\', \'{}\'::jsonb)),\'{a,b}\',COALESCE(data->\'a\'->\'b\', \'{}\'::jsonb)),\'{a,b,c}\',\'2\'::jsonb)')
})
it('$set fails for _id', function() {
assert.throws(() => convertUpdate('data', { $set: { _id: 'b' } }), 'Mod on _id not allowed')
})
it('$unset', function() {
assert.equal(convertUpdate('data', { $unset: { field: 'value' } }), 'data #- \'{field}\'')
})
it('$unset deep', function() {
assert.equal(convertUpdate('data', { $unset: { 'field.inner': 1 } }), 'data #- \'{field,inner}\'')
})
it('$unset multiple', function() {
assert.equal(convertUpdate('data', { $unset: { field: 1, second: 1 } }), 'data #- \'{field}\' #- \'{second}\'')
})
it('$inc', function() {
assert.equal(convertUpdate('data', { $inc: { count: 2 } }), 'jsonb_set(data,\'{count}\',to_jsonb(COALESCE(Cast(data->>\'count\' as numeric),0)+2))')
})
it('$mul', function() {
assert.equal(convertUpdate('data', { $mul: { count: 2 } }), 'jsonb_set(data,\'{count}\',to_jsonb(COALESCE(Cast(data->>\'count\' as numeric),0)*2),TRUE)')
})
it('$min', function() {
assert.equal(convertUpdate('data', { $min: { count: 5 } }), 'jsonb_set(data,\'{count}\',to_jsonb(LEAST(COALESCE(Cast(data->>\'count\' as numeric),0),5)))')
})
it('$max', function() {
assert.equal(convertUpdate('data', { $max: { count: 5 } }), 'jsonb_set(data,\'{count}\',to_jsonb(GREATEST(COALESCE(Cast(data->>\'count\' as numeric),0),5)))')
})
it('$rename', function() {
assert.equal(convertUpdate('data', { $rename: { current: 'newN' } }), 'jsonb_set(data,\'{newN}\',data->\'current\') #- \'{current}\'')
})
it('$pull', function() {
assert.equal(convertUpdate('data', { $pull: { cities: 'LA' } }), 'jsonb_set(data,\'{cities}\',to_jsonb(ARRAY(SELECT value FROM jsonb_array_elements(data->\'cities\') WHERE NOT value @> \'"LA"\')))')
})
it('$push', function() {
assert.equal(convertUpdate('data', { $push: { cities: 'LA' } }), 'jsonb_set(data,\'{cities}\',to_jsonb(array_append(ARRAY(SELECT value FROM jsonb_array_elements(data->\'cities\')),\'"LA"\')))')
})
it('$addToSet', function() {
assert.equal(convertUpdate('data', { $addToSet: { cities: 'LA' } }), 'jsonb_set(data,\'{cities}\',to_jsonb(array_append(ARRAY(SELECT value FROM jsonb_array_elements(data->\'cities\') WHERE value != \'"LA"\'),\'"LA"\')))')
})
})
describe('combined operators', function() {
it('$set,$inc', function() {
assert.equal(convertUpdate('data', { $set: { active: true }, $inc: { purchases: 2 } }), 'jsonb_set(jsonb_set(data,\'{active}\',\'true\'::jsonb),\'{purchases}\',to_jsonb(COALESCE(Cast(data->>\'purchases\' as numeric),0)+2))')
})
it('$set,$unset,$inc', function() {
assert.equal(convertUpdate('data', { $set: { active: true }, $unset: { field: 'value' }, $inc: { purchases: 2 } }), 'jsonb_set(jsonb_set(data,\'{active}\',\'true\'::jsonb) #- \'{field}\',\'{purchases}\',to_jsonb(COALESCE(Cast(data->>\'purchases\' as numeric),0)+2))')
})
})
describe('array by index', function () {
it('$set', function() {
assert.equal(convertUpdate('data', { $set: {
'Items.0': { test: 0 },
'Items.2': { test: 2 },
'Items.1': { test: 1 },
}}),
'jsonb_set(jsonb_set(jsonb_set(jsonb_set(data,\'{Items}\',COALESCE(data->\'Items\', ' +
'\'{}\'::jsonb)),\'{Items,0}\',\'{"test":0}\'::jsonb),\'{Items,1}\',\'{"test":1}\'::jsonb)' +
',\'{Items,2}\',\'{"test":2}\'::jsonb)')
})
})
})