From f40967784f77fd95bd41bad333b72f4159b2a7b2 Mon Sep 17 00:00:00 2001 From: Mel O'Hagan Date: Fri, 13 May 2022 14:48:07 +0100 Subject: [PATCH] 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(