47 lines
1.4 KiB
JavaScript
47 lines
1.4 KiB
JavaScript
// Journal append.
|
|
|
|
const db = require("@saltcorn/data/db");
|
|
|
|
const { getEnv } = require("./env");
|
|
const { randomUuid } = require("./ids");
|
|
const { OP_SCHEMA_VERSION } = require("./constants");
|
|
const { currentParentOpId, currentCorrelationId } = require("./context");
|
|
|
|
|
|
const recordOp = async (rec) => {
|
|
const env = await getEnv();
|
|
const now = new Date().toISOString();
|
|
const row = {
|
|
op_id: rec.op_id || randomUuid(),
|
|
source_env_id: env.env_id,
|
|
op_type: rec.op_type,
|
|
entity_kind: rec.entity_kind || null,
|
|
entity_uuid: rec.entity_uuid || null,
|
|
payload: JSON.stringify(rec.payload || {}),
|
|
parent_op_id: rec.parent_op_id || currentParentOpId(),
|
|
correlation_id: rec.correlation_id || currentCorrelationId(),
|
|
schema_version: OP_SCHEMA_VERSION,
|
|
created_at: now,
|
|
applied_at: now,
|
|
status: "committed"
|
|
};
|
|
await db.insert("_dd_ops", row, { noid: true });
|
|
return row.op_id;
|
|
};
|
|
|
|
|
|
const recordOpSafely = async (rec) => {
|
|
try {
|
|
return await recordOp(rec);
|
|
} catch (err) {
|
|
// eslint-disable-next-line no-console
|
|
console.error(`[dev-deploy] failed to record op ${rec.op_type}:`, err);
|
|
return null;
|
|
}
|
|
};
|
|
|
|
|
|
module.exports = {
|
|
recordOp,
|
|
recordOpSafely
|
|
};
|