fix mongo code review comment

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

View File

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

View File

@ -10,7 +10,6 @@ module MongoDBModule {
interface MongoDBConfig {
connectionString: string
db: string
// collection: string
}
const SCHEMA: Integration = {
@ -41,7 +40,7 @@ module MongoDBModule {
},
delete: {
type: QueryTypes.JSON,
}
},
},
extra: {
collection: {
@ -54,13 +53,13 @@ module MongoDBModule {
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,7 +75,7 @@ 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)
@ -84,15 +83,17 @@ module MongoDBModule {
// 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) {