Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add $setOnInsert operator to Parse.Server.database.update #8790

Merged
merged 5 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions spec/MongoStorageAdapter.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,61 @@ describe_only_db('mongo')('MongoStorageAdapter', () => {
});
});

it('upserts with $setOnInsert', async () => {
const uuid = require('uuid');
const uuid1 = uuid.v4();
const uuid2 = uuid.v4();
const schema = {
className: 'MyClass',
fields: {
x: { type: 'Number' },
count: { type: 'Number' },
},
classLevelPermissions: {},
};

const myClassSchema = new Parse.Schema(schema.className);
myClassSchema.setCLP(schema.classLevelPermissions);
await myClassSchema.save();

const query = {
x: 1,
};
const update = {
objectId: {
__op: 'SetOnInsert',
amount: uuid1,
},
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 res = await Parse.Server.database.find(
schema.className,
{},
{},
);
expect(res.length).toBe(1);
expect(res[0].objectId).toBe(uuid1);
expect(res[0].count).toBe(2);
expect(res[0].x).toBe(1);
});

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
Original file line number Diff line number Diff line change
Expand Up @@ -976,6 +976,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
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,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 @@ -1813,7 +1816,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
Loading