2021-06-24 19:17:26 +02:00
|
|
|
import {
|
|
|
|
Integration,
|
|
|
|
DatasourceFieldTypes,
|
|
|
|
QueryTypes,
|
2021-06-27 00:09:46 +02:00
|
|
|
} from "../definitions/datasource"
|
2021-06-24 19:16:48 +02:00
|
|
|
|
|
|
|
module MongoDBModule {
|
|
|
|
const { MongoClient } = require("mongodb")
|
|
|
|
|
|
|
|
interface MongoDBConfig {
|
|
|
|
connectionString: string
|
|
|
|
db: string
|
|
|
|
}
|
|
|
|
|
|
|
|
const SCHEMA: Integration = {
|
|
|
|
docs: "https://github.com/mongodb/node-mongodb-native",
|
|
|
|
friendlyName: "MongoDB",
|
|
|
|
description:
|
|
|
|
"MongoDB is a general purpose, document-based, distributed database built for modern application developers and for the cloud era.",
|
|
|
|
datasource: {
|
|
|
|
connectionString: {
|
|
|
|
type: DatasourceFieldTypes.STRING,
|
|
|
|
required: true,
|
|
|
|
default: "mongodb://localhost:27017",
|
|
|
|
},
|
|
|
|
db: {
|
|
|
|
type: DatasourceFieldTypes.STRING,
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
query: {
|
|
|
|
create: {
|
|
|
|
type: QueryTypes.JSON,
|
|
|
|
},
|
|
|
|
read: {
|
|
|
|
type: QueryTypes.JSON,
|
|
|
|
},
|
2021-07-08 14:38:49 +02:00
|
|
|
update: {
|
|
|
|
type: QueryTypes.JSON,
|
|
|
|
},
|
|
|
|
delete: {
|
|
|
|
type: QueryTypes.JSON,
|
2021-07-29 11:11:52 +02:00
|
|
|
},
|
2021-06-24 19:16:48 +02:00
|
|
|
},
|
2021-07-08 14:38:49 +02:00
|
|
|
extra: {
|
|
|
|
collection: {
|
2021-07-29 11:11:52 +02:00
|
|
|
displayName: "Collection",
|
|
|
|
type: DatasourceFieldTypes.STRING,
|
|
|
|
required: true,
|
2021-07-08 14:38:49 +02:00
|
|
|
},
|
|
|
|
actionTypes: {
|
|
|
|
displayName: "Action Types",
|
|
|
|
type: DatasourceFieldTypes.LIST,
|
|
|
|
required: true,
|
|
|
|
data: {
|
2021-07-29 11:11:52 +02:00
|
|
|
read: ["find", "findOne", "findOneAndUpdate", "count", "distinct"],
|
|
|
|
create: ["insertOne", "insertMany"],
|
|
|
|
update: ["updateOne", "updateMany"],
|
|
|
|
delete: ["deleteOne", "deleteMany"],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-06-24 19:16:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class MongoIntegration {
|
|
|
|
private config: MongoDBConfig
|
|
|
|
private client: any
|
|
|
|
|
|
|
|
constructor(config: MongoDBConfig) {
|
|
|
|
this.config = config
|
|
|
|
this.client = new MongoClient(config.connectionString)
|
|
|
|
}
|
|
|
|
|
|
|
|
async connect() {
|
|
|
|
return this.client.connect()
|
|
|
|
}
|
|
|
|
|
2021-07-29 11:11:52 +02:00
|
|
|
async create(query: { json: object; extra: { [key: string]: string } }) {
|
2021-06-24 19:16:48 +02:00
|
|
|
try {
|
|
|
|
await this.connect()
|
|
|
|
const db = this.client.db(this.config.db)
|
2021-07-08 14:38:49 +02:00
|
|
|
const collection = db.collection(query.extra.collection)
|
|
|
|
|
2021-07-29 11:11:52 +02:00
|
|
|
// For mongodb we add an extra actionType to specify
|
2021-07-08 14:38:49 +02:00
|
|
|
// which method we want to call on the collection
|
2021-07-29 11:11:52 +02:00
|
|
|
switch (query.extra.actionTypes) {
|
|
|
|
case "insertOne": {
|
2021-07-08 14:38:49 +02:00
|
|
|
return collection.insertOne(query.json)
|
|
|
|
}
|
2021-07-29 11:11:52 +02:00
|
|
|
case "insertMany": {
|
2021-07-08 14:38:49 +02:00
|
|
|
return collection.insertOne(query.json).toArray()
|
|
|
|
}
|
|
|
|
default: {
|
2021-07-29 11:11:52 +02:00
|
|
|
throw new Error(
|
|
|
|
`actionType ${query.extra.actionTypes} does not exist on DB for create`
|
|
|
|
)
|
2021-07-08 14:38:49 +02:00
|
|
|
}
|
|
|
|
}
|
2021-06-24 19:16:48 +02:00
|
|
|
} catch (err) {
|
|
|
|
console.error("Error writing to mongodb", err)
|
|
|
|
throw err
|
|
|
|
} finally {
|
|
|
|
await this.client.close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-29 11:11:52 +02:00
|
|
|
async read(query: { json: object; extra: { [key: string]: string } }) {
|
2021-06-24 19:16:48 +02:00
|
|
|
try {
|
|
|
|
await this.connect()
|
|
|
|
const db = this.client.db(this.config.db)
|
2021-07-08 14:38:49 +02:00
|
|
|
const collection = db.collection(query.extra.collection)
|
|
|
|
|
2021-07-29 11:11:52 +02:00
|
|
|
switch (query.extra.actionTypes) {
|
|
|
|
case "find": {
|
2021-07-08 14:38:49 +02:00
|
|
|
return collection.find(query.json).toArray()
|
|
|
|
}
|
2021-07-29 11:11:52 +02:00
|
|
|
case "findOne": {
|
2021-07-08 14:38:49 +02:00
|
|
|
return collection.findOne(query.json)
|
|
|
|
}
|
2021-07-29 11:11:52 +02:00
|
|
|
case "findOneAndUpdate": {
|
2021-07-08 14:38:49 +02:00
|
|
|
return collection.findOneAndUpdate(query.json)
|
|
|
|
}
|
2021-07-29 11:11:52 +02:00
|
|
|
case "count": {
|
2021-07-08 14:38:49 +02:00
|
|
|
return collection.countDocuments(query.json)
|
|
|
|
}
|
2021-07-29 11:11:52 +02:00
|
|
|
case "distinct": {
|
2021-07-08 14:38:49 +02:00
|
|
|
return collection.distinct(query.json)
|
|
|
|
}
|
|
|
|
default: {
|
2021-07-29 11:11:52 +02:00
|
|
|
throw new Error(
|
|
|
|
`actionType ${query.extra.actionTypes} does not exist on DB for read`
|
|
|
|
)
|
2021-07-08 14:38:49 +02:00
|
|
|
}
|
|
|
|
}
|
2021-06-24 19:16:48 +02:00
|
|
|
} catch (err) {
|
|
|
|
console.error("Error querying mongodb", err)
|
|
|
|
throw err
|
|
|
|
} finally {
|
|
|
|
await this.client.close()
|
|
|
|
}
|
|
|
|
}
|
2021-07-08 14:38:49 +02:00
|
|
|
|
2021-07-29 11:11:52 +02:00
|
|
|
async update(query: { json: object; extra: { [key: string]: string } }) {
|
2021-07-08 14:38:49 +02:00
|
|
|
try {
|
|
|
|
await this.connect()
|
|
|
|
const db = this.client.db(this.config.db)
|
|
|
|
const collection = db.collection(query.extra.collection)
|
|
|
|
|
2021-07-29 11:11:52 +02:00
|
|
|
switch (query.extra.actionTypes) {
|
|
|
|
case "updateOne": {
|
2021-07-08 14:38:49 +02:00
|
|
|
return collection.updateOne(query.json)
|
|
|
|
}
|
2021-07-29 11:11:52 +02:00
|
|
|
case "updateMany": {
|
2021-07-08 14:38:49 +02:00
|
|
|
return collection.updateMany(query.json).toArray()
|
|
|
|
}
|
|
|
|
default: {
|
2021-07-29 11:11:52 +02:00
|
|
|
throw new Error(
|
|
|
|
`actionType ${query.extra.actionTypes} does not exist on DB for update`
|
|
|
|
)
|
2021-07-08 14:38:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
console.error("Error writing to mongodb", err)
|
|
|
|
throw err
|
|
|
|
} finally {
|
|
|
|
await this.client.close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-29 11:11:52 +02:00
|
|
|
async delete(query: { json: object; extra: { [key: string]: string } }) {
|
2021-07-08 14:38:49 +02:00
|
|
|
try {
|
|
|
|
await this.connect()
|
|
|
|
const db = this.client.db(this.config.db)
|
|
|
|
const collection = db.collection(query.extra.collection)
|
|
|
|
|
2021-07-29 11:11:52 +02:00
|
|
|
switch (query.extra.actionTypes) {
|
|
|
|
case "deleteOne": {
|
2021-07-08 14:38:49 +02:00
|
|
|
return collection.deleteOne(query.json)
|
|
|
|
}
|
2021-07-29 11:11:52 +02:00
|
|
|
case "deleteMany": {
|
2021-07-08 14:38:49 +02:00
|
|
|
return collection.deleteMany(query.json).toArray()
|
|
|
|
}
|
|
|
|
default: {
|
2021-07-29 11:11:52 +02:00
|
|
|
throw new Error(
|
|
|
|
`actionType ${query.extra.actionTypes} does not exist on DB for delete`
|
|
|
|
)
|
2021-07-08 14:38:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
console.error("Error writing to mongodb", err)
|
|
|
|
throw err
|
|
|
|
} finally {
|
|
|
|
await this.client.close()
|
|
|
|
}
|
|
|
|
}
|
2021-06-24 19:16:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
schema: SCHEMA,
|
|
|
|
integration: MongoIntegration,
|
|
|
|
}
|
|
|
|
}
|