Skip to content

Commit 8689535

Browse files
committed
add feature
1 parent fe02d3e commit 8689535

File tree

3 files changed

+49
-1
lines changed

3 files changed

+49
-1
lines changed

spec/MongoStorageAdapter.spec.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,44 @@ describe_only_db('mongo')('MongoStorageAdapter', () => {
254254
expect(obj.get('foo').test.date[0] instanceof Date).toBeTrue();
255255
});
256256

257+
it('upserts with $setOnInsert', async () => {
258+
const uuid = require('uuid');
259+
const schema = {
260+
className: 'MyClass',
261+
fields: {
262+
x: { type: 'Number' },
263+
count: { type: 'Number' },
264+
},
265+
};
266+
const query = {
267+
x: 1,
268+
};
269+
const update = {
270+
objectId: {
271+
__op: 'SetOnInsert',
272+
amount: uuid.v4(),
273+
},
274+
x: 1,
275+
count: {
276+
__op: 'Increment',
277+
amount: 1,
278+
},
279+
};
280+
const res1 = await Parse.Server.database.update(
281+
schema,
282+
query,
283+
update,
284+
{ upsert: true },
285+
);
286+
update.objectId.amount = uuid.v4();
287+
const res2 = await Parse.Server.database.update(
288+
schema,
289+
query,
290+
update,
291+
{ upsert: true },
292+
);
293+
});
294+
257295
it('handles updating a single object with array, object date', done => {
258296
const adapter = new MongoStorageAdapter({ uri: databaseURI });
259297

src/Adapters/Storage/Mongo/MongoTransform.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -986,6 +986,13 @@ function transformUpdateOperator({ __op, amount, objects }, flatten) {
986986
return { __op: '$inc', arg: amount };
987987
}
988988

989+
case 'SetOnInsert':
990+
if (flatten) {
991+
return amount;
992+
} else {
993+
return { __op: '$setOnInsert', arg: amount };
994+
}
995+
989996
case 'Add':
990997
case 'AddUnique':
991998
if (!(objects instanceof Array)) {

src/Controllers/DatabaseController.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,9 @@ const flattenUpdateOperatorsForCreate = object => {
279279
}
280280
object[key] = object[key].amount;
281281
break;
282+
case 'SetOnInsert':
283+
object[key] = object[key].amount;
284+
break;
282285
case 'Add':
283286
if (!(object[key].objects instanceof Array)) {
284287
throw new Parse.Error(Parse.Error.INVALID_JSON, 'objects to add must be an array');
@@ -1817,7 +1820,7 @@ class DatabaseController {
18171820
keyUpdate &&
18181821
typeof keyUpdate === 'object' &&
18191822
keyUpdate.__op &&
1820-
['Add', 'AddUnique', 'Remove', 'Increment'].indexOf(keyUpdate.__op) > -1
1823+
['Add', 'AddUnique', 'Remove', 'Increment', 'SetOnInsert'].indexOf(keyUpdate.__op) > -1
18211824
) {
18221825
// only valid ops that produce an actionable result
18231826
// the op may have happened on a keypath

0 commit comments

Comments
 (0)