From dd6093a7ae0ca0938a3bdf9351b88e492f6bba46 Mon Sep 17 00:00:00 2001 From: Mel O'Hagan Date: Fri, 13 May 2022 13:49:26 +0100 Subject: [PATCH 01/10] Create ObjectIds from matching string --- packages/server/src/integrations/mongodb.ts | 38 ++++++++++++++++----- 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/packages/server/src/integrations/mongodb.ts b/packages/server/src/integrations/mongodb.ts index c955b43a65..044ca27fc8 100644 --- a/packages/server/src/integrations/mongodb.ts +++ b/packages/server/src/integrations/mongodb.ts @@ -6,7 +6,7 @@ import { import { IntegrationBase } from "./base/IntegrationBase" module MongoDBModule { - const { MongoClient } = require("mongodb") + const { MongoClient, ObjectID } = require("mongodb") interface MongoDBConfig { connectionString: string @@ -76,20 +76,41 @@ module MongoDBModule { return this.client.connect() } + createObjectIds(json: any): object { + function replaceObjectIds(json: any) { + for (let field of Object.keys(json)) { + if (field === "_id" && json["_id"].includes("ObjectId")) { + const id = json["_id"].match(/(?<=objectid\(['"]).*(?=['"]\))/gi)[0] + json["_id"] = new ObjectID.createFromHexString(id) + } + } + return json + } + + if (Array.isArray(json)) { + for (let i = 0; i < json.length; i++) { + json[i] = replaceObjectIds(json[i]) + } + return json + } + return replaceObjectIds(json) + } + async create(query: { json: object; extra: { [key: string]: string } }) { try { await this.connect() const db = this.client.db(this.config.db) const collection = db.collection(query.extra.collection) + let json = this.createObjectIds(query.json) // For mongodb we add an extra actionType to specify // which method we want to call on the collection switch (query.extra.actionTypes) { case "insertOne": { - return await collection.insertOne(query.json) + return await collection.insertOne(json) } case "insertMany": { - return await collection.insertOne(query.json).toArray() + return await collection.insertMany(json) } default: { throw new Error( @@ -110,22 +131,23 @@ module MongoDBModule { await this.connect() const db = this.client.db(this.config.db) const collection = db.collection(query.extra.collection) + let json = this.createObjectIds(query.json) switch (query.extra.actionTypes) { case "find": { - return await collection.find(query.json).toArray() + return await collection.find(json).toArray() } case "findOne": { - return await collection.findOne(query.json) + return await collection.findOne(json) } case "findOneAndUpdate": { - return await collection.findOneAndUpdate(query.json) + return await collection.findOneAndUpdate(json) } case "count": { - return await collection.countDocuments(query.json) + return await collection.countDocuments(json) } case "distinct": { - return await collection.distinct(query.json) + return await collection.distinct(json) } default: { throw new Error( From f40967784f77fd95bd41bad333b72f4159b2a7b2 Mon Sep 17 00:00:00 2001 From: Mel O'Hagan Date: Fri, 13 May 2022 14:48:07 +0100 Subject: [PATCH 02/10] Fixed update --- packages/server/src/integrations/mongodb.ts | 39 +++++++++++++++++---- 1 file changed, 33 insertions(+), 6 deletions(-) diff --git a/packages/server/src/integrations/mongodb.ts b/packages/server/src/integrations/mongodb.ts index 044ca27fc8..ae6bd3b0dc 100644 --- a/packages/server/src/integrations/mongodb.ts +++ b/packages/server/src/integrations/mongodb.ts @@ -1,3 +1,4 @@ +import { Object } from "aws-sdk/clients/customerprofiles" import { Integration, DatasourceFieldTypes, @@ -13,6 +14,12 @@ module MongoDBModule { db: string } + interface UpdateDoc { + filter: object + update: object + options: Object + } + const SCHEMA: Integration = { docs: "https://github.com/mongodb/node-mongodb-native", friendlyName: "MongoDB", @@ -77,11 +84,17 @@ module MongoDBModule { } createObjectIds(json: any): object { + const self = this function replaceObjectIds(json: any) { for (let field of Object.keys(json)) { - if (field === "_id" && json["_id"].includes("ObjectId")) { + if (json[field] instanceof Object) { + json[field] = self.createObjectIds(json[field]) + } + if (field === "_id") { const id = json["_id"].match(/(?<=objectid\(['"]).*(?=['"]\))/gi)[0] - json["_id"] = new ObjectID.createFromHexString(id) + if (id) { + json["_id"] = new ObjectID.createFromHexString(id) + } } } return json @@ -141,7 +154,12 @@ module MongoDBModule { return await collection.findOne(json) } case "findOneAndUpdate": { - return await collection.findOneAndUpdate(json) + let findAndUpdateJson = json as UpdateDoc + return await collection.findOneAndUpdate( + findAndUpdateJson.filter, + findAndUpdateJson.update, + findAndUpdateJson.options + ) } case "count": { return await collection.countDocuments(json) @@ -163,18 +181,27 @@ module MongoDBModule { } } - async update(query: { json: object; extra: { [key: string]: string } }) { + async update(query: { json: UpdateDoc; extra: { [key: string]: string } }) { try { await this.connect() const db = this.client.db(this.config.db) const collection = db.collection(query.extra.collection) + let json = this.createObjectIds(query.json) as UpdateDoc switch (query.extra.actionTypes) { case "updateOne": { - return await collection.updateOne(query.json) + return await collection.updateOne( + json.filter, + json.update, + json.options + ) } case "updateMany": { - return await collection.updateMany(query.json).toArray() + return await collection.updateMany( + json.filter, + json.update, + json.options + ) } default: { throw new Error( From 035a7ee625eafdb1b01a4cdfde2ea2dd4d790770 Mon Sep 17 00:00:00 2001 From: Mel O'Hagan Date: Fri, 13 May 2022 14:53:04 +0100 Subject: [PATCH 03/10] MongoDb Delete fix --- packages/server/src/integrations/mongodb.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/server/src/integrations/mongodb.ts b/packages/server/src/integrations/mongodb.ts index ae6bd3b0dc..2cdc5c5ebc 100644 --- a/packages/server/src/integrations/mongodb.ts +++ b/packages/server/src/integrations/mongodb.ts @@ -222,13 +222,14 @@ module MongoDBModule { await this.connect() const db = this.client.db(this.config.db) const collection = db.collection(query.extra.collection) + let json = this.createObjectIds(query.json) switch (query.extra.actionTypes) { case "deleteOne": { - return await collection.deleteOne(query.json) + return await collection.deleteOne(json) } case "deleteMany": { - return await collection.deleteMany(query.json).toArray() + return await collection.deleteMany(json) } default: { throw new Error( From c2fc1598fa56671b2336316eeb26f6866a0161b4 Mon Sep 17 00:00:00 2001 From: Mel O'Hagan Date: Fri, 13 May 2022 15:07:26 +0100 Subject: [PATCH 04/10] Null pointer fix --- packages/server/src/integrations/mongodb.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/server/src/integrations/mongodb.ts b/packages/server/src/integrations/mongodb.ts index 2cdc5c5ebc..afb7c4109c 100644 --- a/packages/server/src/integrations/mongodb.ts +++ b/packages/server/src/integrations/mongodb.ts @@ -91,7 +91,9 @@ module MongoDBModule { json[field] = self.createObjectIds(json[field]) } if (field === "_id") { - const id = json["_id"].match(/(?<=objectid\(['"]).*(?=['"]\))/gi)[0] + const id = json["_id"].match( + /(?<=objectid\(['"]).*(?=['"]\))/gi + )?.[0] if (id) { json["_id"] = new ObjectID.createFromHexString(id) } From 0d3ab9ae332f605f02cbfdeddc47a4fa1b31065f Mon Sep 17 00:00:00 2001 From: Mel O'Hagan Date: Fri, 13 May 2022 15:56:02 +0100 Subject: [PATCH 05/10] Fix test for update --- .../src/integrations/tests/mongo.spec.js | 44 ++++++++++++------- 1 file changed, 28 insertions(+), 16 deletions(-) diff --git a/packages/server/src/integrations/tests/mongo.spec.js b/packages/server/src/integrations/tests/mongo.spec.js index 430ccc1c3a..1260491796 100644 --- a/packages/server/src/integrations/tests/mongo.spec.js +++ b/packages/server/src/integrations/tests/mongo.spec.js @@ -4,19 +4,19 @@ jest.mock("mongodb") class TestConfiguration { constructor(config = {}) { - this.integration = new MongoDBIntegration.integration(config) + this.integration = new MongoDBIntegration.integration(config) } } function disableConsole() { - jest.spyOn(console, 'error'); - console.error.mockImplementation(() => {}); + jest.spyOn(console, "error") + console.error.mockImplementation(() => {}) - return console.error.mockRestore; + return console.error.mockRestore } describe("MongoDB Integration", () => { - let config + let config let indexName = "Users" beforeEach(() => { @@ -25,12 +25,12 @@ describe("MongoDB Integration", () => { it("calls the create method with the correct params", async () => { const body = { - name: "Hello" + name: "Hello", } await config.integration.create({ index: indexName, json: body, - extra: { collection: 'testCollection', actionTypes: 'insertOne'} + extra: { collection: "testCollection", actionTypes: "insertOne" }, }) expect(config.integration.client.insertOne).toHaveBeenCalledWith(body) }) @@ -38,9 +38,9 @@ describe("MongoDB Integration", () => { it("calls the read method with the correct params", async () => { const query = { json: { - address: "test" + address: "test", }, - extra: { collection: 'testCollection', actionTypes: 'find'} + extra: { collection: "testCollection", actionTypes: "find" }, } const response = await config.integration.read(query) expect(config.integration.client.find).toHaveBeenCalledWith(query.json) @@ -50,9 +50,9 @@ describe("MongoDB Integration", () => { it("calls the delete method with the correct params", async () => { const query = { json: { - id: "test" + id: "test", }, - extra: { collection: 'testCollection', actionTypes: 'deleteOne'} + extra: { collection: "testCollection", actionTypes: "deleteOne" }, } await config.integration.delete(query) expect(config.integration.client.deleteOne).toHaveBeenCalledWith(query.json) @@ -61,19 +61,31 @@ describe("MongoDB Integration", () => { it("calls the update method with the correct params", async () => { const query = { json: { - id: "test" + filter: { + id: "test", + }, + update: { + name: "TestName", + }, + options: { + upsert: false, + }, }, - extra: { collection: 'testCollection', actionTypes: 'updateOne'} + extra: { collection: "testCollection", actionTypes: "updateOne" }, } await config.integration.update(query) - expect(config.integration.client.updateOne).toHaveBeenCalledWith(query.json) + expect(config.integration.client.updateOne).toHaveBeenCalledWith( + query.json.filter, + query.json.update, + query.json.options + ) }) it("throws an error when an invalid query.extra.actionType is passed for each method", async () => { const restore = disableConsole() const query = { - extra: { collection: 'testCollection', actionTypes: 'deleteOne'} + extra: { collection: "testCollection", actionTypes: "deleteOne" }, } let error = null @@ -85,4 +97,4 @@ describe("MongoDB Integration", () => { expect(error).toBeDefined() restore() }) -}) \ No newline at end of file +}) From 81f16aa7ce5e69ed425eee66d239e1c6e276729d Mon Sep 17 00:00:00 2001 From: Mel O'Hagan Date: Fri, 13 May 2022 21:22:10 +0100 Subject: [PATCH 06/10] Check type is string before match --- packages/server/src/integrations/mongodb.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/server/src/integrations/mongodb.ts b/packages/server/src/integrations/mongodb.ts index afb7c4109c..8237cd7844 100644 --- a/packages/server/src/integrations/mongodb.ts +++ b/packages/server/src/integrations/mongodb.ts @@ -85,12 +85,12 @@ module MongoDBModule { createObjectIds(json: any): object { const self = this - function replaceObjectIds(json: any) { + function interpolateObjectIds(json: any) { for (let field of Object.keys(json)) { if (json[field] instanceof Object) { json[field] = self.createObjectIds(json[field]) } - if (field === "_id") { + if (field === "_id" && typeof json[field] === "string") { const id = json["_id"].match( /(?<=objectid\(['"]).*(?=['"]\))/gi )?.[0] @@ -104,11 +104,11 @@ module MongoDBModule { if (Array.isArray(json)) { for (let i = 0; i < json.length; i++) { - json[i] = replaceObjectIds(json[i]) + json[i] = interpolateObjectIds(json[i]) } return json } - return replaceObjectIds(json) + return interpolateObjectIds(json) } async create(query: { json: object; extra: { [key: string]: string } }) { From e3883d480d5a19b44027f751e6bffb31730a15ce Mon Sep 17 00:00:00 2001 From: Mel O'Hagan Date: Mon, 16 May 2022 08:42:09 +0100 Subject: [PATCH 07/10] Lowercase the object typo --- packages/server/src/integrations/mongodb.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/server/src/integrations/mongodb.ts b/packages/server/src/integrations/mongodb.ts index 8237cd7844..55b4cdaaf4 100644 --- a/packages/server/src/integrations/mongodb.ts +++ b/packages/server/src/integrations/mongodb.ts @@ -17,7 +17,7 @@ module MongoDBModule { interface UpdateDoc { filter: object update: object - options: Object + options: object } const SCHEMA: Integration = { From 7b570fea221661233e35a45d0bfd0dc67c344b3c Mon Sep 17 00:00:00 2001 From: Mel O'Hagan Date: Mon, 16 May 2022 08:43:27 +0100 Subject: [PATCH 08/10] Removed unused aws Object import --- packages/server/src/integrations/mongodb.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/server/src/integrations/mongodb.ts b/packages/server/src/integrations/mongodb.ts index 55b4cdaaf4..3ecc67258b 100644 --- a/packages/server/src/integrations/mongodb.ts +++ b/packages/server/src/integrations/mongodb.ts @@ -1,4 +1,3 @@ -import { Object } from "aws-sdk/clients/customerprofiles" import { Integration, DatasourceFieldTypes, From 0da9ee0a56a94f434d0e24abee427459be327819 Mon Sep 17 00:00:00 2001 From: Mel O'Hagan Date: Mon, 16 May 2022 09:50:47 +0100 Subject: [PATCH 09/10] Using mongodb types --- package.json | 3 +- packages/server/src/integrations/mongodb.ts | 47 +++++++++++------ .../src/integrations/tests/mongo.spec.js | 9 +++- yarn.lock | 52 ++++++++++++++++--- 4 files changed, 85 insertions(+), 26 deletions(-) diff --git a/package.json b/package.json index fb6d9da990..84f1999ead 100644 --- a/package.json +++ b/package.json @@ -3,6 +3,8 @@ "private": true, "devDependencies": { "@rollup/plugin-json": "^4.0.2", + "@types/mongodb": "3.6.3", + "@typescript-eslint/parser": "4.28.0", "babel-eslint": "^10.0.3", "eslint": "^7.28.0", "eslint-plugin-cypress": "^2.11.3", @@ -16,7 +18,6 @@ "rimraf": "^3.0.2", "rollup-plugin-replace": "^2.2.0", "svelte": "^3.38.2", - "@typescript-eslint/parser": "4.28.0", "typescript": "4.5.5" }, "scripts": { diff --git a/packages/server/src/integrations/mongodb.ts b/packages/server/src/integrations/mongodb.ts index 3ecc67258b..0823d73721 100644 --- a/packages/server/src/integrations/mongodb.ts +++ b/packages/server/src/integrations/mongodb.ts @@ -4,21 +4,23 @@ import { QueryTypes, } from "../definitions/datasource" import { IntegrationBase } from "./base/IntegrationBase" +import { + MongoClient, + ObjectID, + FilterQuery, + UpdateQuery, + FindOneAndUpdateOption, + UpdateOneOptions, + UpdateManyOptions, + CommonOptions, +} from "mongodb" module MongoDBModule { - const { MongoClient, ObjectID } = require("mongodb") - interface MongoDBConfig { connectionString: string db: string } - interface UpdateDoc { - filter: object - update: object - options: object - } - const SCHEMA: Integration = { docs: "https://github.com/mongodb/node-mongodb-native", friendlyName: "MongoDB", @@ -94,7 +96,7 @@ module MongoDBModule { /(?<=objectid\(['"]).*(?=['"]\))/gi )?.[0] if (id) { - json["_id"] = new ObjectID.createFromHexString(id) + json["_id"] = ObjectID.createFromHexString(id) } } } @@ -155,7 +157,11 @@ module MongoDBModule { return await collection.findOne(json) } case "findOneAndUpdate": { - let findAndUpdateJson = json as UpdateDoc + let findAndUpdateJson = json as { + filter: FilterQuery + update: UpdateQuery + options: FindOneAndUpdateOption + } return await collection.findOneAndUpdate( findAndUpdateJson.filter, findAndUpdateJson.update, @@ -182,26 +188,30 @@ module MongoDBModule { } } - async update(query: { json: UpdateDoc; extra: { [key: string]: string } }) { + async update(query: { json: object; extra: { [key: string]: string } }) { try { await this.connect() const db = this.client.db(this.config.db) const collection = db.collection(query.extra.collection) - let json = this.createObjectIds(query.json) as UpdateDoc + let json = this.createObjectIds(query.json) as { + filter: FilterQuery + update: UpdateQuery + options: object + } switch (query.extra.actionTypes) { case "updateOne": { return await collection.updateOne( json.filter, json.update, - json.options + json.options as UpdateOneOptions ) } case "updateMany": { return await collection.updateMany( json.filter, json.update, - json.options + json.options as UpdateManyOptions ) } default: { @@ -223,14 +233,17 @@ module MongoDBModule { await this.connect() const db = this.client.db(this.config.db) const collection = db.collection(query.extra.collection) - let json = this.createObjectIds(query.json) + let json = this.createObjectIds(query.json) as { + filter: FilterQuery + options: CommonOptions + } switch (query.extra.actionTypes) { case "deleteOne": { - return await collection.deleteOne(json) + return await collection.deleteOne(json.filter, json.options) } case "deleteMany": { - return await collection.deleteMany(json) + return await collection.deleteMany(json.filter, json.options) } default: { throw new Error( diff --git a/packages/server/src/integrations/tests/mongo.spec.js b/packages/server/src/integrations/tests/mongo.spec.js index 1260491796..b0a49521ec 100644 --- a/packages/server/src/integrations/tests/mongo.spec.js +++ b/packages/server/src/integrations/tests/mongo.spec.js @@ -50,12 +50,17 @@ describe("MongoDB Integration", () => { it("calls the delete method with the correct params", async () => { const query = { json: { - id: "test", + filter: { + id: "test", + }, + options: { + opt: "option" + } }, extra: { collection: "testCollection", actionTypes: "deleteOne" }, } await config.integration.delete(query) - expect(config.integration.client.deleteOne).toHaveBeenCalledWith(query.json) + expect(config.integration.client.deleteOne).toHaveBeenCalledWith(query.json.filter, query.json.options) }) it("calls the update method with the correct params", async () => { diff --git a/yarn.lock b/yarn.lock index 129b650c73..276dd62d08 100644 --- a/yarn.lock +++ b/yarn.lock @@ -972,6 +972,13 @@ estree-walker "^1.0.1" picomatch "^2.2.2" +"@types/bson@*": + version "4.2.0" + resolved "https://registry.yarnpkg.com/@types/bson/-/bson-4.2.0.tgz#a2f71e933ff54b2c3bf267b67fa221e295a33337" + integrity sha512-ELCPqAdroMdcuxqwMgUpifQyRoTpyYCNr1V9xKyF40VsBobsj+BbWNRvwGchMgBPGqkw655ypkjj2MEF5ywVwg== + dependencies: + bson "*" + "@types/estree@0.0.39": version "0.0.39" resolved "https://registry.yarnpkg.com/@types/estree/-/estree-0.0.39.tgz#e177e699ee1b8c22d23174caaa7422644389509f" @@ -982,6 +989,19 @@ resolved "https://registry.yarnpkg.com/@types/minimist/-/minimist-1.2.2.tgz#ee771e2ba4b3dc5b372935d549fd9617bf345b8c" integrity sha512-jhuKLIRrhvCPLqwPcx6INqmKeiA5EWrsCOPhrlFSrbrmU4ZMPjj5Ul/oLCMDO98XRUIwVm78xICz4EPCektzeQ== +"@types/mongodb@3.6.3": + version "3.6.3" + resolved "https://registry.yarnpkg.com/@types/mongodb/-/mongodb-3.6.3.tgz#5655af409d9e32d5d5ae9a653abf3e5f9c83eb7a" + integrity sha512-6YNqGP1hk5bjUFaim+QoFFuI61WjHiHE1BNeB41TA00Xd2K7zG4lcWyLLq/XtIp36uMavvS5hoAUJ+1u/GcX2Q== + dependencies: + "@types/bson" "*" + "@types/node" "*" + +"@types/node@*": + version "17.0.33" + resolved "https://registry.yarnpkg.com/@types/node/-/node-17.0.33.tgz#3c1879b276dc63e73030bb91165e62a4509cd506" + integrity sha512-miWq2m2FiQZmaHfdZNcbpp9PuXg34W5JZ5CrJ/BaS70VuhoJENBEQybeiYSaPBRNq6KQGnjfEnc/F3PN++D+XQ== + "@types/node@>= 8": version "17.0.18" resolved "https://registry.yarnpkg.com/@types/node/-/node-17.0.18.tgz#3b4fed5cfb58010e3a2be4b6e74615e4847f1074" @@ -1300,6 +1320,11 @@ balanced-match@^1.0.0: resolved "https://registry.yarnpkg.com/balanced-match/-/balanced-match-1.0.2.tgz#e83e3a7e3f300b34cb9d87f615fa0cbf357690ee" integrity sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw== +base64-js@^1.3.1: + version "1.5.1" + resolved "https://registry.yarnpkg.com/base64-js/-/base64-js-1.5.1.tgz#1b1b440160a5bf7ad40b650f095963481903930a" + integrity sha512-AKpaYlHn8t4SVbOHCy+b5+KKgvR4vrsD8vbvrbiQJps7fKDTkjkDry6ji0rUJjC0kzbNePLwzxq8iypo41qeWA== + base@^0.11.1: version "0.11.2" resolved "https://registry.yarnpkg.com/base/-/base-0.11.2.tgz#7bde5ced145b6d551a90db87f83c558b4eb48a8f" @@ -1361,6 +1386,13 @@ braces@^3.0.2: dependencies: fill-range "^7.0.1" +bson@*: + version "4.6.3" + resolved "https://registry.yarnpkg.com/bson/-/bson-4.6.3.tgz#d1a9a0b84b9e84b62390811fc5580f6a8b1d858c" + integrity sha512-rAqP5hcUVJhXP2MCSNVsf0oM2OGU1So6A9pVRDYayvJ5+hygXHQApf87wd5NlhPM1J9RJnbqxIG/f8QTzRoQ4A== + dependencies: + buffer "^5.6.0" + btoa-lite@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/btoa-lite/-/btoa-lite-1.0.0.tgz#337766da15801210fdd956c22e9c6891ab9d0337" @@ -1371,6 +1403,14 @@ buffer-from@^1.0.0: resolved "https://registry.yarnpkg.com/buffer-from/-/buffer-from-1.1.2.tgz#2b146a6fd72e80b4f55d255f35ed59a3a9a41bd5" integrity sha512-E+XQCRwSbaaiChtv6k6Dwgc+bx+Bs6vuKJHHl5kox/BaKbhiXzqQOwK4cO22yElGp2OCmjwVhT3HmxgyPGnJfQ== +buffer@^5.6.0: + version "5.7.1" + resolved "https://registry.yarnpkg.com/buffer/-/buffer-5.7.1.tgz#ba62e7c13133053582197160851a8f648e99eed0" + integrity sha512-EHcyIPBQ4BSGlvjB16k5KgAJ27CIsHY/2JBmCRReo48y9rQ3MaUzWX3KVlBa4U7MyX02HdVj0K7C3WaB3ju7FQ== + dependencies: + base64-js "^1.3.1" + ieee754 "^1.1.13" + builtins@^1.0.3: version "1.0.3" resolved "https://registry.yarnpkg.com/builtins/-/builtins-1.0.3.tgz#cb94faeb61c8696451db36534e1422f94f0aee88" @@ -2969,6 +3009,11 @@ iconv-lite@^0.6.2: dependencies: safer-buffer ">= 2.1.2 < 3.0.0" +ieee754@^1.1.13: + version "1.2.1" + resolved "https://registry.yarnpkg.com/ieee754/-/ieee754-1.2.1.tgz#8eb7a10a63fff25d15a57b001586d177d1b0d352" + integrity sha512-dcyqhDvX1C46lXZcVqCpK+FtMRQVdIMN6/Df5js2zouUsqG7I6sFxitIC+7KYK29KdXOLHdu9zL4sFnoVQnqaA== + iferr@^0.1.5: version "0.1.5" resolved "https://registry.yarnpkg.com/iferr/-/iferr-0.1.5.tgz#c60eed69e6d8fdb6b3104a1fcbca1c192dc5b501" @@ -4663,12 +4708,7 @@ performance-now@^2.1.0: resolved "https://registry.yarnpkg.com/performance-now/-/performance-now-2.1.0.tgz#6309f4e0e5fa913ec1c69307ae364b4b377c9e7b" integrity sha1-Ywn04OX6kT7BxpMHrjZLSzd8nns= -picomatch@^2.2.2: - version "2.3.1" - resolved "https://registry.yarnpkg.com/picomatch/-/picomatch-2.3.1.tgz#3ba3833733646d9d3e4995946c1365a67fb07a42" - integrity sha512-JU3teHTNjmE2VCGFzuY8EXzCDVwEqB2a8fsIvwaStHhAWJEeVd1o1QD80CU6+ZdEXXSLbSsuLwJjkCBWqRQUVA== - -picomatch@^2.3.1: +picomatch@^2.2.2, picomatch@^2.3.1: version "2.3.1" resolved "https://registry.yarnpkg.com/picomatch/-/picomatch-2.3.1.tgz#3ba3833733646d9d3e4995946c1365a67fb07a42" integrity sha512-JU3teHTNjmE2VCGFzuY8EXzCDVwEqB2a8fsIvwaStHhAWJEeVd1o1QD80CU6+ZdEXXSLbSsuLwJjkCBWqRQUVA== From 0f4aaa477907c415de9485e21b3fa52a9e580db0 Mon Sep 17 00:00:00 2001 From: Mel O'Hagan Date: Mon, 16 May 2022 22:44:38 +0100 Subject: [PATCH 10/10] Parse params if needed --- packages/server/src/integrations/mongodb.ts | 39 +++++++++++++++++++-- packages/server/yarn.lock | 18 +++++----- packages/worker/yarn.lock | 18 +++++----- 3 files changed, 55 insertions(+), 20 deletions(-) diff --git a/packages/server/src/integrations/mongodb.ts b/packages/server/src/integrations/mongodb.ts index 0823d73721..ae6754907b 100644 --- a/packages/server/src/integrations/mongodb.ts +++ b/packages/server/src/integrations/mongodb.ts @@ -112,6 +112,33 @@ module MongoDBModule { return interpolateObjectIds(json) } + parseQueryParams(params: string, mode: string) { + let queryParams = params.split(/(?<=(},)).*{/g) + let group1 = queryParams[0] + let group2 = queryParams[2] + let group3 = queryParams[4] + if (group1) { + group1 = JSON.parse(group1.replace(/,+$/, "")) + } + if (group2) { + group2 = JSON.parse("{" + group2.replace(/,+$/, "")) + } + if (group3) { + group3 = JSON.parse("{" + group3.replace(/,+$/, "")) + } + if (mode === "update") { + return { + filter: group1, + update: group2, + options: group3 ?? {}, + } + } + return { + filter: group1, + options: group2 ?? {}, + } + } + async create(query: { json: object; extra: { [key: string]: string } }) { try { await this.connect() @@ -193,7 +220,11 @@ module MongoDBModule { await this.connect() const db = this.client.db(this.config.db) const collection = db.collection(query.extra.collection) - let json = this.createObjectIds(query.json) as { + let queryJson = query.json + if (typeof queryJson === "string") { + queryJson = this.parseQueryParams(queryJson, "update") + } + let json = this.createObjectIds(queryJson) as { filter: FilterQuery update: UpdateQuery options: object @@ -233,7 +264,11 @@ module MongoDBModule { await this.connect() const db = this.client.db(this.config.db) const collection = db.collection(query.extra.collection) - let json = this.createObjectIds(query.json) as { + let queryJson = query.json + if (typeof queryJson === "string") { + queryJson = this.parseQueryParams(queryJson, "delete") + } + let json = this.createObjectIds(queryJson) as { filter: FilterQuery options: CommonOptions } diff --git a/packages/server/yarn.lock b/packages/server/yarn.lock index d7212c2f48..f5e51de813 100644 --- a/packages/server/yarn.lock +++ b/packages/server/yarn.lock @@ -1014,10 +1014,10 @@ resolved "https://registry.yarnpkg.com/@bcoe/v8-coverage/-/v8-coverage-0.2.3.tgz#75a2e8b51cb758a7553d6804a5932d7aace75c39" integrity sha512-0hYQ8SB4Db5zvZB4axdMHGwEaQjkZzFjQiN9LVYvIFB2nSUHW9tYpxWriPrWDASIxiaXax83REcLxuSdnGPZtw== -"@budibase/backend-core@1.0.154": - version "1.0.154" - resolved "https://registry.yarnpkg.com/@budibase/backend-core/-/backend-core-1.0.154.tgz#c310834892e7621778b07579464955487c5c9830" - integrity sha512-mcZxt8XhGgOB4XRHKkWTvBEI4HGp2bo8qyzOJRCvDqlg56S9zqGJDl75Z0N/Wc8N3I53QRcxISerj48odX172A== +"@budibase/backend-core@1.0.160": + version "1.0.160" + resolved "https://registry.yarnpkg.com/@budibase/backend-core/-/backend-core-1.0.160.tgz#000e3b5a3ed91e73a542b4caa202a6f147d91294" + integrity sha512-XfAFU6sRPrCSEKlm58WeuPw8lUoJK+KwO0tcbT+bB2Nb7XCHplskryEgk/PM9ujRU6SMPDx11zKeqRebHlycbA== dependencies: "@techpass/passport-openidconnect" "^0.3.0" aws-sdk "^2.901.0" @@ -1091,12 +1091,12 @@ svelte-flatpickr "^3.2.3" svelte-portal "^1.0.0" -"@budibase/pro@1.0.154": - version "1.0.154" - resolved "https://registry.yarnpkg.com/@budibase/pro/-/pro-1.0.154.tgz#f4e31e30376b206159b711224038141d73a1118e" - integrity sha512-+O6bemrcgyWG4a+D5dIOoZ+LGjW4aN7tRdFeZqoaIPCc1pA6zNtLUkM1nb+Laafuwq2Aht37vEuaRy7jfzVprA== +"@budibase/pro@1.0.160": + version "1.0.160" + resolved "https://registry.yarnpkg.com/@budibase/pro/-/pro-1.0.160.tgz#921c4e3f65b866d84292644dfd8793c4d0b667c7" + integrity sha512-p+Jhnk1n98CWCJXydSQSO7a+HDpqGAHekGQbOR7aayuwuoYzyOXxTcHNLdBp+3lkXhLSZq9oIwfEGpgdrrhXPA== dependencies: - "@budibase/backend-core" "1.0.154" + "@budibase/backend-core" "1.0.160" node-fetch "^2.6.1" "@budibase/standard-components@^0.9.139": diff --git a/packages/worker/yarn.lock b/packages/worker/yarn.lock index a5e4c2d9e7..1b31c8ef50 100644 --- a/packages/worker/yarn.lock +++ b/packages/worker/yarn.lock @@ -293,10 +293,10 @@ resolved "https://registry.yarnpkg.com/@bcoe/v8-coverage/-/v8-coverage-0.2.3.tgz#75a2e8b51cb758a7553d6804a5932d7aace75c39" integrity sha512-0hYQ8SB4Db5zvZB4axdMHGwEaQjkZzFjQiN9LVYvIFB2nSUHW9tYpxWriPrWDASIxiaXax83REcLxuSdnGPZtw== -"@budibase/backend-core@1.0.154": - version "1.0.154" - resolved "https://registry.yarnpkg.com/@budibase/backend-core/-/backend-core-1.0.154.tgz#c310834892e7621778b07579464955487c5c9830" - integrity sha512-mcZxt8XhGgOB4XRHKkWTvBEI4HGp2bo8qyzOJRCvDqlg56S9zqGJDl75Z0N/Wc8N3I53QRcxISerj48odX172A== +"@budibase/backend-core@1.0.160": + version "1.0.160" + resolved "https://registry.yarnpkg.com/@budibase/backend-core/-/backend-core-1.0.160.tgz#000e3b5a3ed91e73a542b4caa202a6f147d91294" + integrity sha512-XfAFU6sRPrCSEKlm58WeuPw8lUoJK+KwO0tcbT+bB2Nb7XCHplskryEgk/PM9ujRU6SMPDx11zKeqRebHlycbA== dependencies: "@techpass/passport-openidconnect" "^0.3.0" aws-sdk "^2.901.0" @@ -321,12 +321,12 @@ uuid "^8.3.2" zlib "^1.0.5" -"@budibase/pro@1.0.154": - version "1.0.154" - resolved "https://registry.yarnpkg.com/@budibase/pro/-/pro-1.0.154.tgz#f4e31e30376b206159b711224038141d73a1118e" - integrity sha512-+O6bemrcgyWG4a+D5dIOoZ+LGjW4aN7tRdFeZqoaIPCc1pA6zNtLUkM1nb+Laafuwq2Aht37vEuaRy7jfzVprA== +"@budibase/pro@1.0.160": + version "1.0.160" + resolved "https://registry.yarnpkg.com/@budibase/pro/-/pro-1.0.160.tgz#921c4e3f65b866d84292644dfd8793c4d0b667c7" + integrity sha512-p+Jhnk1n98CWCJXydSQSO7a+HDpqGAHekGQbOR7aayuwuoYzyOXxTcHNLdBp+3lkXhLSZq9oIwfEGpgdrrhXPA== dependencies: - "@budibase/backend-core" "1.0.154" + "@budibase/backend-core" "1.0.160" node-fetch "^2.6.1" "@cspotcode/source-map-consumer@0.8.0":