fix mongo code review comment

This commit is contained in:
Martin McKeaveney 2021-07-29 10:11:52 +01:00
parent 44cdcdf38e
commit fac820f204
2 changed files with 45 additions and 38 deletions

View File

@ -51,8 +51,8 @@ export interface QueryDefinition {
export interface ExtraQueryConfig { export interface ExtraQueryConfig {
[key: string]: { [key: string]: {
displayName: string, displayName: string
type: string, type: string
required: boolean required: boolean
data?: object data?: object
} }

View File

@ -10,7 +10,6 @@ module MongoDBModule {
interface MongoDBConfig { interface MongoDBConfig {
connectionString: string connectionString: string
db: string db: string
// collection: string
} }
const SCHEMA: Integration = { const SCHEMA: Integration = {
@ -41,26 +40,26 @@ module MongoDBModule {
}, },
delete: { delete: {
type: QueryTypes.JSON, type: QueryTypes.JSON,
} },
}, },
extra: { extra: {
collection: { collection: {
displayName: "Collection", displayName: "Collection",
type: DatasourceFieldTypes.STRING, type: DatasourceFieldTypes.STRING,
required: true, required: true,
}, },
actionTypes: { actionTypes: {
displayName: "Action Types", displayName: "Action Types",
type: DatasourceFieldTypes.LIST, type: DatasourceFieldTypes.LIST,
required: true, required: true,
data: { data: {
read: ['find', 'findOne', 'findOneAndUpdate', "count", "distinct"], read: ["find", "findOne", "findOneAndUpdate", "count", "distinct"],
create: ['insertOne', 'insertMany'], create: ["insertOne", "insertMany"],
update: ['updateOne', 'updateMany'], update: ["updateOne", "updateMany"],
delete: ['deleteOne', 'deleteMany'] delete: ["deleteOne", "deleteMany"],
} },
} },
} },
} }
class MongoIntegration { class MongoIntegration {
@ -76,23 +75,25 @@ module MongoDBModule {
return this.client.connect() return this.client.connect()
} }
async create(query: { json: object, extra: { [key: string]: string } }) { async create(query: { json: object; 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)
// 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 // which method we want to call on the collection
switch(query.extra.actionTypes) { switch (query.extra.actionTypes) {
case 'insertOne': { case "insertOne": {
return collection.insertOne(query.json) return collection.insertOne(query.json)
} }
case 'insertMany': { case "insertMany": {
return collection.insertOne(query.json).toArray() return collection.insertOne(query.json).toArray()
} }
default: { 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) { } 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 { 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)
switch(query.extra.actionTypes) { switch (query.extra.actionTypes) {
case 'find': { case "find": {
return collection.find(query.json).toArray() return collection.find(query.json).toArray()
} }
case 'findOne': { case "findOne": {
return collection.findOne(query.json) return collection.findOne(query.json)
} }
case 'findOneAndUpdate': { case "findOneAndUpdate": {
return collection.findOneAndUpdate(query.json) return collection.findOneAndUpdate(query.json)
} }
case 'count': { case "count": {
return collection.countDocuments(query.json) return collection.countDocuments(query.json)
} }
case 'distinct': { case "distinct": {
return collection.distinct(query.json) return collection.distinct(query.json)
} }
default: { 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) { } 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 { 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)
switch(query.extra.actionTypes) { switch (query.extra.actionTypes) {
case 'updateOne': { case "updateOne": {
return collection.updateOne(query.json) return collection.updateOne(query.json)
} }
case 'updateMany': { case "updateMany": {
return collection.updateMany(query.json).toArray() return collection.updateMany(query.json).toArray()
} }
default: { 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) { } 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 { 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)
switch(query.extra.actionTypes) { switch (query.extra.actionTypes) {
case 'deleteOne': { case "deleteOne": {
return collection.deleteOne(query.json) return collection.deleteOne(query.json)
} }
case 'deleteMany': { case "deleteMany": {
return collection.deleteMany(query.json).toArray() return collection.deleteMany(query.json).toArray()
} }
default: { 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) { } catch (err) {