budibase/packages/server/src/integrations/mongodb.ts

204 lines
5.6 KiB
TypeScript
Raw Normal View History

2021-06-24 19:17:26 +02:00
import {
Integration,
DatasourceFieldTypes,
QueryTypes,
} from "../definitions/datasource"
import { IntegrationBase } from "./base/IntegrationBase"
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,
},
update: {
type: QueryTypes.JSON,
},
delete: {
type: QueryTypes.JSON,
2021-07-29 11:11:52 +02:00
},
},
extra: {
collection: {
2021-07-29 11:11:52 +02:00
displayName: "Collection",
type: DatasourceFieldTypes.STRING,
required: true,
},
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"],
},
},
},
}
class MongoIntegration implements IntegrationBase {
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 } }) {
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
// For mongodb we add an extra actionType to specify
// which method we want to call on the collection
2021-07-29 11:11:52 +02:00
switch (query.extra.actionTypes) {
case "insertOne": {
2021-09-30 16:05:15 +02:00
return await collection.insertOne(query.json)
}
2021-07-29 11:11:52 +02:00
case "insertMany": {
2021-09-30 16:05:15 +02:00
return await 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`
)
}
}
} 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 } }) {
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 "find": {
2021-09-30 16:05:15 +02:00
return await collection.find(query.json).toArray()
}
2021-07-29 11:11:52 +02:00
case "findOne": {
2021-09-30 16:05:15 +02:00
return await collection.findOne(query.json)
}
2021-07-29 11:11:52 +02:00
case "findOneAndUpdate": {
2021-09-30 16:05:15 +02:00
return await collection.findOneAndUpdate(query.json)
}
2021-07-29 11:11:52 +02:00
case "count": {
2021-09-30 16:05:15 +02:00
return await collection.countDocuments(query.json)
}
2021-07-29 11:11:52 +02:00
case "distinct": {
2021-09-30 16:05:15 +02:00
return await 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`
)
}
}
} catch (err) {
console.error("Error querying mongodb", err)
throw err
} finally {
await this.client.close()
}
}
2021-07-29 11:11:52 +02:00
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)
2021-07-29 11:11:52 +02:00
switch (query.extra.actionTypes) {
case "updateOne": {
2021-09-30 16:05:15 +02:00
return await collection.updateOne(query.json)
}
2021-07-29 11:11:52 +02:00
case "updateMany": {
2021-09-30 16:05:15 +02:00
return await 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`
)
}
}
} 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 } }) {
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-09-30 16:05:15 +02:00
return await collection.deleteOne(query.json)
}
2021-07-29 11:11:52 +02:00
case "deleteMany": {
2021-09-30 16:05:15 +02:00
return await 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`
)
}
}
} catch (err) {
console.error("Error writing to mongodb", err)
throw err
} finally {
await this.client.close()
}
}
}
module.exports = {
schema: SCHEMA,
integration: MongoIntegration,
}
}