2021-06-24 19:17:26 +02:00
|
|
|
import {
|
|
|
|
Integration,
|
2022-08-11 14:50:05 +02:00
|
|
|
DatasourceFieldType,
|
|
|
|
QueryType,
|
2022-08-11 12:48:58 +02:00
|
|
|
IntegrationBase,
|
|
|
|
} from "@budibase/types"
|
2022-05-16 10:50:47 +02:00
|
|
|
import {
|
|
|
|
MongoClient,
|
2022-08-25 12:02:46 +02:00
|
|
|
ObjectId,
|
|
|
|
Filter,
|
|
|
|
UpdateFilter,
|
|
|
|
FindOneAndUpdateOptions,
|
|
|
|
UpdateOptions,
|
|
|
|
OperationOptions,
|
2022-05-16 10:50:47 +02:00
|
|
|
} from "mongodb"
|
2021-06-24 19:16:48 +02:00
|
|
|
|
2022-08-12 18:03:06 +02:00
|
|
|
interface MongoDBConfig {
|
|
|
|
connectionString: string
|
|
|
|
db: string
|
|
|
|
}
|
2021-06-24 19:16:48 +02:00
|
|
|
|
2022-08-31 16:53:32 +02:00
|
|
|
interface MongoDBQuery {
|
|
|
|
json: object | string
|
|
|
|
extra: {
|
|
|
|
[key: string]: string
|
2021-06-24 19:16:48 +02:00
|
|
|
}
|
2022-08-31 16:53:32 +02:00
|
|
|
}
|
2021-06-24 19:16:48 +02:00
|
|
|
|
2022-08-12 18:03:06 +02:00
|
|
|
const SCHEMA: Integration = {
|
|
|
|
docs: "https://github.com/mongodb/node-mongodb-native",
|
|
|
|
friendlyName: "MongoDB",
|
|
|
|
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: DatasourceFieldType.STRING,
|
|
|
|
required: true,
|
|
|
|
default: "mongodb://localhost:27017",
|
2021-06-24 19:16:48 +02:00
|
|
|
},
|
2022-08-12 18:03:06 +02:00
|
|
|
db: {
|
|
|
|
type: DatasourceFieldType.STRING,
|
|
|
|
required: true,
|
2021-06-24 19:16:48 +02:00
|
|
|
},
|
2022-08-12 18:03:06 +02:00
|
|
|
},
|
|
|
|
query: {
|
|
|
|
create: {
|
|
|
|
type: QueryType.JSON,
|
|
|
|
},
|
|
|
|
read: {
|
|
|
|
type: QueryType.JSON,
|
|
|
|
},
|
|
|
|
update: {
|
|
|
|
type: QueryType.JSON,
|
|
|
|
},
|
|
|
|
delete: {
|
|
|
|
type: QueryType.JSON,
|
|
|
|
},
|
2022-10-03 21:08:47 +02:00
|
|
|
aggregate: {
|
2022-10-04 12:03:24 +02:00
|
|
|
type: QueryType.JSON,
|
2022-10-12 16:31:53 +02:00
|
|
|
steps: [
|
|
|
|
{
|
|
|
|
key: "$addFields",
|
|
|
|
template: "{\n\t\n}",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "$bucket",
|
|
|
|
template: `{
|
|
|
|
"groupBy": "",
|
|
|
|
"boundaries": [],
|
|
|
|
"default": "",
|
|
|
|
"output": {}
|
|
|
|
}`,
|
2022-10-12 17:03:51 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "$bucketAuto",
|
|
|
|
template: `{
|
|
|
|
"groupBy": "",
|
|
|
|
"buckets": 1,
|
|
|
|
"output": {},
|
|
|
|
"granularity": "R5"
|
|
|
|
}`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "$changeStream",
|
|
|
|
template: `{
|
|
|
|
"allChangesForCluster": true,
|
|
|
|
"fullDocument": "",
|
|
|
|
"fullDocumentBeforeChange": "",
|
|
|
|
"resumeAfter": 1,
|
|
|
|
"showExpandedEvents": true,
|
|
|
|
"startAfter": {},
|
|
|
|
"startAtOperationTime": ""
|
|
|
|
}`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "$collStats",
|
|
|
|
template: `{
|
|
|
|
"latencyStats": { "histograms": true } },
|
|
|
|
"storageStats": { "scale": 1 } },
|
|
|
|
"count": {},
|
|
|
|
"queryExecStats": {}
|
|
|
|
}`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "$count",
|
|
|
|
template: ``,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "$densify",
|
|
|
|
template: `{
|
|
|
|
"field": "",
|
|
|
|
"partitionByFields": [],
|
|
|
|
"range": {
|
|
|
|
"step": 1,
|
|
|
|
"unit": 1,
|
|
|
|
"bounds": "full"
|
|
|
|
}
|
|
|
|
}`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "$documents",
|
|
|
|
template: `[]`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "$facet",
|
|
|
|
template: `{\n\t\n}`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "$fill",
|
|
|
|
template: `{
|
|
|
|
"partitionBy": "",
|
|
|
|
"partitionByFields": [],
|
|
|
|
"sortBy": {},
|
|
|
|
"output": {}
|
|
|
|
}`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "$geoNear",
|
|
|
|
template: `{
|
2022-10-12 17:20:11 +02:00
|
|
|
"near": {
|
|
|
|
"type": "Point",
|
|
|
|
"coordinates": [
|
|
|
|
-73.98142, 40.71782
|
|
|
|
]
|
|
|
|
},
|
2022-10-12 17:03:51 +02:00
|
|
|
"key": "location",
|
|
|
|
"distanceField": "dist.calculated",
|
|
|
|
"query": { "category": "Parks" }
|
2022-10-12 17:20:11 +02:00
|
|
|
}`,
|
2022-10-12 17:03:51 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "$graphLookup",
|
|
|
|
template: `{
|
|
|
|
"from": "",
|
|
|
|
"startWith": "",
|
|
|
|
"connectFromField": "",
|
|
|
|
"connectToField": "",
|
|
|
|
"as": "",
|
|
|
|
"maxDepth": 1,
|
|
|
|
"depthField": "",
|
|
|
|
"restrictSearchWithMatch": {}
|
2022-10-12 17:20:11 +02:00
|
|
|
}`,
|
2022-10-12 17:03:51 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "$group",
|
|
|
|
template: `{
|
|
|
|
"_id": ""
|
|
|
|
}`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "$indexStats",
|
|
|
|
template: "{\n\t\n}",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "$limit",
|
|
|
|
template: `1`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "$listLocalSessions",
|
|
|
|
template: `{\n\t\n}`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "$listSessions",
|
|
|
|
template: `{\n\t\n}`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "$lookup",
|
|
|
|
template: `{
|
|
|
|
"from": "",
|
|
|
|
"localField": "",
|
|
|
|
"foreignField": "",
|
|
|
|
"as": ""
|
2022-10-12 17:20:11 +02:00
|
|
|
}`,
|
2022-10-12 16:31:53 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "$match",
|
|
|
|
template: "{\n\t\n}",
|
|
|
|
},
|
2022-10-12 17:03:51 +02:00
|
|
|
{
|
|
|
|
key: "$merge",
|
|
|
|
template: `{
|
|
|
|
"into": {},
|
|
|
|
"on": "_id",
|
|
|
|
"whenMatched": "replace",
|
|
|
|
"whenNotMatched": "insert"
|
2022-10-12 17:20:11 +02:00
|
|
|
}`,
|
2022-10-12 17:03:51 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "$out",
|
|
|
|
template: `{
|
|
|
|
"db": "",
|
|
|
|
"coll": ""
|
2022-10-12 17:20:11 +02:00
|
|
|
}`,
|
2022-10-12 17:03:51 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "$planCacheStats",
|
|
|
|
template: "{\n\t\n}",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "$project",
|
|
|
|
template: "{\n\t\n}",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "$redact",
|
|
|
|
template: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "$replaceRoot",
|
|
|
|
template: `{ "newRoot": "" }`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "$replaceWith",
|
|
|
|
template: ``,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "$sample",
|
|
|
|
template: `{ "size": 3 }`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "$set",
|
|
|
|
template: "{\n\t\n}",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "$setWindowFields",
|
|
|
|
template: `{
|
|
|
|
"partitionBy": "",
|
|
|
|
"sortBy": {},
|
|
|
|
"output": {}
|
|
|
|
}`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "$skip",
|
|
|
|
template: `1`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "$sort",
|
|
|
|
template: "{\n\t\n}",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "$sortByCount",
|
|
|
|
template: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "$unionWith",
|
|
|
|
template: `{
|
|
|
|
"coll": "",
|
|
|
|
"pipeline": []
|
2022-10-12 17:20:11 +02:00
|
|
|
}`,
|
2022-10-12 17:03:51 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "$unset",
|
|
|
|
template: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "$unwind",
|
|
|
|
template: `{
|
|
|
|
"path": "",
|
|
|
|
"includeArrayIndex": "",
|
|
|
|
"preserveNullAndEmptyArrays": true
|
2022-10-12 17:20:11 +02:00
|
|
|
}`,
|
2022-10-12 17:03:51 +02:00
|
|
|
},
|
|
|
|
],
|
2022-10-03 21:08:47 +02:00
|
|
|
},
|
2022-08-12 18:03:06 +02:00
|
|
|
},
|
|
|
|
extra: {
|
|
|
|
collection: {
|
|
|
|
displayName: "Collection",
|
|
|
|
type: DatasourceFieldType.STRING,
|
|
|
|
required: true,
|
|
|
|
},
|
2022-10-04 12:03:24 +02:00
|
|
|
actionType: {
|
|
|
|
displayName: "Action Type",
|
2022-08-12 18:03:06 +02:00
|
|
|
type: DatasourceFieldType.LIST,
|
|
|
|
required: true,
|
|
|
|
data: {
|
|
|
|
read: ["find", "findOne", "findOneAndUpdate", "count", "distinct"],
|
|
|
|
create: ["insertOne", "insertMany"],
|
|
|
|
update: ["updateOne", "updateMany"],
|
|
|
|
delete: ["deleteOne", "deleteMany"],
|
2022-10-04 12:03:24 +02:00
|
|
|
aggregate: ["json", "flow"],
|
2021-07-29 11:11:52 +02:00
|
|
|
},
|
|
|
|
},
|
2022-08-12 18:03:06 +02:00
|
|
|
},
|
|
|
|
}
|
2021-06-24 19:16:48 +02:00
|
|
|
|
2022-08-12 18:03:06 +02:00
|
|
|
class MongoIntegration implements IntegrationBase {
|
|
|
|
private config: MongoDBConfig
|
|
|
|
private client: any
|
2021-06-24 19:16:48 +02:00
|
|
|
|
2022-08-12 18:03:06 +02:00
|
|
|
constructor(config: MongoDBConfig) {
|
|
|
|
this.config = config
|
|
|
|
this.client = new MongoClient(config.connectionString)
|
|
|
|
}
|
2021-06-24 19:16:48 +02:00
|
|
|
|
2022-08-12 18:03:06 +02:00
|
|
|
async connect() {
|
|
|
|
return this.client.connect()
|
|
|
|
}
|
2021-06-24 19:16:48 +02:00
|
|
|
|
2022-08-12 18:03:06 +02:00
|
|
|
createObjectIds(json: any): object {
|
|
|
|
const self = this
|
|
|
|
function interpolateObjectIds(json: any) {
|
|
|
|
for (let field of Object.keys(json)) {
|
|
|
|
if (json[field] instanceof Object) {
|
|
|
|
json[field] = self.createObjectIds(json[field])
|
|
|
|
}
|
|
|
|
if (
|
2022-08-31 16:53:32 +02:00
|
|
|
typeof json[field] === "string" &&
|
|
|
|
json[field].toLowerCase().startsWith("objectid")
|
2022-08-12 18:03:06 +02:00
|
|
|
) {
|
|
|
|
const id = json[field].match(/(?<=objectid\(['"]).*(?=['"]\))/gi)?.[0]
|
|
|
|
if (id) {
|
2022-10-03 20:10:01 +02:00
|
|
|
json[field] = ObjectId.createFromHexString(id)
|
2022-05-13 14:49:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-08-12 18:03:06 +02:00
|
|
|
return json
|
|
|
|
}
|
2022-05-13 14:49:26 +02:00
|
|
|
|
2022-08-12 18:03:06 +02:00
|
|
|
if (Array.isArray(json)) {
|
|
|
|
for (let i = 0; i < json.length; i++) {
|
|
|
|
json[i] = interpolateObjectIds(json[i])
|
2022-05-13 14:49:26 +02:00
|
|
|
}
|
2022-08-12 18:03:06 +02:00
|
|
|
return json
|
2022-05-13 14:49:26 +02:00
|
|
|
}
|
2022-08-12 18:03:06 +02:00
|
|
|
return interpolateObjectIds(json)
|
|
|
|
}
|
2022-05-13 14:49:26 +02:00
|
|
|
|
2022-08-12 18:03:06 +02:00
|
|
|
parseQueryParams(params: string, mode: string) {
|
|
|
|
let queryParams = []
|
|
|
|
let openCount = 0
|
|
|
|
let inQuotes = false
|
|
|
|
let i = 0
|
|
|
|
let startIndex = 0
|
|
|
|
for (let c of params) {
|
|
|
|
if (c === '"' && i > 0 && params[i - 1] !== "\\") {
|
|
|
|
inQuotes = !inQuotes
|
2022-08-08 22:56:55 +02:00
|
|
|
}
|
2022-08-12 18:03:06 +02:00
|
|
|
if (c === "{" && !inQuotes) {
|
|
|
|
openCount++
|
|
|
|
if (openCount === 1) {
|
|
|
|
startIndex = i
|
2022-05-16 23:44:38 +02:00
|
|
|
}
|
2022-08-12 18:03:06 +02:00
|
|
|
} else if (c === "}" && !inQuotes) {
|
|
|
|
if (openCount === 1) {
|
|
|
|
queryParams.push(JSON.parse(params.substring(startIndex, i + 1)))
|
2022-05-16 23:44:38 +02:00
|
|
|
}
|
2022-08-12 18:03:06 +02:00
|
|
|
openCount--
|
2022-05-16 23:44:38 +02:00
|
|
|
}
|
2022-08-12 18:03:06 +02:00
|
|
|
i++
|
|
|
|
}
|
|
|
|
let group1 = queryParams[0] ?? {}
|
|
|
|
let group2 = queryParams[1] ?? {}
|
|
|
|
let group3 = queryParams[2] ?? {}
|
|
|
|
if (mode === "update") {
|
2022-05-16 23:44:38 +02:00
|
|
|
return {
|
|
|
|
filter: group1,
|
2022-08-12 18:03:06 +02:00
|
|
|
update: group2,
|
|
|
|
options: group3,
|
2022-05-16 23:44:38 +02:00
|
|
|
}
|
|
|
|
}
|
2022-08-12 18:03:06 +02:00
|
|
|
return {
|
|
|
|
filter: group1,
|
|
|
|
options: group2,
|
|
|
|
}
|
|
|
|
}
|
2022-05-16 23:44:38 +02:00
|
|
|
|
2022-08-31 16:53:32 +02:00
|
|
|
async create(query: MongoDBQuery) {
|
2022-08-12 18:03:06 +02:00
|
|
|
try {
|
|
|
|
await this.connect()
|
|
|
|
const db = this.client.db(this.config.db)
|
|
|
|
const collection = db.collection(query.extra.collection)
|
|
|
|
let json = this.createObjectIds(query.json)
|
2021-07-08 14:38:49 +02:00
|
|
|
|
2022-08-12 18:03:06 +02:00
|
|
|
// For mongodb we add an extra actionType to specify
|
|
|
|
// which method we want to call on the collection
|
2022-10-04 12:03:24 +02:00
|
|
|
switch (query.extra.actionType) {
|
2022-08-12 18:03:06 +02:00
|
|
|
case "insertOne": {
|
|
|
|
return await collection.insertOne(json)
|
|
|
|
}
|
|
|
|
case "insertMany": {
|
|
|
|
return await collection.insertMany(json)
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
throw new Error(
|
2022-10-04 12:03:24 +02:00
|
|
|
`actionType ${query.extra.actionType} does not exist on DB for create`
|
2022-08-12 18:03:06 +02:00
|
|
|
)
|
2021-07-08 14:38:49 +02:00
|
|
|
}
|
2021-06-24 19:16:48 +02:00
|
|
|
}
|
2022-08-12 18:03:06 +02:00
|
|
|
} catch (err) {
|
|
|
|
console.error("Error writing to mongodb", err)
|
|
|
|
throw err
|
|
|
|
} finally {
|
|
|
|
await this.client.close()
|
2021-06-24 19:16:48 +02:00
|
|
|
}
|
2022-08-12 18:03:06 +02:00
|
|
|
}
|
2021-06-24 19:16:48 +02:00
|
|
|
|
2022-08-31 16:53:32 +02:00
|
|
|
async read(query: MongoDBQuery) {
|
2022-08-12 18:03:06 +02:00
|
|
|
try {
|
|
|
|
await this.connect()
|
|
|
|
const db = this.client.db(this.config.db)
|
|
|
|
const collection = db.collection(query.extra.collection)
|
|
|
|
let json = this.createObjectIds(query.json)
|
2021-07-08 14:38:49 +02:00
|
|
|
|
2022-10-04 12:03:24 +02:00
|
|
|
switch (query.extra.actionType) {
|
2022-08-12 18:03:06 +02:00
|
|
|
case "find": {
|
|
|
|
return await collection.find(json).toArray()
|
|
|
|
}
|
|
|
|
case "findOne": {
|
|
|
|
return await collection.findOne(json)
|
|
|
|
}
|
|
|
|
case "findOneAndUpdate": {
|
|
|
|
if (typeof query.json === "string") {
|
|
|
|
json = this.parseQueryParams(query.json, "update")
|
2021-07-08 14:38:49 +02:00
|
|
|
}
|
2022-08-12 18:03:06 +02:00
|
|
|
let findAndUpdateJson = this.createObjectIds(json) as {
|
2022-10-03 20:10:01 +02:00
|
|
|
filter: Filter<any>
|
|
|
|
update: UpdateFilter<any>
|
|
|
|
options: FindOneAndUpdateOptions
|
2021-07-08 14:38:49 +02:00
|
|
|
}
|
2022-08-12 18:03:06 +02:00
|
|
|
return await collection.findOneAndUpdate(
|
|
|
|
findAndUpdateJson.filter,
|
|
|
|
findAndUpdateJson.update,
|
|
|
|
findAndUpdateJson.options
|
|
|
|
)
|
|
|
|
}
|
|
|
|
case "count": {
|
|
|
|
return await collection.countDocuments(json)
|
|
|
|
}
|
|
|
|
case "distinct": {
|
|
|
|
return await collection.distinct(json)
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
throw new Error(
|
2022-10-04 12:03:24 +02:00
|
|
|
`actionType ${query.extra.actionType} does not exist on DB for read`
|
2022-08-12 18:03:06 +02:00
|
|
|
)
|
2021-07-08 14:38:49 +02:00
|
|
|
}
|
2021-06-24 19:16:48 +02:00
|
|
|
}
|
2022-08-12 18:03:06 +02:00
|
|
|
} catch (err) {
|
|
|
|
console.error("Error querying mongodb", err)
|
|
|
|
throw err
|
|
|
|
} finally {
|
|
|
|
await this.client.close()
|
2021-06-24 19:16:48 +02:00
|
|
|
}
|
2022-08-12 18:03:06 +02:00
|
|
|
}
|
2021-07-08 14:38:49 +02:00
|
|
|
|
2022-08-31 16:53:32 +02:00
|
|
|
async update(query: MongoDBQuery) {
|
2022-08-12 18:03:06 +02:00
|
|
|
try {
|
|
|
|
await this.connect()
|
|
|
|
const db = this.client.db(this.config.db)
|
|
|
|
const collection = db.collection(query.extra.collection)
|
|
|
|
let queryJson = query.json
|
|
|
|
if (typeof queryJson === "string") {
|
|
|
|
queryJson = this.parseQueryParams(queryJson, "update")
|
|
|
|
}
|
|
|
|
let json = this.createObjectIds(queryJson) as {
|
2022-10-03 20:10:01 +02:00
|
|
|
filter: Filter<any>
|
|
|
|
update: UpdateFilter<any>
|
2022-08-12 18:03:06 +02:00
|
|
|
options: object
|
|
|
|
}
|
2021-07-08 14:38:49 +02:00
|
|
|
|
2022-10-04 12:03:24 +02:00
|
|
|
switch (query.extra.actionType) {
|
2022-08-12 18:03:06 +02:00
|
|
|
case "updateOne": {
|
|
|
|
return await collection.updateOne(
|
|
|
|
json.filter,
|
|
|
|
json.update,
|
2022-10-03 20:10:01 +02:00
|
|
|
json.options as UpdateOptions
|
2022-08-12 18:03:06 +02:00
|
|
|
)
|
2022-05-16 23:44:38 +02:00
|
|
|
}
|
2022-08-12 18:03:06 +02:00
|
|
|
case "updateMany": {
|
|
|
|
return await collection.updateMany(
|
|
|
|
json.filter,
|
|
|
|
json.update,
|
2022-10-03 20:10:01 +02:00
|
|
|
json.options as UpdateOptions
|
2022-08-12 18:03:06 +02:00
|
|
|
)
|
2022-05-16 10:50:47 +02:00
|
|
|
}
|
2022-08-12 18:03:06 +02:00
|
|
|
default: {
|
|
|
|
throw new Error(
|
2022-10-04 12:03:24 +02:00
|
|
|
`actionType ${query.extra.actionType} does not exist on DB for update`
|
2022-08-12 18:03:06 +02:00
|
|
|
)
|
2021-07-08 14:38:49 +02:00
|
|
|
}
|
|
|
|
}
|
2022-08-12 18:03:06 +02:00
|
|
|
} catch (err) {
|
|
|
|
console.error("Error writing to mongodb", err)
|
|
|
|
throw err
|
|
|
|
} finally {
|
|
|
|
await this.client.close()
|
2021-07-08 14:38:49 +02:00
|
|
|
}
|
2022-08-12 18:03:06 +02:00
|
|
|
}
|
2021-07-08 14:38:49 +02:00
|
|
|
|
2022-08-31 16:53:32 +02:00
|
|
|
async delete(query: MongoDBQuery) {
|
2022-08-12 18:03:06 +02:00
|
|
|
try {
|
|
|
|
await this.connect()
|
|
|
|
const db = this.client.db(this.config.db)
|
|
|
|
const collection = db.collection(query.extra.collection)
|
|
|
|
let queryJson = query.json
|
|
|
|
if (typeof queryJson === "string") {
|
|
|
|
queryJson = this.parseQueryParams(queryJson, "delete")
|
|
|
|
}
|
|
|
|
let json = this.createObjectIds(queryJson) as {
|
2022-10-03 20:10:01 +02:00
|
|
|
filter: Filter<any>
|
|
|
|
options: OperationOptions
|
2022-08-12 18:03:06 +02:00
|
|
|
}
|
|
|
|
if (!json.options) {
|
|
|
|
json = {
|
|
|
|
filter: json,
|
|
|
|
options: {},
|
2022-05-16 23:44:38 +02:00
|
|
|
}
|
2022-08-12 18:03:06 +02:00
|
|
|
}
|
|
|
|
|
2022-10-04 12:03:24 +02:00
|
|
|
switch (query.extra.actionType) {
|
2022-08-12 18:03:06 +02:00
|
|
|
case "deleteOne": {
|
|
|
|
return await collection.deleteOne(json.filter, json.options)
|
2022-05-16 10:50:47 +02:00
|
|
|
}
|
2022-08-12 18:03:06 +02:00
|
|
|
case "deleteMany": {
|
|
|
|
return await collection.deleteMany(json.filter, json.options)
|
2022-06-17 17:54:23 +02:00
|
|
|
}
|
2022-08-12 18:03:06 +02:00
|
|
|
default: {
|
|
|
|
throw new Error(
|
2022-10-04 12:03:24 +02:00
|
|
|
`actionType ${query.extra.actionType} does not exist on DB for delete`
|
2022-08-12 18:03:06 +02:00
|
|
|
)
|
2021-07-08 14:38:49 +02:00
|
|
|
}
|
|
|
|
}
|
2022-08-12 18:03:06 +02:00
|
|
|
} catch (err) {
|
|
|
|
console.error("Error writing to mongodb", err)
|
|
|
|
throw err
|
|
|
|
} finally {
|
|
|
|
await this.client.close()
|
2021-07-08 14:38:49 +02:00
|
|
|
}
|
2021-06-24 19:16:48 +02:00
|
|
|
}
|
2022-08-25 18:30:47 +02:00
|
|
|
|
2022-10-04 12:03:54 +02:00
|
|
|
async aggregate(query: {
|
|
|
|
json: object
|
|
|
|
steps: any[]
|
|
|
|
extra: { [key: string]: string }
|
|
|
|
}) {
|
2022-10-03 18:18:16 +02:00
|
|
|
try {
|
|
|
|
await this.connect()
|
|
|
|
const db = this.client.db(this.config.db)
|
|
|
|
const collection = db.collection(query.extra.collection)
|
2022-10-03 21:08:47 +02:00
|
|
|
let response = []
|
2022-10-04 12:03:24 +02:00
|
|
|
if (query.extra?.actionType === "flow") {
|
|
|
|
for await (const doc of collection.aggregate(
|
|
|
|
query.steps.map(({ key, value }) => {
|
|
|
|
let temp: any = {}
|
|
|
|
temp[key] = JSON.parse(value.value)
|
2022-10-12 19:54:53 +02:00
|
|
|
return this.createObjectIds(temp)
|
2022-10-04 12:03:24 +02:00
|
|
|
})
|
|
|
|
)) {
|
|
|
|
response.push(doc)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
const stages: Array<any> = query.json as Array<any>
|
2022-10-12 19:54:53 +02:00
|
|
|
for await (const doc of collection.aggregate(stages ? this.createObjectIds(stages) : [])) {
|
2022-10-04 12:03:24 +02:00
|
|
|
response.push(doc)
|
|
|
|
}
|
2022-08-25 18:30:47 +02:00
|
|
|
}
|
2022-10-03 18:18:16 +02:00
|
|
|
return response
|
|
|
|
} catch (err) {
|
|
|
|
console.error("Error writing to mongodb", err)
|
|
|
|
throw err
|
|
|
|
} finally {
|
|
|
|
await this.client.close()
|
2022-08-25 18:30:47 +02:00
|
|
|
}
|
2022-10-03 18:18:16 +02:00
|
|
|
}
|
2022-08-12 18:03:06 +02:00
|
|
|
}
|
2021-06-24 19:16:48 +02:00
|
|
|
|
2022-08-12 18:03:06 +02:00
|
|
|
export default {
|
|
|
|
schema: SCHEMA,
|
|
|
|
integration: MongoIntegration,
|
2021-06-24 19:16:48 +02:00
|
|
|
}
|