1
0
Fork 0
mirror of synced 2024-06-01 18:20:18 +12:00
budibase/packages/server/src/integrations/mongodb.ts

301 lines
8.3 KiB
TypeScript
Raw Normal View History

2021-06-25 05:17:26 +12:00
import {
Integration,
DatasourceFieldTypes,
QueryTypes,
IntegrationBase,
} from "@budibase/types"
2022-05-16 20:50:47 +12:00
import {
MongoClient,
ObjectID,
FilterQuery,
UpdateQuery,
FindOneAndUpdateOption,
UpdateOneOptions,
UpdateManyOptions,
CommonOptions,
} from "mongodb"
module MongoDBModule {
interface MongoDBConfig {
connectionString: string
db: string
}
const SCHEMA: Integration = {
docs: "https://github.com/mongodb/node-mongodb-native",
friendlyName: "MongoDB",
2022-06-23 22:35:57 +12:00
type: "Non-relational",
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 21:11:52 +12:00
},
},
extra: {
collection: {
2021-07-29 21:11:52 +12:00
displayName: "Collection",
type: DatasourceFieldTypes.STRING,
required: true,
},
actionTypes: {
displayName: "Action Types",
type: DatasourceFieldTypes.LIST,
required: true,
data: {
2021-07-29 21:11:52 +12: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()
}
2022-05-14 00:49:26 +12:00
createObjectIds(json: any): object {
2022-05-14 01:48:07 +12:00
const self = this
2022-05-14 08:22:10 +12:00
function interpolateObjectIds(json: any) {
2022-05-14 00:49:26 +12:00
for (let field of Object.keys(json)) {
2022-05-14 01:48:07 +12:00
if (json[field] instanceof Object) {
json[field] = self.createObjectIds(json[field])
}
2022-05-14 08:22:10 +12:00
if (field === "_id" && typeof json[field] === "string") {
2022-05-14 02:07:26 +12:00
const id = json["_id"].match(
/(?<=objectid\(['"]).*(?=['"]\))/gi
)?.[0]
2022-05-14 01:48:07 +12:00
if (id) {
2022-05-16 20:50:47 +12:00
json["_id"] = ObjectID.createFromHexString(id)
2022-05-14 01:48:07 +12:00
}
2022-05-14 00:49:26 +12:00
}
}
return json
}
if (Array.isArray(json)) {
for (let i = 0; i < json.length; i++) {
2022-05-14 08:22:10 +12:00
json[i] = interpolateObjectIds(json[i])
2022-05-14 00:49:26 +12:00
}
return json
}
2022-05-14 08:22:10 +12:00
return interpolateObjectIds(json)
2022-05-14 00:49:26 +12:00
}
2022-05-17 09:44:38 +12:00
parseQueryParams(params: string, mode: string) {
2022-05-17 23:11:43 +12:00
let queryParams = params.split(/(?<=}),[\n\s]*(?={)/g)
let group1 = queryParams[0] ? JSON.parse(queryParams[0]) : {}
let group2 = queryParams[1] ? JSON.parse(queryParams[1]) : {}
let group3 = queryParams[2] ? JSON.parse(queryParams[2]) : {}
2022-05-17 09:44:38 +12:00
if (mode === "update") {
return {
filter: group1,
update: group2,
2022-05-17 23:11:43 +12:00
options: group3,
2022-05-17 09:44:38 +12:00
}
}
return {
filter: group1,
2022-05-17 23:11:43 +12:00
options: group2,
2022-05-17 09:44:38 +12:00
}
}
2021-07-29 21:11:52 +12: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)
2022-05-14 00:49:26 +12:00
let json = this.createObjectIds(query.json)
2021-07-29 21:11:52 +12:00
// For mongodb we add an extra actionType to specify
// which method we want to call on the collection
2021-07-29 21:11:52 +12:00
switch (query.extra.actionTypes) {
case "insertOne": {
2022-05-14 00:49:26 +12:00
return await collection.insertOne(json)
}
2021-07-29 21:11:52 +12:00
case "insertMany": {
2022-05-14 00:49:26 +12:00
return await collection.insertMany(json)
}
default: {
2021-07-29 21:11:52 +12: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 21:11:52 +12: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)
2022-05-14 00:49:26 +12:00
let json = this.createObjectIds(query.json)
2021-07-29 21:11:52 +12:00
switch (query.extra.actionTypes) {
case "find": {
2022-05-14 00:49:26 +12:00
return await collection.find(json).toArray()
}
2021-07-29 21:11:52 +12:00
case "findOne": {
2022-05-14 00:49:26 +12:00
return await collection.findOne(json)
}
2021-07-29 21:11:52 +12:00
case "findOneAndUpdate": {
2022-05-16 20:50:47 +12:00
let findAndUpdateJson = json as {
filter: FilterQuery<any>
update: UpdateQuery<any>
options: FindOneAndUpdateOption<any>
}
2022-05-14 01:48:07 +12:00
return await collection.findOneAndUpdate(
findAndUpdateJson.filter,
findAndUpdateJson.update,
findAndUpdateJson.options
)
}
2021-07-29 21:11:52 +12:00
case "count": {
2022-05-14 00:49:26 +12:00
return await collection.countDocuments(json)
}
2021-07-29 21:11:52 +12:00
case "distinct": {
2022-05-14 00:49:26 +12:00
return await collection.distinct(json)
}
default: {
2021-07-29 21:11:52 +12: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()
}
}
2022-05-16 20:50:47 +12: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)
2022-05-17 09:44:38 +12:00
let queryJson = query.json
if (typeof queryJson === "string") {
queryJson = this.parseQueryParams(queryJson, "update")
}
let json = this.createObjectIds(queryJson) as {
2022-05-16 20:50:47 +12:00
filter: FilterQuery<any>
update: UpdateQuery<any>
options: object
}
2021-07-29 21:11:52 +12:00
switch (query.extra.actionTypes) {
case "updateOne": {
2022-05-14 01:48:07 +12:00
return await collection.updateOne(
json.filter,
json.update,
2022-05-16 20:50:47 +12:00
json.options as UpdateOneOptions
2022-05-14 01:48:07 +12:00
)
}
2021-07-29 21:11:52 +12:00
case "updateMany": {
2022-05-14 01:48:07 +12:00
return await collection.updateMany(
json.filter,
json.update,
2022-05-16 20:50:47 +12:00
json.options as UpdateManyOptions
2022-05-14 01:48:07 +12:00
)
}
default: {
2021-07-29 21:11:52 +12: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 21:11:52 +12: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)
2022-05-17 09:44:38 +12:00
let queryJson = query.json
if (typeof queryJson === "string") {
queryJson = this.parseQueryParams(queryJson, "delete")
}
let json = this.createObjectIds(queryJson) as {
2022-05-16 20:50:47 +12:00
filter: FilterQuery<any>
options: CommonOptions
}
if (!json.options) {
json = {
filter: json,
options: {},
}
}
2021-07-29 21:11:52 +12:00
switch (query.extra.actionTypes) {
case "deleteOne": {
2022-05-16 20:50:47 +12:00
return await collection.deleteOne(json.filter, json.options)
}
2021-07-29 21:11:52 +12:00
case "deleteMany": {
2022-05-16 20:50:47 +12:00
return await collection.deleteMany(json.filter, json.options)
}
default: {
2021-07-29 21:11:52 +12: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,
}
}