@dudousxd/nestjs-durable-transport-db 0.1.1 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Davide Carvalho
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/dist/index.cjs ADDED
@@ -0,0 +1,323 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
20
+
21
+ // src/index.ts
22
+ var index_exports = {};
23
+ __export(index_exports, {
24
+ DbTransport: () => DbTransport,
25
+ mikroOrmExecutor: () => mikroOrmExecutor,
26
+ typeOrmExecutor: () => typeOrmExecutor
27
+ });
28
+ module.exports = __toCommonJS(index_exports);
29
+
30
+ // src/db-transport.ts
31
+ var import_nestjs_durable_core = require("@dudousxd/nestjs-durable-core");
32
+
33
+ // src/executors.ts
34
+ function typeOrmExecutor(ds) {
35
+ return {
36
+ dialect: /postgres/.test(String(ds.options.type)) ? "postgres" : "mysql",
37
+ escapeId: /* @__PURE__ */ __name((id) => ds.driver.escape(id), "escapeId"),
38
+ query: /* @__PURE__ */ __name((sql, params) => ds.query(sql, params), "query"),
39
+ transaction: /* @__PURE__ */ __name(async (fn) => {
40
+ const qr = ds.createQueryRunner();
41
+ await qr.connect();
42
+ await qr.startTransaction();
43
+ try {
44
+ const result = await fn({
45
+ query: /* @__PURE__ */ __name((sql, params) => qr.query(sql, params), "query")
46
+ });
47
+ await qr.commitTransaction();
48
+ return result;
49
+ } catch (err) {
50
+ await qr.rollbackTransaction();
51
+ throw err;
52
+ } finally {
53
+ await qr.release();
54
+ }
55
+ }, "transaction")
56
+ };
57
+ }
58
+ __name(typeOrmExecutor, "typeOrmExecutor");
59
+ function mikroOrmExecutor(em) {
60
+ const platform = em.getPlatform();
61
+ return {
62
+ dialect: /postgre/i.test(platform.constructor.name) ? "postgres" : "mysql",
63
+ escapeId: /* @__PURE__ */ __name((id) => platform.quoteIdentifier(id), "escapeId"),
64
+ query: /* @__PURE__ */ __name((sql, params) => em.getConnection().execute(sql, params ?? [], "all"), "query"),
65
+ transaction: /* @__PURE__ */ __name((fn) => em.transactional((txEm) => fn({
66
+ query: /* @__PURE__ */ __name((sql, params) => txEm.getConnection().execute(sql, params ?? [], "all", txEm.getTransactionContext?.()), "query")
67
+ })), "transaction")
68
+ };
69
+ }
70
+ __name(mikroOrmExecutor, "mikroOrmExecutor");
71
+
72
+ // src/db-transport.ts
73
+ var DbTransport = class {
74
+ static {
75
+ __name(this, "DbTransport");
76
+ }
77
+ exec;
78
+ group;
79
+ tasksTable;
80
+ resultsTable;
81
+ pollMs;
82
+ leaseMs;
83
+ batchSize;
84
+ autoCreate;
85
+ instanceId;
86
+ isPg;
87
+ handlers = /* @__PURE__ */ new Map();
88
+ running = true;
89
+ schemaReady;
90
+ taskLoop;
91
+ resultLoop;
92
+ constructor(options) {
93
+ this.exec = options.executor;
94
+ this.group = options.group;
95
+ const prefix = options.prefix ?? "durable";
96
+ this.tasksTable = `${prefix}_transport_tasks`;
97
+ this.resultsTable = `${prefix}_transport_results`;
98
+ this.pollMs = options.pollMs ?? 500;
99
+ this.leaseMs = options.leaseMs ?? 3e4;
100
+ this.batchSize = options.batchSize ?? 10;
101
+ this.autoCreate = options.autoCreate ?? true;
102
+ this.instanceId = options.instanceId ?? `db-${Math.floor(Date.now()).toString(36)}`;
103
+ this.isPg = this.exec.dialect === "postgres";
104
+ }
105
+ // ── dialect helpers ──────────────────────────────────────────────────────────────────────────
106
+ /** Positional placeholder for param `i` (1-based): `$i` on Postgres, `?` elsewhere. */
107
+ ph(i) {
108
+ return this.isPg ? `$${i}` : "?";
109
+ }
110
+ q(id) {
111
+ return this.exec.escapeId(id);
112
+ }
113
+ now() {
114
+ return Math.floor(Date.now());
115
+ }
116
+ // ── schema ───────────────────────────────────────────────────────────────────────────────────
117
+ ensureSchema() {
118
+ if (!this.schemaReady) this.schemaReady = this.createTables();
119
+ return this.schemaReady;
120
+ }
121
+ async createTables() {
122
+ if (!this.autoCreate) return;
123
+ const str = "varchar(191)";
124
+ const txt = this.isPg ? "text" : "longtext";
125
+ const tasks = this.q(this.tasksTable);
126
+ const results = this.q(this.resultsTable);
127
+ await this.exec.query(`CREATE TABLE IF NOT EXISTS ${tasks} (
128
+ ${this.q("step_id")} ${str} PRIMARY KEY,
129
+ ${this.q("run_id")} ${str} NOT NULL,
130
+ ${this.q("seq")} integer NOT NULL,
131
+ ${this.q("name")} ${str} NOT NULL,
132
+ ${this.q("grp")} ${str} NOT NULL,
133
+ ${this.q("input")} ${txt},
134
+ ${this.q("attempt")} integer NOT NULL,
135
+ ${this.q("status")} varchar(32) NOT NULL,
136
+ ${this.q("claimed_by")} ${str},
137
+ ${this.q("claimed_at")} bigint,
138
+ ${this.q("created_at")} bigint NOT NULL
139
+ )`);
140
+ await this.exec.query(`CREATE TABLE IF NOT EXISTS ${results} (
141
+ ${this.q("step_id")} ${str} PRIMARY KEY,
142
+ ${this.q("run_id")} ${str} NOT NULL,
143
+ ${this.q("seq")} integer NOT NULL,
144
+ ${this.q("status")} varchar(32) NOT NULL,
145
+ ${this.q("output")} ${txt},
146
+ ${this.q("error")} ${txt},
147
+ ${this.q("started_at")} bigint,
148
+ ${this.q("claimed_by")} ${str},
149
+ ${this.q("claimed_at")} bigint,
150
+ ${this.q("created_at")} bigint NOT NULL
151
+ )`);
152
+ await this.exec.query(`CREATE INDEX ${this.q(`${this.tasksTable}_grp_idx`)} ON ${tasks} (${this.q("grp")}, ${this.q("status")}, ${this.q("created_at")})`).catch(() => {
153
+ });
154
+ }
155
+ // ── engine → worker ────────────────────────────────────────────────────────────────────────
156
+ async dispatch(task) {
157
+ await this.ensureSchema();
158
+ const t = this.q(this.tasksTable);
159
+ const ignore = this.isPg ? `INSERT INTO ${t} (...) ON CONFLICT (${this.q("step_id")}) DO NOTHING` : `INSERT IGNORE INTO ${t} (...)`;
160
+ const cols = [
161
+ "step_id",
162
+ "run_id",
163
+ "seq",
164
+ "name",
165
+ "grp",
166
+ "input",
167
+ "attempt",
168
+ "status",
169
+ "created_at"
170
+ ];
171
+ const sql = ignore.replace("(...)", `(${cols.map((c) => this.q(c)).join(", ")}) VALUES (${cols.map((_, i) => this.ph(i + 1)).join(", ")})`);
172
+ await this.exec.query(sql, [
173
+ task.stepId,
174
+ task.runId,
175
+ task.seq,
176
+ task.name,
177
+ task.group,
178
+ JSON.stringify(task.input ?? null),
179
+ task.attempt,
180
+ "pending",
181
+ this.now()
182
+ ]);
183
+ }
184
+ // ── worker → engine ────────────────────────────────────────────────────────────────────────
185
+ handle(name, fn) {
186
+ if (!this.group) throw new Error("DbTransport needs a `group` to register handlers");
187
+ this.handlers.set(name, fn);
188
+ if (!this.taskLoop) this.taskLoop = this.loop(() => this.drainTasks(this.group));
189
+ }
190
+ onResult(handler) {
191
+ if (!this.resultLoop) this.resultLoop = this.loop(() => this.drainResults(handler));
192
+ }
193
+ onHeartbeat(_handler) {
194
+ }
195
+ // ── polling ──────────────────────────────────────────────────────────────────────────────────
196
+ /** Run `tick` repeatedly; sleep `pollMs` when it reports an empty queue. Stops on `close()`. */
197
+ async loop(tick) {
198
+ await this.ensureSchema();
199
+ while (this.running) {
200
+ let processed = 0;
201
+ try {
202
+ processed = await tick();
203
+ } catch (err) {
204
+ if (this.running) console.error("[DbTransport] poll failed", err);
205
+ }
206
+ if (processed === 0) await this.sleep(this.pollMs);
207
+ }
208
+ }
209
+ /** Claim a batch of tasks (FOR UPDATE SKIP LOCKED), then run each outside the lock. */
210
+ async drainTasks(group) {
211
+ const claimed = await this.claim(this.tasksTable, `${this.q("grp")} = ${this.ph(1)}`, [
212
+ group
213
+ ]);
214
+ for (const row of claimed) {
215
+ const task = {
216
+ runId: row.run_id,
217
+ seq: Number(row.seq),
218
+ stepId: row.step_id,
219
+ name: row.name,
220
+ group,
221
+ input: row.input == null ? void 0 : JSON.parse(row.input),
222
+ attempt: Number(row.attempt)
223
+ };
224
+ const result = await (0, import_nestjs_durable_core.runStepHandler)(task, this.handlers.get(task.name));
225
+ await this.insertResult(result);
226
+ await this.exec.query(`DELETE FROM ${this.q(this.tasksTable)} WHERE ${this.q("step_id")} = ${this.ph(1)}`, [
227
+ task.stepId
228
+ ]);
229
+ }
230
+ return claimed.length;
231
+ }
232
+ /** Claim a batch of results, deliver each to the engine, then delete it. */
233
+ async drainResults(handler) {
234
+ const claimed = await this.claim(this.resultsTable, "1 = 1", []);
235
+ for (const row of claimed) {
236
+ const result = {
237
+ runId: row.run_id,
238
+ seq: Number(row.seq),
239
+ stepId: row.step_id,
240
+ status: row.status,
241
+ output: row.output == null ? void 0 : JSON.parse(row.output),
242
+ error: row.error == null ? void 0 : JSON.parse(row.error),
243
+ startedAt: row.started_at == null ? void 0 : Number(row.started_at)
244
+ };
245
+ await handler(result);
246
+ await this.exec.query(`DELETE FROM ${this.q(this.resultsTable)} WHERE ${this.q("step_id")} = ${this.ph(1)}`, [
247
+ result.stepId
248
+ ]);
249
+ }
250
+ return claimed.length;
251
+ }
252
+ /**
253
+ * Atomically claim up to `batchSize` un-leased rows matching `where` and stamp them with this
254
+ * instance's lease. `SELECT … FOR UPDATE SKIP LOCKED` inside the txn means concurrent instances
255
+ * skip each other's locked rows instead of blocking, so no row is claimed twice.
256
+ */
257
+ async claim(table, where, whereParams) {
258
+ const t = this.q(table);
259
+ const staleBefore = this.now() - this.leaseMs;
260
+ return this.exec.transaction(async (tx) => {
261
+ const leaseClause = `(${this.q("claimed_at")} IS NULL OR ${this.q("claimed_at")} < ${this.ph(whereParams.length + 1)})`;
262
+ const rows = await tx.query(`SELECT * FROM ${t} WHERE ${where} AND ${leaseClause}
263
+ ORDER BY ${this.q("created_at")} ASC LIMIT ${this.batchSize} FOR UPDATE SKIP LOCKED`, [
264
+ ...whereParams,
265
+ staleBefore
266
+ ]);
267
+ if (rows.length > 0) {
268
+ const ids = rows.map((r) => r.step_id);
269
+ const placeholders = ids.map((_, i) => this.ph(i + 3)).join(", ");
270
+ await tx.query(`UPDATE ${t} SET ${this.q("claimed_by")} = ${this.ph(1)}, ${this.q("claimed_at")} = ${this.ph(2)}
271
+ WHERE ${this.q("step_id")} IN (${placeholders})`, [
272
+ this.instanceId,
273
+ this.now(),
274
+ ...ids
275
+ ]);
276
+ }
277
+ return rows;
278
+ });
279
+ }
280
+ async insertResult(result) {
281
+ const t = this.q(this.resultsTable);
282
+ const cols = [
283
+ "step_id",
284
+ "run_id",
285
+ "seq",
286
+ "status",
287
+ "output",
288
+ "error",
289
+ "started_at",
290
+ "created_at"
291
+ ];
292
+ const head = this.isPg ? `INSERT INTO ${t} (...) ON CONFLICT (${this.q("step_id")}) DO NOTHING` : `INSERT IGNORE INTO ${t} (...)`;
293
+ const sql = head.replace("(...)", `(${cols.map((c) => this.q(c)).join(", ")}) VALUES (${cols.map((_, i) => this.ph(i + 1)).join(", ")})`);
294
+ await this.exec.query(sql, [
295
+ result.stepId,
296
+ result.runId,
297
+ result.seq,
298
+ result.status,
299
+ result.output === void 0 ? null : JSON.stringify(result.output),
300
+ result.error === void 0 ? null : JSON.stringify(result.error),
301
+ result.startedAt ?? null,
302
+ this.now()
303
+ ]);
304
+ }
305
+ sleep(ms) {
306
+ return new Promise((r) => setTimeout(r, ms));
307
+ }
308
+ /** Stop the pollers and wait for the current ticks to settle. Does not close the shared DataSource. */
309
+ async close() {
310
+ this.running = false;
311
+ await Promise.allSettled([
312
+ this.taskLoop,
313
+ this.resultLoop
314
+ ]);
315
+ }
316
+ };
317
+ // Annotate the CommonJS export names for ESM import in node:
318
+ 0 && (module.exports = {
319
+ DbTransport,
320
+ mikroOrmExecutor,
321
+ typeOrmExecutor
322
+ });
323
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts","../src/db-transport.ts","../src/executors.ts"],"sourcesContent":["export * from './db-transport';\n","import {\n type Heartbeat,\n type RemoteTask,\n type StepHandler,\n type StepResult,\n type Transport,\n runStepHandler,\n} from '@dudousxd/nestjs-durable-core';\nimport type { SqlExecutor } from './executors';\n\nexport { mikroOrmExecutor, type SqlExecutor, type SqlTx, typeOrmExecutor } from './executors';\n\n/** Raw row shapes returned by `SELECT *` (declared fields, so they're not `| undefined` under noUncheckedIndexedAccess). */\ninterface TaskRow {\n step_id: string;\n run_id: string;\n seq: string | number;\n name: string;\n input: string | null;\n attempt: string | number;\n}\ninterface ResultRow {\n step_id: string;\n run_id: string;\n seq: string | number;\n status: string;\n output: string | null;\n error: string | null;\n started_at: string | number | null;\n}\n\nexport interface DbTransportOptions {\n /**\n * How to run SQL — built from the app's **own** ORM/connection (no broker, no extra connection).\n * Use `mikroOrmExecutor(em)` to ride the app's MikroORM, `typeOrmExecutor(dataSource)` for TypeORM,\n * or implement `SqlExecutor` for anything else.\n */\n executor: SqlExecutor;\n /** The worker group this instance serves. Required to register `handle()` consumers. */\n group?: string;\n /** Table-name prefix. Tables: `${prefix}_transport_tasks` / `${prefix}_transport_results`. Default `durable`. */\n prefix?: string;\n /** Poll interval in ms when a queue is empty. Default 500. */\n pollMs?: number;\n /** How long a claimed row is owned before it's reclaimable (crash recovery), in ms. Default 30_000. */\n leaseMs?: number;\n /** Max rows claimed per poll. Default 10. */\n batchSize?: number;\n /** Create the two tables on first use if missing. Default true. */\n autoCreate?: boolean;\n /** Identifies this instance in `claimed_by` (for debugging). Defaults to a random id. */\n instanceId?: string;\n}\n\n/**\n * A SQL-backed `Transport` — DBOS-style. Instead of a broker (Redis/SQS), remote steps are **rows**\n * in the same database the durable store already uses: `dispatch` inserts a task row, a worker\n * claims it with `SELECT … FOR UPDATE SKIP LOCKED` (so instances never double-claim), runs it, and\n * writes a result row the engine polls. Zero new infrastructure — the DB you already have IS the\n * queue.\n *\n * Trade-off vs a real broker: throughput is bounded by polling + row contention. Great for\n * workflow/pipeline scale (modest rate, long steps); not for high-fanout firehoses.\n *\n * Requires `FOR UPDATE SKIP LOCKED` — **MySQL 8+** or **Postgres 9.5+** (not SQLite). Run one\n * instance engine-side (`onResult`, dispatches) and one per worker process (`handle()` for its group).\n */\nexport class DbTransport implements Transport {\n private readonly exec: SqlExecutor;\n private readonly group?: string | undefined;\n private readonly tasksTable: string;\n private readonly resultsTable: string;\n private readonly pollMs: number;\n private readonly leaseMs: number;\n private readonly batchSize: number;\n private readonly autoCreate: boolean;\n private readonly instanceId: string;\n private readonly isPg: boolean;\n\n private readonly handlers = new Map<string, StepHandler>();\n private running = true;\n private schemaReady?: Promise<void>;\n private taskLoop?: Promise<void>;\n private resultLoop?: Promise<void>;\n\n constructor(options: DbTransportOptions) {\n this.exec = options.executor;\n this.group = options.group;\n const prefix = options.prefix ?? 'durable';\n this.tasksTable = `${prefix}_transport_tasks`;\n this.resultsTable = `${prefix}_transport_results`;\n this.pollMs = options.pollMs ?? 500;\n this.leaseMs = options.leaseMs ?? 30_000;\n this.batchSize = options.batchSize ?? 10;\n this.autoCreate = options.autoCreate ?? true;\n this.instanceId = options.instanceId ?? `db-${Math.floor(Date.now()).toString(36)}`;\n this.isPg = this.exec.dialect === 'postgres';\n }\n\n // ── dialect helpers ──────────────────────────────────────────────────────────────────────────\n /** Positional placeholder for param `i` (1-based): `$i` on Postgres, `?` elsewhere. */\n private ph(i: number): string {\n return this.isPg ? `$${i}` : '?';\n }\n private q(id: string): string {\n return this.exec.escapeId(id);\n }\n private now(): number {\n return Math.floor(Date.now());\n }\n\n // ── schema ───────────────────────────────────────────────────────────────────────────────────\n private ensureSchema(): Promise<void> {\n if (!this.schemaReady) this.schemaReady = this.createTables();\n return this.schemaReady;\n }\n\n private async createTables(): Promise<void> {\n if (!this.autoCreate) return;\n const str = 'varchar(191)';\n const txt = this.isPg ? 'text' : 'longtext';\n const tasks = this.q(this.tasksTable);\n const results = this.q(this.resultsTable);\n await this.exec.query(\n `CREATE TABLE IF NOT EXISTS ${tasks} (\n ${this.q('step_id')} ${str} PRIMARY KEY,\n ${this.q('run_id')} ${str} NOT NULL,\n ${this.q('seq')} integer NOT NULL,\n ${this.q('name')} ${str} NOT NULL,\n ${this.q('grp')} ${str} NOT NULL,\n ${this.q('input')} ${txt},\n ${this.q('attempt')} integer NOT NULL,\n ${this.q('status')} varchar(32) NOT NULL,\n ${this.q('claimed_by')} ${str},\n ${this.q('claimed_at')} bigint,\n ${this.q('created_at')} bigint NOT NULL\n )`,\n );\n await this.exec.query(\n `CREATE TABLE IF NOT EXISTS ${results} (\n ${this.q('step_id')} ${str} PRIMARY KEY,\n ${this.q('run_id')} ${str} NOT NULL,\n ${this.q('seq')} integer NOT NULL,\n ${this.q('status')} varchar(32) NOT NULL,\n ${this.q('output')} ${txt},\n ${this.q('error')} ${txt},\n ${this.q('started_at')} bigint,\n ${this.q('claimed_by')} ${str},\n ${this.q('claimed_at')} bigint,\n ${this.q('created_at')} bigint NOT NULL\n )`,\n );\n // Best-effort index for the group poll (MySQL has no CREATE INDEX IF NOT EXISTS < 8.0.13).\n await this.exec\n .query(\n `CREATE INDEX ${this.q(`${this.tasksTable}_grp_idx`)} ON ${tasks} (${this.q('grp')}, ${this.q('status')}, ${this.q('created_at')})`,\n )\n .catch(() => {});\n }\n\n // ── engine → worker ────────────────────────────────────────────────────────────────────────\n async dispatch(task: RemoteTask): Promise<void> {\n await this.ensureSchema();\n const t = this.q(this.tasksTable);\n // Idempotent: a redelivered dispatch for the same step_id is ignored (the row already exists).\n const ignore = this.isPg\n ? `INSERT INTO ${t} (...) ON CONFLICT (${this.q('step_id')}) DO NOTHING`\n : `INSERT IGNORE INTO ${t} (...)`;\n const cols = ['step_id', 'run_id', 'seq', 'name', 'grp', 'input', 'attempt', 'status', 'created_at'];\n const sql = ignore.replace(\n '(...)',\n `(${cols.map((c) => this.q(c)).join(', ')}) VALUES (${cols.map((_, i) => this.ph(i + 1)).join(', ')})`,\n );\n await this.exec.query(sql, [\n task.stepId,\n task.runId,\n task.seq,\n task.name,\n task.group,\n JSON.stringify(task.input ?? null),\n task.attempt,\n 'pending',\n this.now(),\n ]);\n }\n\n // ── worker → engine ────────────────────────────────────────────────────────────────────────\n handle(name: string, fn: StepHandler): void {\n if (!this.group) throw new Error('DbTransport needs a `group` to register handlers');\n this.handlers.set(name, fn);\n if (!this.taskLoop) this.taskLoop = this.loop(() => this.drainTasks(this.group as string));\n }\n\n onResult(handler: (result: StepResult) => Promise<void>): void {\n if (!this.resultLoop) this.resultLoop = this.loop(() => this.drainResults(handler));\n }\n\n onHeartbeat(_handler: (beat: Heartbeat) => Promise<void>): void {\n // Not modelled; the lease/reclaim on `claimed_at` is the liveness mechanism.\n }\n\n // ── polling ──────────────────────────────────────────────────────────────────────────────────\n /** Run `tick` repeatedly; sleep `pollMs` when it reports an empty queue. Stops on `close()`. */\n private async loop(tick: () => Promise<number>): Promise<void> {\n await this.ensureSchema();\n while (this.running) {\n let processed = 0;\n try {\n processed = await tick();\n } catch (err) {\n if (this.running) console.error('[DbTransport] poll failed', err);\n }\n if (processed === 0) await this.sleep(this.pollMs);\n }\n }\n\n /** Claim a batch of tasks (FOR UPDATE SKIP LOCKED), then run each outside the lock. */\n private async drainTasks(group: string): Promise<number> {\n const claimed = await this.claim<TaskRow>(\n this.tasksTable,\n `${this.q('grp')} = ${this.ph(1)}`,\n [group],\n );\n for (const row of claimed) {\n const task: RemoteTask = {\n runId: row.run_id,\n seq: Number(row.seq),\n stepId: row.step_id,\n name: row.name,\n group,\n input: row.input == null ? undefined : JSON.parse(row.input),\n attempt: Number(row.attempt),\n };\n const result = await runStepHandler(task, this.handlers.get(task.name));\n await this.insertResult(result);\n await this.exec.query(\n `DELETE FROM ${this.q(this.tasksTable)} WHERE ${this.q('step_id')} = ${this.ph(1)}`,\n [task.stepId],\n );\n }\n return claimed.length;\n }\n\n /** Claim a batch of results, deliver each to the engine, then delete it. */\n private async drainResults(handler: (result: StepResult) => Promise<void>): Promise<number> {\n const claimed = await this.claim<ResultRow>(this.resultsTable, '1 = 1', []);\n for (const row of claimed) {\n const result: StepResult = {\n runId: row.run_id,\n seq: Number(row.seq),\n stepId: row.step_id,\n status: row.status as StepResult['status'],\n output: row.output == null ? undefined : JSON.parse(row.output),\n error: row.error == null ? undefined : JSON.parse(row.error),\n startedAt: row.started_at == null ? undefined : Number(row.started_at),\n };\n await handler(result);\n await this.exec.query(\n `DELETE FROM ${this.q(this.resultsTable)} WHERE ${this.q('step_id')} = ${this.ph(1)}`,\n [result.stepId],\n );\n }\n return claimed.length;\n }\n\n /**\n * Atomically claim up to `batchSize` un-leased rows matching `where` and stamp them with this\n * instance's lease. `SELECT … FOR UPDATE SKIP LOCKED` inside the txn means concurrent instances\n * skip each other's locked rows instead of blocking, so no row is claimed twice.\n */\n private async claim<T extends { step_id: string }>(\n table: string,\n where: string,\n whereParams: unknown[],\n ): Promise<T[]> {\n const t = this.q(table);\n const staleBefore = this.now() - this.leaseMs;\n return this.exec.transaction(async (tx) => {\n const leaseClause = `(${this.q('claimed_at')} IS NULL OR ${this.q('claimed_at')} < ${this.ph(whereParams.length + 1)})`;\n const rows = await tx.query<T>(\n `SELECT * FROM ${t} WHERE ${where} AND ${leaseClause}\n ORDER BY ${this.q('created_at')} ASC LIMIT ${this.batchSize} FOR UPDATE SKIP LOCKED`,\n [...whereParams, staleBefore],\n );\n if (rows.length > 0) {\n const ids = rows.map((r) => r.step_id);\n const placeholders = ids.map((_, i) => this.ph(i + 3)).join(', ');\n await tx.query(\n `UPDATE ${t} SET ${this.q('claimed_by')} = ${this.ph(1)}, ${this.q('claimed_at')} = ${this.ph(2)}\n WHERE ${this.q('step_id')} IN (${placeholders})`,\n [this.instanceId, this.now(), ...ids],\n );\n }\n return rows;\n });\n }\n\n private async insertResult(result: StepResult): Promise<void> {\n const t = this.q(this.resultsTable);\n const cols = ['step_id', 'run_id', 'seq', 'status', 'output', 'error', 'started_at', 'created_at'];\n const head = this.isPg\n ? `INSERT INTO ${t} (...) ON CONFLICT (${this.q('step_id')}) DO NOTHING`\n : `INSERT IGNORE INTO ${t} (...)`;\n const sql = head.replace(\n '(...)',\n `(${cols.map((c) => this.q(c)).join(', ')}) VALUES (${cols.map((_, i) => this.ph(i + 1)).join(', ')})`,\n );\n await this.exec.query(sql, [\n result.stepId,\n result.runId,\n result.seq,\n result.status,\n result.output === undefined ? null : JSON.stringify(result.output),\n result.error === undefined ? null : JSON.stringify(result.error),\n result.startedAt ?? null,\n this.now(),\n ]);\n }\n\n private sleep(ms: number): Promise<void> {\n return new Promise((r) => setTimeout(r, ms));\n }\n\n /** Stop the pollers and wait for the current ticks to settle. Does not close the shared DataSource. */\n async close(): Promise<void> {\n this.running = false;\n await Promise.allSettled([this.taskLoop, this.resultLoop]);\n }\n}\n","/**\n * The tiny SQL surface `DbTransport` needs, so it rides **whatever ORM/connection the app already\n * has** instead of forcing a specific one. Implement it for your ORM (adapters for TypeORM and\n * MikroORM ship below) and hand it to `new DbTransport({ executor })`. Mirrors how the durable\n * *store* is ORM-pluggable (store-typeorm / store-mikro-orm / …).\n */\nexport interface SqlExecutor {\n /** Drives placeholder style (`$n` vs `?`) and a couple of DDL type choices. */\n readonly dialect: 'mysql' | 'postgres';\n /** Quote/escape an identifier (table/column name) for this dialect. */\n escapeId(id: string): string;\n /** Run a statement outside a transaction. */\n query<T = unknown>(sql: string, params?: unknown[]): Promise<T[]>;\n /** Run `fn` inside a transaction — the claim uses `FOR UPDATE SKIP LOCKED` within it. */\n transaction<T>(fn: (tx: SqlTx) => Promise<T>): Promise<T>;\n}\n\nexport interface SqlTx {\n query<T = unknown>(sql: string, params?: unknown[]): Promise<T[]>;\n}\n\n// ── TypeORM ──────────────────────────────────────────────────────────────────────────────────────\n\n/** Structural slice of a TypeORM `DataSource` — avoids a hard dependency on typeorm. */\ninterface TypeOrmDataSourceLike {\n options: { type: string };\n driver: { escape(id: string): string };\n query(sql: string, params?: unknown[]): Promise<unknown>;\n createQueryRunner(): {\n connect(): Promise<void>;\n startTransaction(): Promise<void>;\n commitTransaction(): Promise<void>;\n rollbackTransaction(): Promise<void>;\n release(): Promise<void>;\n query(sql: string, params?: unknown[]): Promise<unknown>;\n };\n}\n\n/** Build an executor from a TypeORM `DataSource` (e.g. the one the durable store already uses). */\nexport function typeOrmExecutor(ds: TypeOrmDataSourceLike): SqlExecutor {\n return {\n dialect: /postgres/.test(String(ds.options.type)) ? 'postgres' : 'mysql',\n escapeId: (id) => ds.driver.escape(id),\n query: <T>(sql: string, params?: unknown[]) => ds.query(sql, params) as Promise<T[]>,\n transaction: async <T>(fn: (tx: SqlTx) => Promise<T>) => {\n const qr = ds.createQueryRunner();\n await qr.connect();\n await qr.startTransaction();\n try {\n const result = await fn({\n query: <U>(sql: string, params?: unknown[]) => qr.query(sql, params) as Promise<U[]>,\n });\n await qr.commitTransaction();\n return result;\n } catch (err) {\n await qr.rollbackTransaction();\n throw err;\n } finally {\n await qr.release();\n }\n },\n };\n}\n\n// ── MikroORM ─────────────────────────────────────────────────────────────────────────────────────\n\n/** Structural slice of a MikroORM `EntityManager` — avoids a hard dependency on @mikro-orm/core. */\ninterface MikroOrmEmLike {\n getPlatform(): { constructor: { name: string }; quoteIdentifier(id: string): string };\n getConnection(): {\n execute(sql: string, params?: unknown[], method?: 'all', ctx?: unknown): Promise<unknown>;\n };\n getTransactionContext?(): unknown;\n transactional<T>(cb: (em: MikroOrmEmLike) => Promise<T>): Promise<T>;\n}\n\n/**\n * Build an executor from a MikroORM `EntityManager` — i.e. **the app's own configured ORM**. Runs\n * raw SQL through `em.getConnection().execute(...)` and wraps the claim in `em.transactional(...)`,\n * threading the transaction context so `FOR UPDATE SKIP LOCKED` locks within the tx.\n */\nexport function mikroOrmExecutor(em: MikroOrmEmLike): SqlExecutor {\n const platform = em.getPlatform();\n return {\n dialect: /postgre/i.test(platform.constructor.name) ? 'postgres' : 'mysql',\n escapeId: (id) => platform.quoteIdentifier(id),\n query: <T>(sql: string, params?: unknown[]) =>\n em.getConnection().execute(sql, params ?? [], 'all') as Promise<T[]>,\n transaction: <T>(fn: (tx: SqlTx) => Promise<T>) =>\n em.transactional((txEm) =>\n fn({\n query: <U>(sql: string, params?: unknown[]) =>\n txEm\n .getConnection()\n .execute(sql, params ?? [], 'all', txEm.getTransactionContext?.()) as Promise<U[]>,\n }),\n ),\n };\n}\n"],"mapp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AAA,CAAA,QAAkB0E,YAAAA,KAClC;UAAC,KAAK7F;UAAY,KAAKc,IAAG;aAAO6E;SAAI;MAEzC;AACA,aAAOD;IACT,CAAA;EACF;EAEA,MAAcb,aAAaH,QAAmC;AAC5D,UAAM1C,IAAI,KAAKb,EAAE,KAAKxB,YAAY;AAClC,UAAMuC,OAAO;MAAC;MAAW;MAAU;MAAO;MAAU;MAAU;MAAS;MAAc;;AACrF,UAAM4D,OAAO,KAAK7F,OACd,eAAe+B,CAAAA,uBAAwB,KAAKb,EAAE,SAAA,CAAA,iBAC9C,sBAAsBa,CAAAA;AAC1B,UAAMG,MAAM2D,KAAK1D,QACf,SACA,IAAIF,KAAKG,IAAI,CAACC,MAAM,KAAKnB,EAAEmB,CAAAA,CAAAA,EAAIC,KAAK,IAAA,CAAA,aAAkBL,KAAKG,IAAI,CAACG,GAAGtB,MAAM,KAAKD,GAAGC,IAAI,CAAA,CAAA,EAAIqB,KAAK,IAAA,CAAA,GAAQ;AAExG,UAAM,KAAK/C,KAAKoC,MAAMO,KAAK;MACzBuC,OAAOjC;MACPiC,OAAOhC;MACPgC,OAAO/B;MACP+B,OAAOK;MACPL,OAAOM,WAAWR,SAAY,OAAO3B,KAAKC,UAAU4B,OAAOM,MAAM;MACjEN,OAAOV,UAAUQ,SAAY,OAAO3B,KAAKC,UAAU4B,OAAOV,KAAK;MAC/DU,OAAOO,aAAa;MACpB,KAAKnE,IAAG;KACT;EACH;EAEQmD,MAAM8B,IAA2B;AACvC,WAAO,IAAIC,QAAQ,CAACJ,MAAMK,WAAWL,GAAGG,EAAAA,CAAAA;EAC1C;;EAGA,MAAMG,QAAuB;AAC3B,SAAK9F,UAAU;AACf,UAAM4F,QAAQG,WAAW;MAAC,KAAK7F;MAAU,KAAKC;KAAW;EAC3D;AACF;","names":["typeOrmExecutor","ds","dialect","test","String","options","type","escapeId","id","driver","escape","query","sql","params","transaction","fn","qr","createQueryRunner","connect","startTransaction","result","commitTransaction","err","rollbackTransaction","release","mikroOrmExecutor","em","platform","getPlatform","name","quoteIdentifier","getConnection","execute","transactional","txEm","getTransactionContext","DbTransport","exec","group","tasksTable","resultsTable","pollMs","leaseMs","batchSize","autoCreate","instanceId","isPg","handlers","Map","running","schemaReady","taskLoop","resultLoop","options","executor","prefix","Math","floor","Date","now","toString","dialect","ph","i","q","id","escapeId","ensureSchema","createTables","str","txt","tasks","results","query","catch","dispatch","task","t","ignore","cols","sql","replace","map","c","join","_","stepId","runId","seq","name","JSON","stringify","input","attempt","handle","fn","Error","set","loop","drainTasks","onResult","handler","drainResults","onHeartbeat","_handler","tick","processed","err","console","error","sleep","claimed","claim","row","run_id","Number","step_id","undefined","parse","result","runStepHandler","get","insertResult","length","status","output","startedAt","started_at","table","where","whereParams","staleBefore","transaction","tx","leaseClause","rows","ids","r","placeholders","head","ms","Promise","setTimeout","close","allSettled"]}
@@ -1,7 +1,66 @@
1
- import { type Heartbeat, type RemoteTask, type StepHandler, type StepResult, type Transport } from '@dudousxd/nestjs-durable-core';
2
- import type { SqlExecutor } from './executors';
3
- export { mikroOrmExecutor, type SqlExecutor, type SqlTx, typeOrmExecutor } from './executors';
4
- export interface DbTransportOptions {
1
+ import { Transport, RemoteTask, StepHandler, StepResult, Heartbeat } from '@dudousxd/nestjs-durable-core';
2
+
3
+ /**
4
+ * The tiny SQL surface `DbTransport` needs, so it rides **whatever ORM/connection the app already
5
+ * has** instead of forcing a specific one. Implement it for your ORM (adapters for TypeORM and
6
+ * MikroORM ship below) and hand it to `new DbTransport({ executor })`. Mirrors how the durable
7
+ * *store* is ORM-pluggable (store-typeorm / store-mikro-orm / …).
8
+ */
9
+ interface SqlExecutor {
10
+ /** Drives placeholder style (`$n` vs `?`) and a couple of DDL type choices. */
11
+ readonly dialect: 'mysql' | 'postgres';
12
+ /** Quote/escape an identifier (table/column name) for this dialect. */
13
+ escapeId(id: string): string;
14
+ /** Run a statement outside a transaction. */
15
+ query<T = unknown>(sql: string, params?: unknown[]): Promise<T[]>;
16
+ /** Run `fn` inside a transaction — the claim uses `FOR UPDATE SKIP LOCKED` within it. */
17
+ transaction<T>(fn: (tx: SqlTx) => Promise<T>): Promise<T>;
18
+ }
19
+ interface SqlTx {
20
+ query<T = unknown>(sql: string, params?: unknown[]): Promise<T[]>;
21
+ }
22
+ /** Structural slice of a TypeORM `DataSource` — avoids a hard dependency on typeorm. */
23
+ interface TypeOrmDataSourceLike {
24
+ options: {
25
+ type: string;
26
+ };
27
+ driver: {
28
+ escape(id: string): string;
29
+ };
30
+ query(sql: string, params?: unknown[]): Promise<unknown>;
31
+ createQueryRunner(): {
32
+ connect(): Promise<void>;
33
+ startTransaction(): Promise<void>;
34
+ commitTransaction(): Promise<void>;
35
+ rollbackTransaction(): Promise<void>;
36
+ release(): Promise<void>;
37
+ query(sql: string, params?: unknown[]): Promise<unknown>;
38
+ };
39
+ }
40
+ /** Build an executor from a TypeORM `DataSource` (e.g. the one the durable store already uses). */
41
+ declare function typeOrmExecutor(ds: TypeOrmDataSourceLike): SqlExecutor;
42
+ /** Structural slice of a MikroORM `EntityManager` — avoids a hard dependency on @mikro-orm/core. */
43
+ interface MikroOrmEmLike {
44
+ getPlatform(): {
45
+ constructor: {
46
+ name: string;
47
+ };
48
+ quoteIdentifier(id: string): string;
49
+ };
50
+ getConnection(): {
51
+ execute(sql: string, params?: unknown[], method?: 'all', ctx?: unknown): Promise<unknown>;
52
+ };
53
+ getTransactionContext?(): unknown;
54
+ transactional<T>(cb: (em: MikroOrmEmLike) => Promise<T>): Promise<T>;
55
+ }
56
+ /**
57
+ * Build an executor from a MikroORM `EntityManager` — i.e. **the app's own configured ORM**. Runs
58
+ * raw SQL through `em.getConnection().execute(...)` and wraps the claim in `em.transactional(...)`,
59
+ * threading the transaction context so `FOR UPDATE SKIP LOCKED` locks within the tx.
60
+ */
61
+ declare function mikroOrmExecutor(em: MikroOrmEmLike): SqlExecutor;
62
+
63
+ interface DbTransportOptions {
5
64
  /**
6
65
  * How to run SQL — built from the app's **own** ORM/connection (no broker, no extra connection).
7
66
  * Use `mikroOrmExecutor(em)` to ride the app's MikroORM, `typeOrmExecutor(dataSource)` for TypeORM,
@@ -36,7 +95,7 @@ export interface DbTransportOptions {
36
95
  * Requires `FOR UPDATE SKIP LOCKED` — **MySQL 8+** or **Postgres 9.5+** (not SQLite). Run one
37
96
  * instance engine-side (`onResult`, dispatches) and one per worker process (`handle()` for its group).
38
97
  */
39
- export declare class DbTransport implements Transport {
98
+ declare class DbTransport implements Transport {
40
99
  private readonly exec;
41
100
  private readonly group?;
42
101
  private readonly tasksTable;
@@ -80,4 +139,5 @@ export declare class DbTransport implements Transport {
80
139
  /** Stop the pollers and wait for the current ticks to settle. Does not close the shared DataSource. */
81
140
  close(): Promise<void>;
82
141
  }
83
- //# sourceMappingURL=db-transport.d.ts.map
142
+
143
+ export { DbTransport, type DbTransportOptions, type SqlExecutor, type SqlTx, mikroOrmExecutor, typeOrmExecutor };
package/dist/index.d.ts CHANGED
@@ -1,2 +1,143 @@
1
- export * from './db-transport';
2
- //# sourceMappingURL=index.d.ts.map
1
+ import { Transport, RemoteTask, StepHandler, StepResult, Heartbeat } from '@dudousxd/nestjs-durable-core';
2
+
3
+ /**
4
+ * The tiny SQL surface `DbTransport` needs, so it rides **whatever ORM/connection the app already
5
+ * has** instead of forcing a specific one. Implement it for your ORM (adapters for TypeORM and
6
+ * MikroORM ship below) and hand it to `new DbTransport({ executor })`. Mirrors how the durable
7
+ * *store* is ORM-pluggable (store-typeorm / store-mikro-orm / …).
8
+ */
9
+ interface SqlExecutor {
10
+ /** Drives placeholder style (`$n` vs `?`) and a couple of DDL type choices. */
11
+ readonly dialect: 'mysql' | 'postgres';
12
+ /** Quote/escape an identifier (table/column name) for this dialect. */
13
+ escapeId(id: string): string;
14
+ /** Run a statement outside a transaction. */
15
+ query<T = unknown>(sql: string, params?: unknown[]): Promise<T[]>;
16
+ /** Run `fn` inside a transaction — the claim uses `FOR UPDATE SKIP LOCKED` within it. */
17
+ transaction<T>(fn: (tx: SqlTx) => Promise<T>): Promise<T>;
18
+ }
19
+ interface SqlTx {
20
+ query<T = unknown>(sql: string, params?: unknown[]): Promise<T[]>;
21
+ }
22
+ /** Structural slice of a TypeORM `DataSource` — avoids a hard dependency on typeorm. */
23
+ interface TypeOrmDataSourceLike {
24
+ options: {
25
+ type: string;
26
+ };
27
+ driver: {
28
+ escape(id: string): string;
29
+ };
30
+ query(sql: string, params?: unknown[]): Promise<unknown>;
31
+ createQueryRunner(): {
32
+ connect(): Promise<void>;
33
+ startTransaction(): Promise<void>;
34
+ commitTransaction(): Promise<void>;
35
+ rollbackTransaction(): Promise<void>;
36
+ release(): Promise<void>;
37
+ query(sql: string, params?: unknown[]): Promise<unknown>;
38
+ };
39
+ }
40
+ /** Build an executor from a TypeORM `DataSource` (e.g. the one the durable store already uses). */
41
+ declare function typeOrmExecutor(ds: TypeOrmDataSourceLike): SqlExecutor;
42
+ /** Structural slice of a MikroORM `EntityManager` — avoids a hard dependency on @mikro-orm/core. */
43
+ interface MikroOrmEmLike {
44
+ getPlatform(): {
45
+ constructor: {
46
+ name: string;
47
+ };
48
+ quoteIdentifier(id: string): string;
49
+ };
50
+ getConnection(): {
51
+ execute(sql: string, params?: unknown[], method?: 'all', ctx?: unknown): Promise<unknown>;
52
+ };
53
+ getTransactionContext?(): unknown;
54
+ transactional<T>(cb: (em: MikroOrmEmLike) => Promise<T>): Promise<T>;
55
+ }
56
+ /**
57
+ * Build an executor from a MikroORM `EntityManager` — i.e. **the app's own configured ORM**. Runs
58
+ * raw SQL through `em.getConnection().execute(...)` and wraps the claim in `em.transactional(...)`,
59
+ * threading the transaction context so `FOR UPDATE SKIP LOCKED` locks within the tx.
60
+ */
61
+ declare function mikroOrmExecutor(em: MikroOrmEmLike): SqlExecutor;
62
+
63
+ interface DbTransportOptions {
64
+ /**
65
+ * How to run SQL — built from the app's **own** ORM/connection (no broker, no extra connection).
66
+ * Use `mikroOrmExecutor(em)` to ride the app's MikroORM, `typeOrmExecutor(dataSource)` for TypeORM,
67
+ * or implement `SqlExecutor` for anything else.
68
+ */
69
+ executor: SqlExecutor;
70
+ /** The worker group this instance serves. Required to register `handle()` consumers. */
71
+ group?: string;
72
+ /** Table-name prefix. Tables: `${prefix}_transport_tasks` / `${prefix}_transport_results`. Default `durable`. */
73
+ prefix?: string;
74
+ /** Poll interval in ms when a queue is empty. Default 500. */
75
+ pollMs?: number;
76
+ /** How long a claimed row is owned before it's reclaimable (crash recovery), in ms. Default 30_000. */
77
+ leaseMs?: number;
78
+ /** Max rows claimed per poll. Default 10. */
79
+ batchSize?: number;
80
+ /** Create the two tables on first use if missing. Default true. */
81
+ autoCreate?: boolean;
82
+ /** Identifies this instance in `claimed_by` (for debugging). Defaults to a random id. */
83
+ instanceId?: string;
84
+ }
85
+ /**
86
+ * A SQL-backed `Transport` — DBOS-style. Instead of a broker (Redis/SQS), remote steps are **rows**
87
+ * in the same database the durable store already uses: `dispatch` inserts a task row, a worker
88
+ * claims it with `SELECT … FOR UPDATE SKIP LOCKED` (so instances never double-claim), runs it, and
89
+ * writes a result row the engine polls. Zero new infrastructure — the DB you already have IS the
90
+ * queue.
91
+ *
92
+ * Trade-off vs a real broker: throughput is bounded by polling + row contention. Great for
93
+ * workflow/pipeline scale (modest rate, long steps); not for high-fanout firehoses.
94
+ *
95
+ * Requires `FOR UPDATE SKIP LOCKED` — **MySQL 8+** or **Postgres 9.5+** (not SQLite). Run one
96
+ * instance engine-side (`onResult`, dispatches) and one per worker process (`handle()` for its group).
97
+ */
98
+ declare class DbTransport implements Transport {
99
+ private readonly exec;
100
+ private readonly group?;
101
+ private readonly tasksTable;
102
+ private readonly resultsTable;
103
+ private readonly pollMs;
104
+ private readonly leaseMs;
105
+ private readonly batchSize;
106
+ private readonly autoCreate;
107
+ private readonly instanceId;
108
+ private readonly isPg;
109
+ private readonly handlers;
110
+ private running;
111
+ private schemaReady?;
112
+ private taskLoop?;
113
+ private resultLoop?;
114
+ constructor(options: DbTransportOptions);
115
+ /** Positional placeholder for param `i` (1-based): `$i` on Postgres, `?` elsewhere. */
116
+ private ph;
117
+ private q;
118
+ private now;
119
+ private ensureSchema;
120
+ private createTables;
121
+ dispatch(task: RemoteTask): Promise<void>;
122
+ handle(name: string, fn: StepHandler): void;
123
+ onResult(handler: (result: StepResult) => Promise<void>): void;
124
+ onHeartbeat(_handler: (beat: Heartbeat) => Promise<void>): void;
125
+ /** Run `tick` repeatedly; sleep `pollMs` when it reports an empty queue. Stops on `close()`. */
126
+ private loop;
127
+ /** Claim a batch of tasks (FOR UPDATE SKIP LOCKED), then run each outside the lock. */
128
+ private drainTasks;
129
+ /** Claim a batch of results, deliver each to the engine, then delete it. */
130
+ private drainResults;
131
+ /**
132
+ * Atomically claim up to `batchSize` un-leased rows matching `where` and stamp them with this
133
+ * instance's lease. `SELECT … FOR UPDATE SKIP LOCKED` inside the txn means concurrent instances
134
+ * skip each other's locked rows instead of blocking, so no row is claimed twice.
135
+ */
136
+ private claim;
137
+ private insertResult;
138
+ private sleep;
139
+ /** Stop the pollers and wait for the current ticks to settle. Does not close the shared DataSource. */
140
+ close(): Promise<void>;
141
+ }
142
+
143
+ export { DbTransport, type DbTransportOptions, type SqlExecutor, type SqlTx, mikroOrmExecutor, typeOrmExecutor };