From 0f1bb2ccff70bbf163eac889adb732eda563d470 Mon Sep 17 00:00:00 2001 From: Martin McKeaveney Date: Thu, 29 Jul 2021 10:11:52 +0100 Subject: [PATCH] fix mongo code review comment --- packages/server/src/definitions/datasource.ts | 4 +- packages/server/src/integrations/mongodb.ts | 79 ++++++++++--------- 2 files changed, 45 insertions(+), 38 deletions(-) diff --git a/packages/server/src/definitions/datasource.ts b/packages/server/src/definitions/datasource.ts index 63de7f0d2a..ec6a0db9c9 100644 --- a/packages/server/src/definitions/datasource.ts +++ b/packages/server/src/definitions/datasource.ts @@ -51,8 +51,8 @@ export interface QueryDefinition { export interface ExtraQueryConfig { [key: string]: { - displayName: string, - type: string, + displayName: string + type: string required: boolean data?: object } diff --git a/packages/server/src/integrations/mongodb.ts b/packages/server/src/integrations/mongodb.ts index ab21da6fd0..bbab9a0d84 100644 --- a/packages/server/src/integrations/mongodb.ts +++ b/packages/server/src/integrations/mongodb.ts @@ -10,7 +10,6 @@ module MongoDBModule { interface MongoDBConfig { connectionString: string db: string - // collection: string } const SCHEMA: Integration = { @@ -41,26 +40,26 @@ module MongoDBModule { }, delete: { type: QueryTypes.JSON, - } + }, }, extra: { collection: { - displayName: "Collection", - type: DatasourceFieldTypes.STRING, - required: true, + displayName: "Collection", + type: DatasourceFieldTypes.STRING, + required: true, }, actionTypes: { displayName: "Action Types", type: DatasourceFieldTypes.LIST, required: true, data: { - read: ['find', 'findOne', 'findOneAndUpdate', "count", "distinct"], - create: ['insertOne', 'insertMany'], - update: ['updateOne', 'updateMany'], - delete: ['deleteOne', 'deleteMany'] - } - } - } + read: ["find", "findOne", "findOneAndUpdate", "count", "distinct"], + create: ["insertOne", "insertMany"], + update: ["updateOne", "updateMany"], + delete: ["deleteOne", "deleteMany"], + }, + }, + }, } class MongoIntegration { @@ -76,23 +75,25 @@ module MongoDBModule { return this.client.connect() } - async create(query: { json: object, extra: { [key: string]: string } }) { + 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) - // For mongodb we add an extra actionType to specify + // For mongodb we add an extra actionType to specify // which method we want to call on the collection - switch(query.extra.actionTypes) { - case 'insertOne': { + switch (query.extra.actionTypes) { + case "insertOne": { return collection.insertOne(query.json) } - case 'insertMany': { + case "insertMany": { return collection.insertOne(query.json).toArray() } default: { - throw new Error(`actionType ${query.extra.actionTypes} does not exist on DB for create`) + throw new Error( + `actionType ${query.extra.actionTypes} does not exist on DB for create` + ) } } } catch (err) { @@ -103,30 +104,32 @@ module MongoDBModule { } } - async read(query: { json: object, extra: { [key: string]: string } }) { + async read(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) - switch(query.extra.actionTypes) { - case 'find': { + switch (query.extra.actionTypes) { + case "find": { return collection.find(query.json).toArray() } - case 'findOne': { + case "findOne": { return collection.findOne(query.json) } - case 'findOneAndUpdate': { + case "findOneAndUpdate": { return collection.findOneAndUpdate(query.json) } - case 'count': { + case "count": { return collection.countDocuments(query.json) } - case 'distinct': { + case "distinct": { return collection.distinct(query.json) } default: { - throw new Error(`actionType ${query.extra.actionTypes} does not exist on DB for read`) + throw new Error( + `actionType ${query.extra.actionTypes} does not exist on DB for read` + ) } } } catch (err) { @@ -137,21 +140,23 @@ module MongoDBModule { } } - async update(query: { json: object, 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) - switch(query.extra.actionTypes) { - case 'updateOne': { + switch (query.extra.actionTypes) { + case "updateOne": { return collection.updateOne(query.json) } - case 'updateMany': { + case "updateMany": { return collection.updateMany(query.json).toArray() } default: { - throw new Error(`actionType ${query.extra.actionTypes} does not exist on DB for update`) + throw new Error( + `actionType ${query.extra.actionTypes} does not exist on DB for update` + ) } } } catch (err) { @@ -162,21 +167,23 @@ module MongoDBModule { } } - async delete(query: { json: object, extra: { [key: string]: string } }) { + async delete(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) - switch(query.extra.actionTypes) { - case 'deleteOne': { + switch (query.extra.actionTypes) { + case "deleteOne": { return collection.deleteOne(query.json) } - case 'deleteMany': { + case "deleteMany": { return collection.deleteMany(query.json).toArray() } default: { - throw new Error(`actionType ${query.extra.actionTypes} does not exist on DB for delete`) + throw new Error( + `actionType ${query.extra.actionTypes} does not exist on DB for delete` + ) } } } catch (err) {