From 98d8080826e6d639e7d8c4bf7143be816b6646a8 Mon Sep 17 00:00:00 2001 From: Mel O'Hagan Date: Mon, 8 Aug 2022 17:53:17 +0100 Subject: [PATCH] Bug fix findOneAndUpdate --- packages/server/__mocks__/mongodb.ts | 2 + packages/server/src/integrations/mongodb.ts | 5 +- .../src/integrations/tests/mongo.spec.js | 48 ++++++++++++++++++- 3 files changed, 53 insertions(+), 2 deletions(-) diff --git a/packages/server/__mocks__/mongodb.ts b/packages/server/__mocks__/mongodb.ts index 3b6db96b78..92ec89227f 100644 --- a/packages/server/__mocks__/mongodb.ts +++ b/packages/server/__mocks__/mongodb.ts @@ -8,6 +8,7 @@ module MongoMock { this.insertMany = jest.fn(() => ({ toArray: () => [] })) this.find = jest.fn(() => ({ toArray: () => [] })) this.findOne = jest.fn() + this.findOneAndUpdate = jest.fn() this.count = jest.fn() this.deleteOne = jest.fn() this.deleteMany = jest.fn(() => ({ toArray: () => [] })) @@ -19,6 +20,7 @@ module MongoMock { find: this.find, insertMany: this.insertMany, findOne: this.findOne, + findOneAndUpdate: this.findOneAndUpdate, count: this.count, deleteOne: this.deleteOne, deleteMany: this.deleteMany, diff --git a/packages/server/src/integrations/mongodb.ts b/packages/server/src/integrations/mongodb.ts index e300715781..a520cdb157 100644 --- a/packages/server/src/integrations/mongodb.ts +++ b/packages/server/src/integrations/mongodb.ts @@ -179,7 +179,10 @@ module MongoDBModule { return await collection.findOne(json) } case "findOneAndUpdate": { - let findAndUpdateJson = json as { + if (typeof query.json === "string") { + json = this.parseQueryParams(query.json, "update") + } + let findAndUpdateJson = this.createObjectIds(json) as { filter: FilterQuery update: UpdateQuery options: FindOneAndUpdateOption diff --git a/packages/server/src/integrations/tests/mongo.spec.js b/packages/server/src/integrations/tests/mongo.spec.js index f866048e4b..555188a526 100644 --- a/packages/server/src/integrations/tests/mongo.spec.js +++ b/packages/server/src/integrations/tests/mongo.spec.js @@ -132,6 +132,9 @@ describe("MongoDB Integration", () => { _id: mongo.ObjectID.createFromHexString("FFFF12345678ABCD12345678"), name: "ObjectId('updatedName')", }) + expect(args[2]).toEqual({ + upsert: false + }) }) it("creates ObjectIds if the $ operator fields contains a match on ObjectId", async () => { @@ -148,7 +151,7 @@ describe("MongoDB Integration", () => { }, }, options: { - upsert: false, + upsert: true, }, }, extra: { collection: "testCollection", actionTypes: "updateOne" }, @@ -167,5 +170,48 @@ describe("MongoDB Integration", () => { _id: mongo.ObjectID.createFromHexString("FFFF12345678ABCD12345678"), } }) + expect(args[2]).toEqual({ + upsert: true + }) + }) + + it("supports findOneAndUpdate", async () => { + const query = { + json: { + filter: { + _id: { + $eq: "ObjectId('ACBD12345678ABCD12345678')", + } + }, + update: { + $set: { + name: "UPDATED", + age: 99 + }, + }, + options: { + upsert: false, + }, + }, + extra: { collection: "testCollection", actionTypes: "findOneAndUpdate" }, + } + await config.integration.read(query) + expect(config.integration.client.findOneAndUpdate).toHaveBeenCalled() + + const args = config.integration.client.findOneAndUpdate.mock.calls[0] + expect(args[0]).toEqual({ + _id: { + $eq: mongo.ObjectID.createFromHexString("ACBD12345678ABCD12345678"), + } + }) + expect(args[1]).toEqual({ + $set: { + name: "UPDATED", + age: 99 + } + }) + expect(args[2]).toEqual({ + upsert: false + }) }) })