pg-trx-outbox
Version:
Transactional outbox of Postgres for Node.js with little Event Sourcing
70 lines (69 loc) • 2.36 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PgTrxOutbox = void 0;
const notifier_ts_1 = require("./notifier.js");
const poller_ts_1 = require("./poller.js");
const transfer_ts_1 = require("./transfer.js");
const fsm_ts_1 = require("./fsm.js");
const ts_pattern_1 = require("ts-pattern");
const pg_ts_1 = require("./pg.js");
const ts_fp_di_1 = require("ts-fp-di");
const es_ts_1 = require("./es.js");
class PgTrxOutbox {
pg;
transfer;
adapter;
poller;
notifier;
es;
fsm;
constructor(options) {
const opts = {
...options,
outboxOptions: {
onError(err) {
console.error(`Error happens on pg-trx-outbox: ${err.stack ?? err.message ?? err}`);
},
...options.outboxOptions,
},
};
this.adapter = opts.adapter;
this.pg = new pg_ts_1.Pg(opts);
this.es = new es_ts_1.Es(this.pg, this.adapter, opts);
this.transfer = new transfer_ts_1.Transfer(opts, this.pg, this.adapter, this.es);
this.fsm = new fsm_ts_1.FSM(opts, this.transfer);
(0, ts_pattern_1.match)(opts.outboxOptions?.mode)
.with(ts_pattern_1.P.union('short-polling', void 0), () => (this.poller = new poller_ts_1.Poller(opts, this.fsm)))
.with('notify', () => {
this.poller = new poller_ts_1.Poller(opts, this.fsm);
this.notifier = new notifier_ts_1.Notifier(opts, this.fsm);
})
.exhaustive();
}
async start() {
await this.transfer.start();
await this.adapter.start();
await this.pg.start();
await this.es.start();
await this.poller?.start();
await this.notifier?.start();
}
async stop() {
await this.es.stop();
await this.notifier?.stop();
await this.poller?.stop();
await this.transfer.stop();
await this.pg.stop();
await this.adapter.stop();
}
contextId() {
return (0, ts_fp_di_1.diExists)() && (0, ts_fp_di_1.diHas)('pg_trx_outbox_context_id') ? (0, ts_fp_di_1.diDep)('pg_trx_outbox_context_id') : null;
}
getLastEventId() {
return this.es.getLastEventId();
}
fetchEvents() {
this.fsm.send('manual');
}
}
exports.PgTrxOutbox = PgTrxOutbox;