budibase/packages/server/src/events/index.js

38 lines
888 B
JavaScript
Raw Normal View History

const EventEmitter = require("events").EventEmitter
2020-06-01 11:41:28 +02:00
const CouchDB = require("../db")
2020-05-24 23:54:08 +02:00
2020-05-31 18:12:52 +02:00
const emitter = new EventEmitter()
2020-06-01 11:41:28 +02:00
async function determineWorkflowsToTrigger(instanceId, event) {
const db = new CouchDB(instanceId)
2020-05-31 18:12:52 +02:00
const workflowsToTrigger = await db.query("database/by_workflow_trigger", {
2020-06-01 11:41:28 +02:00
key: [event],
2020-05-31 18:12:52 +02:00
})
2020-06-01 11:41:28 +02:00
return workflowsToTrigger.rows
2020-05-31 18:12:52 +02:00
}
emitter.on("record:save", async function(event) {
2020-06-01 11:41:28 +02:00
const workflowsToTrigger = await determineWorkflowsToTrigger(
instanceId,
"record:save"
)
2020-05-31 18:12:52 +02:00
for (let workflow of workflowsToTrigger) {
2020-06-01 12:52:15 +02:00
// TODO: server side workflow triggers
2020-05-31 18:12:52 +02:00
}
})
2020-06-01 11:41:28 +02:00
emitter.on("record:delete", async function(event) {
const workflowsToTrigger = await determineWorkflowsToTrigger(
instanceId,
"record:delete"
)
2020-05-31 18:12:52 +02:00
for (let workflow of workflowsToTrigger) {
2020-06-01 12:52:15 +02:00
// TODO: server side workflow triggers
2020-05-31 18:12:52 +02:00
}
})
module.exports = emitter