Skip to content

Commit

Permalink
feat: Add $setOnInsert operator to Parse.Server.database.update (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
mtrezza committed Oct 25, 2023
1 parent fe02d3e commit f630a45
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
39 changes: 39 additions & 0 deletions spec/MongoStorageAdapter.spec.js
Expand Up @@ -254,6 +254,45 @@ describe_only_db('mongo')('MongoStorageAdapter', () => {
expect(obj.get('foo').test.date[0] instanceof Date).toBeTrue();
});

it('upserts with $setOnInsert', async () => {
const uuid = require('uuid');
const uuid1 = uuid.v4();
const uuid2 = uuid.v4();
const query = {
x: 1,
};
const update = {
objectId: {
__op: 'SetOnInsert',
amount: uuid1,
},
x: 1,
count: {
__op: 'Increment',
amount: 1,
},
};
await Parse.Server.database.update(
'MyClass',
query,
update,
{ upsert: true },
);
update.objectId.amount = uuid2;
await Parse.Server.database.update(
'MyClass',
query,
update,
{ upsert: true },
);
const q = new Parse.Query('MyClass');
const docs = await q.find();
expect(docs.length).toBe(1);
expect(docs[0].id).toBe(uuid1);
expect(docs[0].get('x')).toBe(1);
expect(docs[0].get('count')).toBe(2);
});

it('handles updating a single object with array, object date', done => {
const adapter = new MongoStorageAdapter({ uri: databaseURI });

Expand Down
7 changes: 7 additions & 0 deletions src/Adapters/Storage/Mongo/MongoTransform.js
Expand Up @@ -986,6 +986,13 @@ function transformUpdateOperator({ __op, amount, objects }, flatten) {
return { __op: '$inc', arg: amount };
}

case 'SetOnInsert':
if (flatten) {
return amount;
} else {
return { __op: '$setOnInsert', arg: amount };
}

case 'Add':
case 'AddUnique':
if (!(objects instanceof Array)) {
Expand Down
5 changes: 4 additions & 1 deletion src/Controllers/DatabaseController.js
Expand Up @@ -279,6 +279,9 @@ const flattenUpdateOperatorsForCreate = object => {
}
object[key] = object[key].amount;
break;
case 'SetOnInsert':
object[key] = object[key].amount;
break;
case 'Add':
if (!(object[key].objects instanceof Array)) {
throw new Parse.Error(Parse.Error.INVALID_JSON, 'objects to add must be an array');
Expand Down Expand Up @@ -1817,7 +1820,7 @@ class DatabaseController {
keyUpdate &&
typeof keyUpdate === 'object' &&
keyUpdate.__op &&
['Add', 'AddUnique', 'Remove', 'Increment'].indexOf(keyUpdate.__op) > -1
['Add', 'AddUnique', 'Remove', 'Increment', 'SetOnInsert'].indexOf(keyUpdate.__op) > -1
) {
// only valid ops that produce an actionable result
// the op may have happened on a keypath
Expand Down

0 comments on commit f630a45

Please sign in to comment.