1
0
Fork 0
mirror of synced 2024-09-21 20:01:32 +12:00
budibase/packages/server/src/events/index.js

34 lines
946 B
JavaScript
Raw Normal View History

const EventEmitter = require("events").EventEmitter
2020-06-01 21:41:28 +12:00
const CouchDB = require("../db")
2020-06-02 08:26:32 +12:00
const { Orchestrator, serverStrategy } = require("./workflow")
2020-05-25 09:54:08 +12:00
2020-06-01 04:12:52 +12:00
const emitter = new EventEmitter()
2020-06-02 03:22:13 +12:00
async function executeRelevantWorkflows(event, eventType) {
const db = new CouchDB(event.instanceId)
2020-06-01 04:12:52 +12:00
const workflowsToTrigger = await db.query("database/by_workflow_trigger", {
2020-06-02 03:22:13 +12:00
key: [eventType],
2020-06-02 08:26:32 +12:00
include_docs: true,
2020-06-01 04:12:52 +12:00
})
2020-06-02 03:22:13 +12:00
const workflows = workflowsToTrigger.rows.map(wf => wf.doc)
2020-06-01 04:12:52 +12:00
2020-06-02 03:22:13 +12:00
// Create orchestrator
const workflowOrchestrator = new Orchestrator()
workflowOrchestrator.strategy = serverStrategy
2020-06-01 04:12:52 +12:00
2020-06-02 03:22:13 +12:00
for (let workflow of workflows) {
workflowOrchestrator.execute(workflow, event)
2020-06-01 04:12:52 +12:00
}
2020-06-02 03:22:13 +12:00
}
2020-06-05 00:55:52 +12:00
emitter.on("record:save", async function(event) {
2020-06-02 08:26:32 +12:00
await executeRelevantWorkflows(event, "record:save")
2020-06-01 04:12:52 +12:00
})
2020-06-01 21:41:28 +12:00
emitter.on("record:delete", async function(event) {
2020-06-02 08:26:32 +12:00
await executeRelevantWorkflows(event, "record:delete")
2020-06-01 04:12:52 +12:00
})
module.exports = emitter