UNPKG

pg-trx-outbox

Version:

Transactional outbox of Postgres for Node.js with little Event Sourcing

54 lines (53 loc) 1.47 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Es = void 0; const pg_cursor_1 = __importDefault(require("pg-cursor")); class Es { lastEventId = '0'; pg; adapter; options; constructor(pg, adapter, options) { this.pg = pg; this.adapter = adapter; this.options = options; } async start() { await this.initSync(); } async stop() { } setLastEventId(index) { this.lastEventId = index; } getLastEventId() { return this.lastEventId; } async initSync() { const client = await this.pg.getClient(); const cursor = client.query(new pg_cursor_1.default(` select id, topic, key, value, context_id from pg_trx_outbox where is_event and id > $1 order by id `, [this.getLastEventId()])); while (true) { const messages = await cursor.read(this.options.eventSourcingOptions?.initSyncBatchSize ?? 100); if (!messages.length) { break; } await this.adapter.send(messages); this.setLastEventId(messages.at(-1).id); } await cursor.close(); client.release(); } } exports.Es = Es;