2020-11-26 17:46:36 +01:00
|
|
|
const { MongoClient } = require("mongodb")
|
|
|
|
|
2021-01-11 18:18:22 +01:00
|
|
|
const SCHEMA = {
|
|
|
|
datasource: {
|
|
|
|
connectionString: {
|
|
|
|
type: "string",
|
|
|
|
required: true,
|
|
|
|
default: "localhost",
|
|
|
|
},
|
|
|
|
db: {
|
|
|
|
type: "string",
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
collection: {
|
|
|
|
type: "string",
|
|
|
|
required: true,
|
|
|
|
},
|
2020-11-26 17:46:36 +01:00
|
|
|
},
|
|
|
|
query: {
|
2021-01-11 18:18:22 +01:00
|
|
|
json: {
|
|
|
|
type: "json",
|
|
|
|
},
|
2020-11-26 17:46:36 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
class MongoIntegration {
|
|
|
|
constructor(config) {
|
|
|
|
this.config = config
|
|
|
|
this.client = new MongoClient(config.connectionString)
|
2020-11-26 22:23:20 +01:00
|
|
|
try {
|
|
|
|
this.config.query = JSON.parse(this.config.query)
|
|
|
|
} catch (err) {
|
|
|
|
this.config.query = {}
|
|
|
|
}
|
2020-11-26 17:46:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async connect() {
|
|
|
|
return this.client.connect()
|
|
|
|
}
|
|
|
|
|
|
|
|
async 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(this.config.query).toArray()
|
|
|
|
return result
|
2020-11-26 22:23:20 +01:00
|
|
|
} catch (err) {
|
|
|
|
console.error("Error querying mongodb", err)
|
|
|
|
throw err
|
2020-11-26 17:46:36 +01:00
|
|
|
} finally {
|
|
|
|
await this.client.close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
2021-01-11 18:18:22 +01:00
|
|
|
schema: SCHEMA,
|
2020-11-26 17:46:36 +01:00
|
|
|
integration: MongoIntegration,
|
|
|
|
}
|