feat: Add $setOnInsert operator to Parse.Server.database.update (#8791)

This commit is contained in:
Manuel
2023-10-25 19:13:27 +02:00
committed by GitHub
parent fe02d3e8aa
commit f630a45aa5
3 changed files with 50 additions and 1 deletions

View File

@@ -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 });

View File

@@ -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)) {

View File

@@ -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');
@@ -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