From d45f503279d7261a8056a5aa0e6a3fceede21137 Mon Sep 17 00:00:00 2001 From: Manuel Trezza <5673677+mtrezza@users.noreply.github.com> Date: Wed, 25 Oct 2023 16:05:45 +0200 Subject: [PATCH 1/5] add setoninsert --- spec/MongoStorageAdapter.spec.js | 38 ++++++++++++++++++++ src/Adapters/Storage/Mongo/MongoTransform.js | 7 ++++ src/Controllers/DatabaseController.js | 5 ++- 3 files changed, 49 insertions(+), 1 deletion(-) diff --git a/spec/MongoStorageAdapter.spec.js b/spec/MongoStorageAdapter.spec.js index f78caa2608..d848b0309d 100644 --- a/spec/MongoStorageAdapter.spec.js +++ b/spec/MongoStorageAdapter.spec.js @@ -213,6 +213,44 @@ describe_only_db('mongo')('MongoStorageAdapter', () => { }); }); + it('upserts with $setOnInsert', async () => { + const uuid = require('uuid'); + const schema = { + className: 'MyClass', + fields: { + x: { type: 'Number' }, + count: { type: 'Number' }, + }, + }; + const query = { + x: 1, + }; + const update = { + objectId: { + __op: 'SetOnInsert', + amount: uuid.v4(), + }, + x: 1, + count: { + __op: 'Increment', + amount: 1, + }, + }; + const res1 = await Parse.Server.database.update( + schema, + query, + update, + { upsert: true }, + ); + update.objectId.amount = uuid.v4(); + const res2 = await Parse.Server.database.update( + schema, + query, + update, + { upsert: true }, + ); + }); + it('handles updating a single object with array, object date', done => { const adapter = new MongoStorageAdapter({ uri: databaseURI }); diff --git a/src/Adapters/Storage/Mongo/MongoTransform.js b/src/Adapters/Storage/Mongo/MongoTransform.js index 91ad23fa4a..ceff4b916b 100644 --- a/src/Adapters/Storage/Mongo/MongoTransform.js +++ b/src/Adapters/Storage/Mongo/MongoTransform.js @@ -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)) { diff --git a/src/Controllers/DatabaseController.js b/src/Controllers/DatabaseController.js index 56c37dd33a..9280a474e8 100644 --- a/src/Controllers/DatabaseController.js +++ b/src/Controllers/DatabaseController.js @@ -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'); @@ -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 From 39c9873a5e5e4a8c71d756f49d323e7eecf35c5e Mon Sep 17 00:00:00 2001 From: Manuel Trezza <5673677+mtrezza@users.noreply.github.com> Date: Wed, 25 Oct 2023 19:02:21 +0200 Subject: [PATCH 2/5] fix test --- spec/MongoStorageAdapter.spec.js | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/spec/MongoStorageAdapter.spec.js b/spec/MongoStorageAdapter.spec.js index d848b0309d..fe352b20e5 100644 --- a/spec/MongoStorageAdapter.spec.js +++ b/spec/MongoStorageAdapter.spec.js @@ -215,20 +215,15 @@ describe_only_db('mongo')('MongoStorageAdapter', () => { it('upserts with $setOnInsert', async () => { const uuid = require('uuid'); - const schema = { - className: 'MyClass', - fields: { - x: { type: 'Number' }, - count: { type: 'Number' }, - }, - }; + const uuid1 = uuid.v4(); + const uuid2 = uuid.v4(); const query = { x: 1, }; const update = { objectId: { __op: 'SetOnInsert', - amount: uuid.v4(), + amount: uuid1, }, x: 1, count: { @@ -236,19 +231,25 @@ describe_only_db('mongo')('MongoStorageAdapter', () => { amount: 1, }, }; - const res1 = await Parse.Server.database.update( - schema, + await Parse.Server.database.update( + 'MyClass', query, update, { upsert: true }, ); - update.objectId.amount = uuid.v4(); - const res2 = await Parse.Server.database.update( - schema, + 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 => { From d788f8417b581019ce7aa9938ad04ccb5e8522a4 Mon Sep 17 00:00:00 2001 From: Manuel Trezza <5673677+mtrezza@users.noreply.github.com> Date: Wed, 25 Oct 2023 19:32:07 +0200 Subject: [PATCH 3/5] test --- spec/MongoStorageAdapter.spec.js | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/spec/MongoStorageAdapter.spec.js b/spec/MongoStorageAdapter.spec.js index fe352b20e5..752ba20f18 100644 --- a/spec/MongoStorageAdapter.spec.js +++ b/spec/MongoStorageAdapter.spec.js @@ -213,10 +213,17 @@ describe_only_db('mongo')('MongoStorageAdapter', () => { }); }); - it('upserts with $setOnInsert', async () => { + fit('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' }, + }, + }; const query = { x: 1, }; @@ -225,7 +232,6 @@ describe_only_db('mongo')('MongoStorageAdapter', () => { __op: 'SetOnInsert', amount: uuid1, }, - x: 1, count: { __op: 'Increment', amount: 1, @@ -244,12 +250,18 @@ describe_only_db('mongo')('MongoStorageAdapter', () => { 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); + // const res = await Parse.Server.database.find( + // schema.className, + // schema, + // {}, + // {}, + // ); + // 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 => { From 468fc2adcc94e802bec6d133ebdc7a53156ca7a1 Mon Sep 17 00:00:00 2001 From: Manuel Trezza <5673677+mtrezza@users.noreply.github.com> Date: Wed, 25 Oct 2023 20:04:38 +0200 Subject: [PATCH 4/5] fix test --- spec/MongoStorageAdapter.spec.js | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/spec/MongoStorageAdapter.spec.js b/spec/MongoStorageAdapter.spec.js index 752ba20f18..5e6bd1745a 100644 --- a/spec/MongoStorageAdapter.spec.js +++ b/spec/MongoStorageAdapter.spec.js @@ -213,7 +213,7 @@ describe_only_db('mongo')('MongoStorageAdapter', () => { }); }); - fit('upserts with $setOnInsert', async () => { + it('upserts with $setOnInsert', async () => { const uuid = require('uuid'); const uuid1 = uuid.v4(); const uuid2 = uuid.v4(); @@ -223,7 +223,13 @@ describe_only_db('mongo')('MongoStorageAdapter', () => { 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, }; @@ -250,18 +256,16 @@ describe_only_db('mongo')('MongoStorageAdapter', () => { update, { upsert: true }, ); - // const res = await Parse.Server.database.find( - // schema.className, - // schema, - // {}, - // {}, - // ); - // 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); + + 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 => { From 8eb186603da685f4be2701ebd4677b639fe66dfe Mon Sep 17 00:00:00 2001 From: Manuel Trezza <5673677+mtrezza@users.noreply.github.com> Date: Wed, 25 Oct 2023 20:07:08 +0200 Subject: [PATCH 5/5] fix lint --- spec/MongoStorageAdapter.spec.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/MongoStorageAdapter.spec.js b/spec/MongoStorageAdapter.spec.js index 5e6bd1745a..f669196c44 100644 --- a/spec/MongoStorageAdapter.spec.js +++ b/spec/MongoStorageAdapter.spec.js @@ -225,11 +225,11 @@ describe_only_db('mongo')('MongoStorageAdapter', () => { }, classLevelPermissions: {}, }; - + const myClassSchema = new Parse.Schema(schema.className); myClassSchema.setCLP(schema.classLevelPermissions); await myClassSchema.save(); - + const query = { x: 1, };