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.6 KiB
78 lines
1.6 KiB
const { MongoClient } = require("mongodb")
|
|
const { FIELD_TYPES, QUERY_TYPES } = require("./Integration")
|
|
|
|
const SCHEMA = {
|
|
datasource: {
|
|
connectionString: {
|
|
type: FIELD_TYPES.STRING,
|
|
required: true,
|
|
default: "localhost",
|
|
},
|
|
db: {
|
|
type: FIELD_TYPES.STRING,
|
|
required: true,
|
|
},
|
|
collection: {
|
|
type: FIELD_TYPES.STRING,
|
|
required: true,
|
|
},
|
|
},
|
|
query: {
|
|
create: {
|
|
JSON: {
|
|
type: QUERY_TYPES.JSON,
|
|
},
|
|
},
|
|
read: {
|
|
JSON: {
|
|
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,
|
|
}
|
|
|