2021-06-24 19:17:26 +02:00
|
|
|
import {
|
|
|
|
Integration,
|
|
|
|
DatasourceFieldTypes,
|
|
|
|
QueryTypes,
|
2021-06-27 00:09:46 +02:00
|
|
|
} from "../definitions/datasource"
|
2021-11-10 20:35:09 +01:00
|
|
|
import { IntegrationBase } from "./base/IntegrationBase"
|
2022-05-16 10:50:47 +02:00
|
|
|
import {
|
|
|
|
MongoClient,
|
|
|
|
ObjectID,
|
|
|
|
FilterQuery,
|
|
|
|
UpdateQuery,
|
|
|
|
FindOneAndUpdateOption,
|
|
|
|
UpdateOneOptions,
|
|
|
|
UpdateManyOptions,
|
|
|
|
CommonOptions,
|
|
|
|
} from "mongodb"
|
2021-06-24 19:16:48 +02:00
|
|
|
|
|
|
|
module MongoDBModule {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-11-10 20:35:09 +01:00
|
|
|
class MongoIntegration implements IntegrationBase {
|
2021-06-24 19:16:48 +02:00
|
|
|
private config: MongoDBConfig
|
|
|
|
private client: any
|
|
|
|
|
|
|
|
constructor(config: MongoDBConfig) {
|
|
|
|
this.config = config
|
|
|
|
this.client = new MongoClient(config.connectionString)
|
|
|
|
}
|
|
|
|
|
|
|
|
async connect() {
|
|
|
|
return this.client.connect()
|
|
|
|
}
|
|
|
|
|
2022-05-13 14:49:26 +02:00
|
|
|
createObjectIds(json: any): object {
|
2022-05-13 15:48:07 +02:00
|
|
|
const self = this
|
2022-05-13 22:22:10 +02:00
|
|
|
function interpolateObjectIds(json: any) {
|
2022-05-13 14:49:26 +02:00
|
|
|
for (let field of Object.keys(json)) {
|
2022-05-13 15:48:07 +02:00
|
|
|
if (json[field] instanceof Object) {
|
|
|
|
json[field] = self.createObjectIds(json[field])
|
|
|
|
}
|
2022-05-13 22:22:10 +02:00
|
|
|
if (field === "_id" && typeof json[field] === "string") {
|
2022-05-13 16:07:26 +02:00
|
|
|
const id = json["_id"].match(
|
|
|
|
/(?<=objectid\(['"]).*(?=['"]\))/gi
|
|
|
|
)?.[0]
|
2022-05-13 15:48:07 +02:00
|
|
|
if (id) {
|
2022-05-16 10:50:47 +02:00
|
|
|
json["_id"] = ObjectID.createFromHexString(id)
|
2022-05-13 15:48:07 +02:00
|
|
|
}
|
2022-05-13 14:49:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return json
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Array.isArray(json)) {
|
|
|
|
for (let i = 0; i < json.length; i++) {
|
2022-05-13 22:22:10 +02:00
|
|
|
json[i] = interpolateObjectIds(json[i])
|
2022-05-13 14:49:26 +02:00
|
|
|
}
|
|
|
|
return json
|
|
|
|
}
|
2022-05-13 22:22:10 +02:00
|
|
|
return interpolateObjectIds(json)
|
2022-05-13 14:49:26 +02:00
|
|
|
}
|
|
|
|
|
2022-05-16 23:44:38 +02:00
|
|
|
parseQueryParams(params: string, mode: string) {
|
|
|
|
let queryParams = params.split(/(?<=(},)).*{/g)
|
|
|
|
let group1 = queryParams[0]
|
|
|
|
let group2 = queryParams[2]
|
|
|
|
let group3 = queryParams[4]
|
|
|
|
if (group1) {
|
|
|
|
group1 = JSON.parse(group1.replace(/,+$/, ""))
|
|
|
|
}
|
|
|
|
if (group2) {
|
|
|
|
group2 = JSON.parse("{" + group2.replace(/,+$/, ""))
|
|
|
|
}
|
|
|
|
if (group3) {
|
|
|
|
group3 = JSON.parse("{" + group3.replace(/,+$/, ""))
|
|
|
|
}
|
|
|
|
if (mode === "update") {
|
|
|
|
return {
|
|
|
|
filter: group1,
|
|
|
|
update: group2,
|
|
|
|
options: group3 ?? {},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
filter: group1,
|
|
|
|
options: group2 ?? {},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)
|
2022-05-13 14:49:26 +02:00
|
|
|
let json = this.createObjectIds(query.json)
|
2021-07-08 14:38:49 +02:00
|
|
|
|
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": {
|
2022-05-13 14:49:26 +02:00
|
|
|
return await collection.insertOne(json)
|
2021-07-08 14:38:49 +02:00
|
|
|
}
|
2021-07-29 11:11:52 +02:00
|
|
|
case "insertMany": {
|
2022-05-13 14:49:26 +02:00
|
|
|
return await collection.insertMany(json)
|
2021-07-08 14:38:49 +02:00
|
|
|
}
|
|
|
|
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)
|
2022-05-13 14:49:26 +02:00
|
|
|
let json = this.createObjectIds(query.json)
|
2021-07-08 14:38:49 +02:00
|
|
|
|
2021-07-29 11:11:52 +02:00
|
|
|
switch (query.extra.actionTypes) {
|
|
|
|
case "find": {
|
2022-05-13 14:49:26 +02:00
|
|
|
return await collection.find(json).toArray()
|
2021-07-08 14:38:49 +02:00
|
|
|
}
|
2021-07-29 11:11:52 +02:00
|
|
|
case "findOne": {
|
2022-05-13 14:49:26 +02:00
|
|
|
return await collection.findOne(json)
|
2021-07-08 14:38:49 +02:00
|
|
|
}
|
2021-07-29 11:11:52 +02:00
|
|
|
case "findOneAndUpdate": {
|
2022-05-16 10:50:47 +02:00
|
|
|
let findAndUpdateJson = json as {
|
|
|
|
filter: FilterQuery<any>
|
|
|
|
update: UpdateQuery<any>
|
|
|
|
options: FindOneAndUpdateOption<any>
|
|
|
|
}
|
2022-05-13 15:48:07 +02:00
|
|
|
return await collection.findOneAndUpdate(
|
|
|
|
findAndUpdateJson.filter,
|
|
|
|
findAndUpdateJson.update,
|
|
|
|
findAndUpdateJson.options
|
|
|
|
)
|
2021-07-08 14:38:49 +02:00
|
|
|
}
|
2021-07-29 11:11:52 +02:00
|
|
|
case "count": {
|
2022-05-13 14:49:26 +02:00
|
|
|
return await collection.countDocuments(json)
|
2021-07-08 14:38:49 +02:00
|
|
|
}
|
2021-07-29 11:11:52 +02:00
|
|
|
case "distinct": {
|
2022-05-13 14:49:26 +02:00
|
|
|
return await collection.distinct(json)
|
2021-07-08 14:38:49 +02:00
|
|
|
}
|
|
|
|
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
|
|
|
|
2022-05-16 10:50:47 +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)
|
2022-05-16 23:44:38 +02:00
|
|
|
let queryJson = query.json
|
|
|
|
if (typeof queryJson === "string") {
|
|
|
|
queryJson = this.parseQueryParams(queryJson, "update")
|
|
|
|
}
|
|
|
|
let json = this.createObjectIds(queryJson) as {
|
2022-05-16 10:50:47 +02:00
|
|
|
filter: FilterQuery<any>
|
|
|
|
update: UpdateQuery<any>
|
|
|
|
options: object
|
|
|
|
}
|
2021-07-08 14:38:49 +02:00
|
|
|
|
2021-07-29 11:11:52 +02:00
|
|
|
switch (query.extra.actionTypes) {
|
|
|
|
case "updateOne": {
|
2022-05-13 15:48:07 +02:00
|
|
|
return await collection.updateOne(
|
|
|
|
json.filter,
|
|
|
|
json.update,
|
2022-05-16 10:50:47 +02:00
|
|
|
json.options as UpdateOneOptions
|
2022-05-13 15:48:07 +02:00
|
|
|
)
|
2021-07-08 14:38:49 +02:00
|
|
|
}
|
2021-07-29 11:11:52 +02:00
|
|
|
case "updateMany": {
|
2022-05-13 15:48:07 +02:00
|
|
|
return await collection.updateMany(
|
|
|
|
json.filter,
|
|
|
|
json.update,
|
2022-05-16 10:50:47 +02:00
|
|
|
json.options as UpdateManyOptions
|
2022-05-13 15:48:07 +02:00
|
|
|
)
|
2021-07-08 14:38:49 +02:00
|
|
|
}
|
|
|
|
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)
|
2022-05-16 23:44:38 +02:00
|
|
|
let queryJson = query.json
|
|
|
|
if (typeof queryJson === "string") {
|
|
|
|
queryJson = this.parseQueryParams(queryJson, "delete")
|
|
|
|
}
|
|
|
|
let json = this.createObjectIds(queryJson) as {
|
2022-05-16 10:50:47 +02:00
|
|
|
filter: FilterQuery<any>
|
|
|
|
options: CommonOptions
|
|
|
|
}
|
2021-07-08 14:38:49 +02:00
|
|
|
|
2021-07-29 11:11:52 +02:00
|
|
|
switch (query.extra.actionTypes) {
|
|
|
|
case "deleteOne": {
|
2022-05-16 10:50:47 +02:00
|
|
|
return await collection.deleteOne(json.filter, json.options)
|
2021-07-08 14:38:49 +02:00
|
|
|
}
|
2021-07-29 11:11:52 +02:00
|
|
|
case "deleteMany": {
|
2022-05-16 10:50:47 +02:00
|
|
|
return await collection.deleteMany(json.filter, json.options)
|
2021-07-08 14:38:49 +02:00
|
|
|
}
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|