Fixed update

This commit is contained in:
Mel O'Hagan 2022-05-13 14:48:07 +01:00 committed by mike12345567
parent dd6093a7ae
commit f40967784f
1 changed files with 33 additions and 6 deletions

View File

@ -1,3 +1,4 @@
import { Object } from "aws-sdk/clients/customerprofiles"
import { import {
Integration, Integration,
DatasourceFieldTypes, DatasourceFieldTypes,
@ -13,6 +14,12 @@ module MongoDBModule {
db: string db: string
} }
interface UpdateDoc {
filter: object
update: object
options: Object
}
const SCHEMA: Integration = { const SCHEMA: Integration = {
docs: "https://github.com/mongodb/node-mongodb-native", docs: "https://github.com/mongodb/node-mongodb-native",
friendlyName: "MongoDB", friendlyName: "MongoDB",
@ -77,11 +84,17 @@ module MongoDBModule {
} }
createObjectIds(json: any): object { createObjectIds(json: any): object {
const self = this
function replaceObjectIds(json: any) { function replaceObjectIds(json: any) {
for (let field of Object.keys(json)) { 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] const id = json["_id"].match(/(?<=objectid\(['"]).*(?=['"]\))/gi)[0]
json["_id"] = new ObjectID.createFromHexString(id) if (id) {
json["_id"] = new ObjectID.createFromHexString(id)
}
} }
} }
return json return json
@ -141,7 +154,12 @@ module MongoDBModule {
return await collection.findOne(json) return await collection.findOne(json)
} }
case "findOneAndUpdate": { case "findOneAndUpdate": {
return await collection.findOneAndUpdate(json) let findAndUpdateJson = json as UpdateDoc
return await collection.findOneAndUpdate(
findAndUpdateJson.filter,
findAndUpdateJson.update,
findAndUpdateJson.options
)
} }
case "count": { case "count": {
return await collection.countDocuments(json) 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 { try {
await this.connect() await this.connect()
const db = this.client.db(this.config.db) const db = this.client.db(this.config.db)
const collection = db.collection(query.extra.collection) const collection = db.collection(query.extra.collection)
let json = this.createObjectIds(query.json) as UpdateDoc
switch (query.extra.actionTypes) { switch (query.extra.actionTypes) {
case "updateOne": { case "updateOne": {
return await collection.updateOne(query.json) return await collection.updateOne(
json.filter,
json.update,
json.options
)
} }
case "updateMany": { case "updateMany": {
return await collection.updateMany(query.json).toArray() return await collection.updateMany(
json.filter,
json.update,
json.options
)
} }
default: { default: {
throw new Error( throw new Error(