Skip to content

Commit 0910728

Browse files
Merge pull request #39 from RoobinGood/feature/add-postgresql10-json-operators
Add postgresql10 json operators: concatenate (||), delete json (-), delete by key (#-)
2 parents 81b5847 + c368e35 commit 0910728

3 files changed

Lines changed: 64 additions & 0 deletions

File tree

lib/dialects/postgresql/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ var util = require('util');
77
var templatesInit = require('./templates');
88
var blocksInit = require('./blocks');
99
var operatorsInit = require('./operators');
10+
var modifiersInit = require('./modifiers');
1011

1112
var Dialect = module.exports = function(builder) {
1213
BaseDialect.call(this, builder);
@@ -19,6 +20,9 @@ var Dialect = module.exports = function(builder) {
1920

2021
// init operators
2122
operatorsInit(this);
23+
24+
// init modifiers
25+
modifiersInit(this);
2226
};
2327

2428
util.inherits(Dialect, BaseDialect);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'use strict';
2+
3+
module.exports = function(dialect) {
4+
dialect.modifiers.add('$jsonConcatenate', function(field, value) {
5+
return [field, '=', field, '||', value].join(' ');
6+
});
7+
8+
dialect.modifiers.add('$jsonDelete', function(field, value) {
9+
return [field, '=', field, '-', value].join(' ');
10+
});
11+
12+
dialect.modifiers.add('$jsonDeleteByPath', function(field, value) {
13+
return [field, '=', field, '#-', value].join(' ');
14+
});
15+
};

tests/6_dialects/0_postgresql.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,51 @@ describe('PostgreSQL dialect', function() {
122122
);
123123
expect(result.values).to.be.eql(['hello%']);
124124
});
125+
126+
it('should be ok with `$jsonConcatenate` modification operator', function() {
127+
var result = jsonSql.build({
128+
type: 'update',
129+
table: 'test',
130+
modifier: {
131+
$jsonConcatenate: {
132+
params: {c: 1}
133+
}
134+
}
135+
});
136+
137+
expect(result.query).to.be.equal('update "test" set "params" = "params" || $1;');
138+
expect(result.values).to.be.eql(['{"c":1}']);
139+
});
140+
141+
it('should be ok with `$jsonDelete` modification operator', function() {
142+
var result = jsonSql.build({
143+
type: 'update',
144+
table: 'test',
145+
modifier: {
146+
$jsonDelete: {
147+
params: {c: 1}
148+
}
149+
}
150+
});
151+
152+
expect(result.query).to.be.equal('update "test" set "params" = "params" - $1;');
153+
expect(result.values).to.be.eql(['{"c":1}']);
154+
});
155+
156+
it('should be ok with `$jsonDeleteByPath` modification operator', function() {
157+
var result = jsonSql.build({
158+
type: 'update',
159+
table: 'test',
160+
modifier: {
161+
$jsonDeleteByPath: {
162+
params: '{1,d}'
163+
}
164+
}
165+
});
166+
167+
expect(result.query).to.be.equal('update "test" set "params" = "params" #- $1;');
168+
expect(result.values).to.be.eql(['{1,d}']);
169+
});
125170
});
126171

127172
describe('explain', function() {

0 commit comments

Comments
 (0)