mirror of https://github.com/Budibase/budibase.git
nocodelowcodelow-codedockerdocker-composeinternal-projectinternal-toolinternal-toolslow-code-developmentlow-code-development-platformopensourceselfhostedweb-devweb-developmentweb-development-toolswebdevwebdevelopmentworkflow-automationautomationdeveloper-tools
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
78 lines
1.8 KiB
78 lines
1.8 KiB
const { MongoClient } = require("mongodb")
|
|
const { FIELD_TYPES, QUERY_TYPES } = require("./Integration")
|
|
|
|
const SCHEMA = {
|
|
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: FIELD_TYPES.STRING,
|
|
required: true,
|
|
default: "mongodb://localhost:27017",
|
|
},
|
|
db: {
|
|
type: FIELD_TYPES.STRING,
|
|
required: true,
|
|
},
|
|
collection: {
|
|
type: FIELD_TYPES.STRING,
|
|
required: true,
|
|
},
|
|
},
|
|
query: {
|
|
create: {
|
|
type: QUERY_TYPES.JSON,
|
|
},
|
|
read: {
|
|
type: QUERY_TYPES.JSON,
|
|
},
|
|
},
|
|
}
|
|
|
|
class MongoIntegration {
|
|
constructor(config) {
|
|
this.config = config
|
|
this.client = new MongoClient(config.connectionString)
|
|
}
|
|
|
|
async connect() {
|
|
return this.client.connect()
|
|
}
|
|
|
|
async create(query) {
|
|
try {
|
|
await this.connect()
|
|
const db = this.client.db(this.config.db)
|
|
const collection = db.collection(this.config.collection)
|
|
const result = await collection.insertOne(query.json)
|
|
return result
|
|
} catch (err) {
|
|
console.error("Error writing to mongodb", err)
|
|
throw err
|
|
} finally {
|
|
await this.client.close()
|
|
}
|
|
}
|
|
|
|
async read(query) {
|
|
try {
|
|
await this.connect()
|
|
const db = this.client.db(this.config.db)
|
|
const collection = db.collection(this.config.collection)
|
|
const result = await collection.find(query.json).toArray()
|
|
return result
|
|
} catch (err) {
|
|
console.error("Error querying mongodb", err)
|
|
throw err
|
|
} finally {
|
|
await this.client.close()
|
|
}
|
|
}
|
|
}
|
|
|
|
module.exports = {
|
|
schema: SCHEMA,
|
|
integration: MongoIntegration,
|
|
}
|
|
|