Skip to content

Commit 2165d6d

Browse files
committed
fix: wip
1 parent be3acbe commit 2165d6d

File tree

3 files changed

+124
-10
lines changed

3 files changed

+124
-10
lines changed

lib/builder.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,11 @@ Builder.prototype._handleBufferValue = function (value) {
104104
};
105105

106106
Builder.prototype._handleObjectValue = function (value) {
107+
// Handle expression objects first
108+
if (value && typeof value === 'object' && value.expression) {
109+
return this.dialect.buildExpression(value.expression);
110+
}
111+
107112
// Issue #55: Support for BSON ObjectId conversion to String
108113
// Issue #57: Support for null Object type
109114
let stringValue;

lib/dialects/base/blocks.js

Lines changed: 59 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -178,12 +178,56 @@ module.exports = function (dialect) {
178178
const modifierParts = [];
179179

180180
each(modifier, (value, key) => {
181-
const modifierPart =
182-
dialect.wrapIdentifier(key) +
183-
' = ' +
184-
dialect.buildBlock('term', { term: value, type: 'value' });
181+
// Handle MongoDB operators
182+
if (key.startsWith('$')) {
183+
switch (key) {
184+
case '$set': {
185+
each(value, (setValue, setKey) => {
186+
const modifierPart =
187+
dialect.wrapIdentifier(setKey) +
188+
' = ' +
189+
dialect.buildBlock('term', { term: setValue, type: 'value' });
190+
modifierParts.push(modifierPart);
191+
});
192+
193+
break;
194+
}
195+
196+
case '$inc': {
197+
each(value, (incValue, incKey) => {
198+
const modifierPart =
199+
dialect.wrapIdentifier(incKey) +
200+
' = ' +
201+
dialect.wrapIdentifier(incKey) +
202+
' + ' +
203+
dialect.buildBlock('term', { term: incValue, type: 'value' });
204+
modifierParts.push(modifierPart);
205+
});
206+
207+
break;
208+
}
209+
210+
case '$unset': {
211+
each(value, (unsetValue, unsetKey) => {
212+
const modifierPart = dialect.wrapIdentifier(unsetKey) + ' = NULL';
213+
modifierParts.push(modifierPart);
214+
});
185215

186-
modifierParts.push(modifierPart);
216+
break;
217+
}
218+
219+
default: {
220+
throw new Error('Unknown MongoDB operator "' + key + '"');
221+
}
222+
}
223+
} else {
224+
// Handle regular field assignments
225+
const modifierPart =
226+
dialect.wrapIdentifier(key) +
227+
' = ' +
228+
dialect.buildBlock('term', { term: value, type: 'value' });
229+
modifierParts.push(modifierPart);
230+
}
187231
});
188232

189233
return 'set ' + modifierParts.join(', ');
@@ -681,6 +725,11 @@ module.exports = function (dialect) {
681725
throw new TypeError('Invalid term object');
682726
}
683727

728+
// Handle expression objects
729+
if (has(term, 'expression')) {
730+
return dialect.buildExpression(term.expression);
731+
}
732+
684733
if (has(term, 'name')) {
685734
let field = '';
686735

@@ -716,6 +765,11 @@ module.exports = function (dialect) {
716765
throw new TypeError('Invalid term object');
717766
}
718767

768+
// Handle expression objects
769+
if (has(term, 'expression')) {
770+
return dialect.buildExpression(term.expression);
771+
}
772+
719773
if (objectUtils.hasSome(term, termKeys)) {
720774
const termKey = keys(term)[0];
721775
const termValue = term[termKey];

lib/dialects/postgresql/blocks.js

Lines changed: 60 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -177,12 +177,63 @@ module.exports = function (dialect) {
177177
const modifierParts = [];
178178

179179
each(modifier, (value, key) => {
180-
const modifierPart =
181-
dialect.wrapIdentifier(key) +
182-
' = ' +
183-
dialect.buildBlock('term', { term: value, type: 'value' });
180+
// Handle MongoDB operators
181+
if (key.startsWith('$')) {
182+
switch (key) {
183+
case '$set': {
184+
each(value, (fieldValue, fieldKey) => {
185+
const modifierPart =
186+
dialect.wrapIdentifier(fieldKey) +
187+
' = ' +
188+
dialect.buildBlock('term', { term: fieldValue, type: 'value' });
189+
modifierParts.push(modifierPart);
190+
});
191+
192+
break;
193+
}
194+
195+
case '$inc': {
196+
each(value, (incValue, fieldKey) => {
197+
const modifierPart =
198+
dialect.wrapIdentifier(fieldKey) +
199+
' = ' +
200+
dialect.wrapIdentifier(fieldKey) +
201+
' + ' +
202+
dialect.buildBlock('term', { term: incValue, type: 'value' });
203+
modifierParts.push(modifierPart);
204+
});
184205

185-
modifierParts.push(modifierPart);
206+
break;
207+
}
208+
209+
case '$unset': {
210+
each(value, (unsetValue, fieldKey) => {
211+
const modifierPart = dialect.wrapIdentifier(fieldKey) + ' = NULL';
212+
modifierParts.push(modifierPart);
213+
});
214+
215+
break;
216+
}
217+
218+
default: {
219+
// For other MongoDB operators, try to handle them generically
220+
each(value, (fieldValue, fieldKey) => {
221+
const modifierPart =
222+
dialect.wrapIdentifier(fieldKey) +
223+
' = ' +
224+
dialect.buildBlock('term', { term: fieldValue, type: 'value' });
225+
modifierParts.push(modifierPart);
226+
});
227+
}
228+
}
229+
} else {
230+
// Handle regular field assignments
231+
const modifierPart =
232+
dialect.wrapIdentifier(key) +
233+
' = ' +
234+
dialect.buildBlock('term', { term: value, type: 'value' });
235+
modifierParts.push(modifierPart);
236+
}
186237
});
187238

188239
return 'set ' + modifierParts.join(', ');
@@ -559,6 +610,10 @@ module.exports = function (dialect) {
559610
}
560611

561612
if (isObject(term)) {
613+
if (has(term, 'expression')) {
614+
return dialect.buildExpression(term.expression);
615+
}
616+
562617
if (has(term, 'name')) {
563618
let field = '';
564619

0 commit comments

Comments
 (0)