cairnq 0.8.0 → 0.9.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.
@@ -1,9 +1,27 @@
1
1
  import { mkdirSync } from "node:fs";
2
2
  import { dirname, resolve } from "node:path";
3
- import Database from "better-sqlite3";
3
+ import { createRequire } from "node:module";
4
4
  import { nowMs } from "../ids.js";
5
5
  import { loadMigrations, loadStatements } from "../sql.js";
6
6
  import { checkProtocolVersion, COMMENT, statementParams, TaskStore, } from "./base.js";
7
+ // `better-sqlite3` is an optional dependency, matching `pg` on the Postgres side:
8
+ // a Postgres-only deployment should not have to build a native module it never
9
+ // loads, and importing this file must not pull one in. Required (not imported)
10
+ // because ensure() is synchronous — the open path applies migrations and cannot
11
+ // await — and createRequire gives a synchronous load from ESM.
12
+ const require = createRequire(import.meta.url);
13
+ let sqliteModule = null;
14
+ function loadSqlite() {
15
+ if (sqliteModule)
16
+ return sqliteModule;
17
+ try {
18
+ sqliteModule = require("better-sqlite3");
19
+ }
20
+ catch {
21
+ throw new Error("SQLiteStore requires the 'better-sqlite3' package — install it (e.g. `npm i better-sqlite3`)");
22
+ }
23
+ return sqliteModule;
24
+ }
7
25
  const WAL_RETRY_DELAY_MS = 50;
8
26
  const WAL_RETRY_BUDGET_MS = 5_000;
9
27
  const BUSY_RETRY_BASE_MS = 1;
@@ -221,7 +239,7 @@ export class SQLiteStore extends TaskStore {
221
239
  const memory = isMemory(this.path);
222
240
  if (!memory)
223
241
  mkdirSync(dirname(this.path), { recursive: true });
224
- const db = new Database(this.path);
242
+ const db = new (loadSqlite())(this.path);
225
243
  // Only the synchronous part of the open path gets a real busy_timeout: the WAL
226
244
  // switch and the migrations cannot await a retry. See the class comment.
227
245
  db.pragma(`busy_timeout = ${this.busyBudgetMs}`);
package/dist/worker.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import type { BackpressureOptions } from "./backpressure.js";
2
2
  import { TaskContext } from "./context.js";
3
+ import type { PgExecutor } from "./store/pg-executor.js";
3
4
  import type { TaskStore } from "./store/base.js";
4
5
  import { type TaskDef } from "./task.js";
5
6
  export { retryDelayMs, DEFAULT_RETRY_BACKOFF_MS, DEFAULT_RETRY_BACKOFF_MAX_MS } from "./backoff.js";
@@ -130,11 +131,13 @@ export declare class Worker {
130
131
  queues?: string[];
131
132
  busyTimeoutMs?: number;
132
133
  }): Worker;
133
- /** Multi-host backend. `dsn` is a libpq connection string; requires the
134
- * optional `pg` package. */
135
- static postgres(dsn: string, opts?: WorkerOptions & {
134
+ /** Multi-host backend. `source` is a libpq connection string — which requires
135
+ * the optional `pg` package — or a PgExecutor over a driver the application
136
+ * already runs, which cairnq then shares instead of opening a second pool. */
137
+ static postgres(source: string | PgExecutor, opts?: WorkerOptions & {
136
138
  queues?: string[];
137
139
  max?: number;
140
+ schema?: string;
138
141
  }): Worker;
139
142
  get id(): string;
140
143
  task(handler: Handler): this;
package/dist/worker.js CHANGED
@@ -133,11 +133,12 @@ export class Worker {
133
133
  worker.ownsStore = true;
134
134
  return worker;
135
135
  }
136
- /** Multi-host backend. `dsn` is a libpq connection string; requires the
137
- * optional `pg` package. */
138
- static postgres(dsn, opts = {}) {
139
- const { queues = ["default"], max, ...rest } = opts;
140
- const worker = new Worker(new PostgresStore(dsn, { max }), queues, rest);
136
+ /** Multi-host backend. `source` is a libpq connection string — which requires
137
+ * the optional `pg` package — or a PgExecutor over a driver the application
138
+ * already runs, which cairnq then shares instead of opening a second pool. */
139
+ static postgres(source, opts = {}) {
140
+ const { queues = ["default"], max, schema, ...rest } = opts;
141
+ const worker = new Worker(new PostgresStore(source, { max, schema }), queues, rest);
141
142
  worker.ownsStore = true;
142
143
  return worker;
143
144
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cairnq",
3
- "version": "0.8.0",
3
+ "version": "0.9.0",
4
4
  "description": "SQLite-first, cross-language, storage-centered durable task runtime",
5
5
  "license": "MIT",
6
6
  "author": "Jannchie <jannchie@gmail.com>",
@@ -39,19 +39,21 @@
39
39
  "test": "vitest run",
40
40
  "typecheck": "tsc -p tsconfig.json --noEmit"
41
41
  },
42
- "dependencies": {
43
- "better-sqlite3": "^13.0.2"
44
- },
45
42
  "peerDependencies": {
43
+ "better-sqlite3": "^13.0.2",
46
44
  "pg": "^8.13.0"
47
45
  },
48
46
  "peerDependenciesMeta": {
47
+ "better-sqlite3": {
48
+ "optional": true
49
+ },
49
50
  "pg": {
50
51
  "optional": true
51
52
  }
52
53
  },
53
54
  "devDependencies": {
54
55
  "@types/better-sqlite3": "^7.6.11",
56
+ "better-sqlite3": "^13.0.2",
55
57
  "@types/node": "^22.7.0",
56
58
  "@types/pg": "^8.11.10",
57
59
  "pg": "^8.13.0",
package/src/client.ts CHANGED
@@ -4,7 +4,15 @@ import { TaskCanceled, TaskFailed } from "./errors.js";
4
4
  import { isFailed, isSucceeded, type Task, type TaskRef, type TaskStatus } from "./models.js";
5
5
  import { SQLiteStore } from "./store/sqlite.js";
6
6
  import { PostgresStore } from "./store/postgres.js";
7
- import type { ListInput, PurgeInput, SubmitInput, TaskStore } from "./store/base.js";
7
+ import type { PgExecutor } from "./store/pg-executor.js";
8
+ import type {
9
+ ListInput,
10
+ PurgeInput,
11
+ SubmitInput,
12
+ TaskStore,
13
+ WatchOptions,
14
+ WatchSignal,
15
+ } from "./store/base.js";
8
16
  import { type TaskDef, taskName } from "./task.js";
9
17
  import { DEFAULT_WAIT_TIMEOUT_MS, type PollOptions, pollWait, pollWaitByKey } from "./wait.js";
10
18
 
@@ -53,11 +61,16 @@ export class CairnQ {
53
61
  return new CairnQ(new SQLiteStore(path, { busyTimeoutMs }), client);
54
62
  }
55
63
 
56
- /** Multi-host backend. `dsn` is a libpq connection string; requires the
57
- * optional `pg` package. */
58
- static postgres(dsn: string, opts: { max?: number } & ClientOptions = {}): CairnQ {
59
- const { max, ...client } = opts;
60
- return new CairnQ(new PostgresStore(dsn, { max }), client);
64
+ /** Multi-host backend. `source` is a libpq connection string — which requires
65
+ * the optional `pg` package — or a PgExecutor over a driver the application
66
+ * already runs (an ORM's pool, say), which cairnq then shares instead of
67
+ * opening a second one. */
68
+ static postgres(
69
+ source: string | PgExecutor,
70
+ opts: { max?: number; schema?: string } & ClientOptions = {},
71
+ ): CairnQ {
72
+ const { max, schema, ...client } = opts;
73
+ return new CairnQ(new PostgresStore(source, { max, schema }), client);
61
74
  }
62
75
 
63
76
  get store(): TaskStore {
@@ -146,6 +159,20 @@ export class CairnQ {
146
159
  return this._store.stats();
147
160
  }
148
161
 
162
+ /**
163
+ * Call `onSignal` when the tasks on `queues` may have changed. Returns an
164
+ * unsubscribe.
165
+ *
166
+ * Notify-accelerated polling, not an event log: a signal means "re-read now",
167
+ * and `stats()` / `list()` / `get()` are where the truth is. On Postgres an
168
+ * idle watch costs nothing and signals land in milliseconds; everywhere else
169
+ * the timer alone still delivers, so the same consumer code is correct either
170
+ * way. See TaskStore.watch for the full contract.
171
+ */
172
+ watch(opts: WatchOptions, onSignal: (signal: WatchSignal) => void): () => void {
173
+ return this._store.watch(opts, onSignal);
174
+ }
175
+
149
176
  /** Wait for a task to finish. Resolves with the terminal Task (any status);
150
177
  * throws TaskTimeout without stopping the task, so `wait(err.taskId)` picks the
151
178
  * same wait back up — from another process, or after a longer deadline. */
package/src/context.ts CHANGED
@@ -3,6 +3,7 @@ import { asEnvelope, type FailReason, LostLease } from "./errors.js";
3
3
  import { cancelRequested, type Task } from "./models.js";
4
4
  import type { SubmitOptions } from "./client.js";
5
5
  import type { TaskStore } from "./store/base.js";
6
+ import type { PgSession } from "./store/pg-executor.js";
6
7
  import { type TaskDef, taskName } from "./task.js";
7
8
  import { pollWait } from "./wait.js";
8
9
 
@@ -201,6 +202,42 @@ export class TaskContext {
201
202
  return task;
202
203
  }
203
204
 
205
+ /**
206
+ * Finalize this task as succeeded, committing the caller's own writes in the
207
+ * SAME transaction as the settlement. Whatever `write` returns becomes the
208
+ * task's result.
209
+ *
210
+ * await ctx.succeedIn(async (session) => {
211
+ * await db.withSession(session).insert(pages).values(rendered)
212
+ * return { pages: rendered.length }
213
+ * })
214
+ *
215
+ * The alternative — write the rows, then settle — has a window between the two
216
+ * commits where the work is durable but the task still reads as running. A
217
+ * crash there re-runs the whole task, which for a render or an ingest means
218
+ * recomputing it, and for non-idempotent work means doing it twice.
219
+ *
220
+ * `session` is the driver's, so this needs a Postgres store built on a
221
+ * PgExecutor the application shares with its own driver; anything else throws.
222
+ * If the settlement finds the lease gone, `write`'s work is rolled back with
223
+ * it and LostLease is raised. Returns null if this task was already settled.
224
+ *
225
+ * `write` may be replayed if the backend retries the transaction — derive
226
+ * nothing inside it that cannot be derived twice.
227
+ */
228
+ async succeedIn<T>(write: (session: PgSession) => Promise<T>): Promise<Task | null> {
229
+ if (this.isSettled) return null;
230
+ const task = await this.owned(async () => {
231
+ const { task } = await this.store.completeIn<PgSession, T>(
232
+ { taskId: this.task.id, workerId: this.workerId },
233
+ write,
234
+ );
235
+ return task;
236
+ });
237
+ this.markSettled();
238
+ return task;
239
+ }
240
+
204
241
  /**
205
242
  * Finalize this task as failed, now. `error` may be a string reason, an Error,
206
243
  * a TaskError (which carries its own retryability), or a ready envelope.
package/src/index.ts CHANGED
@@ -12,8 +12,19 @@ export { defineTask } from "./task.js";
12
12
  export type { TaskDef } from "./task.js";
13
13
  export { SQLiteStore } from "./store/sqlite.js";
14
14
  export { PostgresStore } from "./store/postgres.js";
15
+ export { ListenUnavailable } from "./store/pg-executor.js";
16
+ export type { PgExecutor, PgSession, Row } from "./store/pg-executor.js";
17
+ export { createPoolExecutor } from "./store/pg-pool.js";
15
18
  export { TaskStore } from "./store/base.js";
16
- export type { ListInput, PurgeInput, SubmitInput, Conflict } from "./store/base.js";
19
+ export type {
20
+ ListInput,
21
+ PurgeInput,
22
+ SubmitInput,
23
+ Conflict,
24
+ WatchOptions,
25
+ WatchSignal,
26
+ } from "./store/base.js";
27
+ export { DEFAULT_WATCH_POLL_MS } from "./store/base.js";
17
28
  export type { Task, TaskRef, TaskStatus, TerminalStatus } from "./models.js";
18
29
  export {
19
30
  STATUSES,
package/src/store/base.ts CHANGED
@@ -201,6 +201,35 @@ export function statementParams(sql: string): readonly string[] {
201
201
  * them in one place is what stops SQLite and Postgres from drifting apart in
202
202
  * behavior; the shared SQL already stops them from drifting in wording.
203
203
  */
204
+ /**
205
+ * Why `watch` is calling back.
206
+ *
207
+ * `queued` / `done` come from the store's push channel and name what moved;
208
+ * `poll` is the timer saying the watch cannot rule out a change. None of them
209
+ * carries state — the row is the truth.
210
+ */
211
+ export interface WatchSignal {
212
+ reason: "queued" | "done" | "poll";
213
+ /** The queue a task was queued on. Only on `queued`. */
214
+ queue?: string;
215
+ /** The task that reached a terminal status. Only on `done`. */
216
+ taskId?: string;
217
+ }
218
+
219
+ export interface WatchOptions {
220
+ /** Restrict `queued` signals to these queues. Unset watches every queue. */
221
+ queues?: string[];
222
+ /**
223
+ * How often to signal in the absence of a push channel — and, where there is
224
+ * one, how long a dropped listener can go unnoticed. The default trades a
225
+ * dashboard's idle query rate against how stale it may look.
226
+ */
227
+ pollMs?: number;
228
+ }
229
+
230
+ /** See WatchOptions.pollMs. */
231
+ export const DEFAULT_WATCH_POLL_MS = 2_000;
232
+
204
233
  export abstract class TaskStore {
205
234
  /** Set by useBackpressure; null means submit is ungated. */
206
235
  private gate: QueueDepthGate | null = null;
@@ -222,6 +251,37 @@ export abstract class TaskStore {
222
251
  */
223
252
  protected abstract tx<T>(fn: (fetch: Fetch) => Promise<T>): Promise<T>;
224
253
 
254
+ /**
255
+ * `tx`, but also handing `fn` the driver's own session so the CALLER can run
256
+ * their statements in the same transaction as the protocol's.
257
+ *
258
+ * This is what lets a task's settlement and the rows that task produced commit
259
+ * together. Without it the two are separate transactions and there is a window
260
+ * where the work is durable but the task still reads as unfinished — a crash
261
+ * there costs a full recomputation on retry, and for non-idempotent work costs
262
+ * more than that.
263
+ *
264
+ * Optional, because the session type is the driver's, not the protocol's: a
265
+ * store that has no session worth handing out simply does not implement it and
266
+ * `completeIn` reports that. Postgres implements it; SQLite does not.
267
+ */
268
+ protected txWithSession?<T>(fn: (fetch: Fetch, session: unknown) => Promise<T>): Promise<T>;
269
+
270
+ /**
271
+ * Register for this store's push channel, if it has one; returns an
272
+ * unsubscribe. A store without a push channel does not implement this, and
273
+ * `watch` degrades to its timer alone.
274
+ */
275
+ protected subscribePush?(onSignal: (signal: WatchSignal) => void): () => void;
276
+
277
+ /**
278
+ * Nudge the push channel back up if it has dropped. Called from `watch`'s
279
+ * timer, which is the only thing keeping a client-side subscriber alive: a
280
+ * process that never claims never calls claimWake, so without this a listener
281
+ * that died once would never come back there.
282
+ */
283
+ protected warmPush?(): void;
284
+
225
285
  /**
226
286
  * Whether it is worth opening the claim transaction at all. SQLite gates its
227
287
  * single write lock behind a read-only probe; Postgres readers don't block
@@ -460,6 +520,57 @@ export abstract class TaskStore {
460
520
  return out;
461
521
  }
462
522
 
523
+ /**
524
+ * Call `onSignal` when the tasks on `queues` may have changed — something was
525
+ * queued, or something finished.
526
+ *
527
+ * This is notify-ACCELERATED POLLING, not an event log, and the difference is
528
+ * the whole contract. Where a push channel is available (Postgres LISTEN) an
529
+ * idle watch costs nothing and a signal arrives within milliseconds of the
530
+ * event. Where it is not — a transaction-mode pooler refuses LISTEN, SQLite has
531
+ * no channel at all — the timer alone still delivers `poll` signals, so a
532
+ * consumer that re-reads on every signal is correct in both cases and merely
533
+ * less prompt in one.
534
+ *
535
+ * What it will NOT do is promise that a signal means something happened, or
536
+ * that every event produces its own signal. Treat a signal as "re-read now"
537
+ * and take the truth from `stats()` / `list()` / `get()`, which is where it
538
+ * lives. `reason` is a hint for reading less: a `done` signal names the task,
539
+ * so a dashboard can refresh that row instead of the list.
540
+ *
541
+ * Returns an unsubscribe. The timer is unref'd — watching does not hold a
542
+ * process open.
543
+ */
544
+ watch(opts: WatchOptions, onSignal: (signal: WatchSignal) => void): () => void {
545
+ const pollMs = Math.max(1, opts.pollMs ?? DEFAULT_WATCH_POLL_MS);
546
+ const queues = opts.queues ?? null;
547
+ let live = true;
548
+ const emit = (signal: WatchSignal): void => {
549
+ // A signal delivered after unsubscribe would have the consumer re-reading
550
+ // a store it has stopped caring about, possibly a closed one.
551
+ if (live) onSignal(signal);
552
+ };
553
+ const unsubscribe = this.subscribePush?.((signal) => {
554
+ // A queued signal names its queue, so a watch scoped to some queues can
555
+ // drop the rest. A done signal names only the task — which queue it was on
556
+ // is not in the notification, so it is never filtered out.
557
+ if (signal.reason === "queued" && queues && signal.queue && !queues.includes(signal.queue)) {
558
+ return;
559
+ }
560
+ emit(signal);
561
+ });
562
+ const timer = setInterval(() => {
563
+ this.warmPush?.();
564
+ emit({ reason: "poll" });
565
+ }, pollMs);
566
+ timer.unref?.();
567
+ return () => {
568
+ live = false;
569
+ unsubscribe?.();
570
+ clearInterval(timer);
571
+ };
572
+ }
573
+
463
574
  /**
464
575
  * How many more tasks fit on `queue` under `maxDepth` — 0 once it is full.
465
576
  *
@@ -636,6 +747,45 @@ export abstract class TaskStore {
636
747
  });
637
748
  }
638
749
 
750
+ /**
751
+ * `complete`, with the caller's own writes committed in the same transaction.
752
+ *
753
+ * `fn` runs first and whatever it returns becomes the task's result; the
754
+ * settlement is the last statement in the transaction. So a lost lease — the
755
+ * settlement matching no row — rolls the caller's writes back with it, and
756
+ * there is no ordering in which the work is recorded but the task is not.
757
+ *
758
+ * The settlement runs LAST rather than checking ownership up front on purpose:
759
+ * the ownership predicate lives in the protocol's complete.sql, and a
760
+ * fail-fast pre-check here would be a second copy of it, free to drift. The
761
+ * cost of that choice is that a doomed attempt does its work before finding
762
+ * out, which is the rare path.
763
+ *
764
+ * `fn` must be replayable for the same reason `tx`'s callback must be.
765
+ */
766
+ async completeIn<S, T>(
767
+ input: { taskId: string; workerId: string },
768
+ fn: (session: S) => Promise<T>,
769
+ ): Promise<{ task: Task; value: T }> {
770
+ if (!this.txWithSession) {
771
+ throw new Error(
772
+ "this store cannot share a transaction with the caller — " +
773
+ "completeIn requires a Postgres store (see PgExecutor)",
774
+ );
775
+ }
776
+ return this.txWithSession(async (fetch, session) => {
777
+ const value = await fn(session as S);
778
+ const rows = await fetch("complete", {
779
+ id: input.taskId,
780
+ worker_id: input.workerId,
781
+ result: value == null ? null : dumpJson(value),
782
+ });
783
+ // Rolls back `fn`'s writes along with the settlement that did not land.
784
+ if (!rows.length) throw new LostLease(input.taskId);
785
+ return { task: rowToTask(rows[0]), value };
786
+ });
787
+ }
788
+
639
789
  async fail(input: {
640
790
  taskId: string;
641
791
  workerId: string;
@@ -0,0 +1,90 @@
1
+ /**
2
+ * The seam between PostgresStore and whatever actually talks to Postgres.
3
+ *
4
+ * PostgresStore owns the *dialect* — the protocol's named-parameter SQL rewritten
5
+ * to `$n`, the migration ledger, the LISTEN policy. It does not own the
6
+ * *connection*. Applications that already run a Postgres driver (an ORM, a pool
7
+ * they size themselves) pass their own executor and cairnq joins that session
8
+ * instead of opening a second one, which is what makes a task's settlement
9
+ * commit in the same transaction as the rows the task produced.
10
+ *
11
+ * Implementing one is small — see `createPoolExecutor` below for the reference
12
+ * implementation over `pg`, and PROTOCOL.md for the two things an adapter must
13
+ * get right that are easy to miss: int8 must come back as a JS number (every
14
+ * cairnq bigint is an epoch-ms or a counter, all inside the safe range), and
15
+ * jsonb must come back as an object, not a string.
16
+ */
17
+
18
+ /** A row as the driver hands it back: column name -> value. */
19
+ export type Row = Record<string, unknown>;
20
+
21
+ /**
22
+ * Somewhere statements can run. The same shape whether it is a pool (each call
23
+ * on some connection) or one transaction's dedicated connection — the store's
24
+ * statements do not care, and this is what lets `tx` hand the same interface to
25
+ * its callback.
26
+ */
27
+ export interface PgSession {
28
+ /**
29
+ * One parameterised statement, `$1`-style. `values` is positional and may
30
+ * legitimately contain nulls — a null parameter is "this filter is off" in
31
+ * several protocol statements, not a missing argument.
32
+ */
33
+ query(text: string, values: readonly unknown[]): Promise<Row[]>;
34
+ /**
35
+ * Parameterless SQL that may hold several statements, for migration DDL.
36
+ * Separate from `query` because it must go over the simple query protocol:
37
+ * the extended protocol a parameterised call uses accepts only one statement,
38
+ * and every migration is a script.
39
+ */
40
+ exec(sql: string): Promise<void>;
41
+ }
42
+
43
+ /** A session that can also open transactions, listen, and be shut down. */
44
+ export interface PgExecutor extends PgSession {
45
+ /**
46
+ * Run `fn` inside one transaction on one dedicated connection, committing if
47
+ * it returns and rolling back if it throws. The store relies on both halves:
48
+ * a claim that cannot see its own `recover_leases` is a double-dispatch, and a
49
+ * keyed submit that commits half way poisons the key.
50
+ */
51
+ tx<T>(fn: (session: PgSession) => Promise<T>): Promise<T>;
52
+
53
+ /**
54
+ * Subscribe a dedicated connection to `channels`. Optional: an executor that
55
+ * omits it (or a Postgres that refuses LISTEN) costs latency, never
56
+ * correctness — the store falls back to plain polling, which is the contract
57
+ * PROTOCOL.md gives for push wakeups.
58
+ *
59
+ * Resolves with a function that stops listening. `onClose` reports a
60
+ * connection that dropped on its own, so the store can degrade and retry.
61
+ *
62
+ * Throw `ListenUnavailable` when this Postgres will never accept LISTEN — a
63
+ * transaction-mode pooler, say. Any other rejection is read as transient and
64
+ * retried with backoff, so a permanent condition raised as a plain Error
65
+ * becomes a reconnect loop that cannot succeed.
66
+ */
67
+ listen?(
68
+ channels: readonly string[],
69
+ onNotify: (channel: string, payload: string | undefined) => void,
70
+ onClose: () => void,
71
+ ): Promise<() => void>;
72
+
73
+ /**
74
+ * Release this executor's resources. Called by PostgresStore.close() ONLY for
75
+ * an executor the store created itself: an injected one belongs to the caller,
76
+ * whose other work would not survive cairnq closing it.
77
+ */
78
+ close(): Promise<void>;
79
+ }
80
+
81
+ /**
82
+ * LISTEN will not work on this connection, and retrying cannot change that.
83
+ * See `PgExecutor.listen`.
84
+ */
85
+ export class ListenUnavailable extends Error {
86
+ constructor(message = "LISTEN is not available on this connection") {
87
+ super(message);
88
+ this.name = "ListenUnavailable";
89
+ }
90
+ }
@@ -0,0 +1,156 @@
1
+ import type * as PG from "pg";
2
+
3
+ import { ListenUnavailable, type PgExecutor, type PgSession, type Row } from "./pg-executor.js";
4
+
5
+ // `pg` is an optional dependency: the SDK is SQLite-first, so it's loaded lazily
6
+ // the first time a pool-backed executor is built. Absent -> a clear install hint.
7
+ // An application that brings its own executor never reaches this.
8
+ let pgModule: typeof import("pg") | null = null;
9
+ async function loadPg(): Promise<typeof import("pg")> {
10
+ if (pgModule) return pgModule;
11
+ let mod: { default?: typeof import("pg") } & typeof import("pg");
12
+ try {
13
+ mod = (await import("pg")) as never;
14
+ } catch {
15
+ throw new Error("PostgresStore requires the 'pg' package — install it (e.g. `npm i pg`)");
16
+ }
17
+ const pg = (mod.default ?? mod) as typeof import("pg");
18
+ // Postgres returns bigint (int8, OID 20) as a string to avoid precision loss.
19
+ // Every cairnq bigint is an epoch-ms or a counter, all within Number's safe
20
+ // integer range, so parse to number once (globally) to match the Task model
21
+ // (*_ms typed as number, same as the SQLite SDK). Set before any query runs.
22
+ pg.types.setTypeParser(pg.types.builtins.INT8, (v: string) => (v == null ? null : Number(v)));
23
+ pgModule = pg;
24
+ return pg;
25
+ }
26
+
27
+ /**
28
+ * Roll back on the way out of a failed transaction, without letting the rollback
29
+ * become the error the caller sees. A dropped connection fails both the statement
30
+ * and the rollback, and it is the first one that says what went wrong.
31
+ */
32
+ async function rollbackQuietly(client: PG.PoolClient): Promise<void> {
33
+ try {
34
+ await client.query("rollback");
35
+ } catch {
36
+ // Already rolled back, or the connection is gone. Either way the original
37
+ // error is the one worth propagating.
38
+ }
39
+ }
40
+
41
+ /** The PgSession face of one pg client or pool. */
42
+ function session(q: Pick<PG.PoolClient, "query">): PgSession {
43
+ return {
44
+ async query(text: string, values: readonly unknown[]): Promise<Row[]> {
45
+ return (await q.query(text, values as unknown[])).rows;
46
+ },
47
+ async exec(sql: string): Promise<void> {
48
+ // No values: pg sends this over the simple query protocol, which is what
49
+ // makes a multi-statement migration script legal here and not in query().
50
+ await q.query(sql);
51
+ },
52
+ };
53
+ }
54
+
55
+ /**
56
+ * A Postgres identifier that is safe to interpolate — cairnq quotes the schema
57
+ * name, and a name that could close that quote could rewrite the statement.
58
+ * Deliberately narrower than what Postgres accepts: a schema cairnq is asked to
59
+ * live in is a deployment decision, not a place to be clever.
60
+ */
61
+ const PLAIN_IDENT = /^[A-Za-z_][A-Za-z0-9_$]*$/;
62
+
63
+ /**
64
+ * The built-in executor: a `pg.Pool` over a libpq DSN. What `CairnQ.postgres(dsn)`
65
+ * uses, and the reference for what an adapter over another driver must do.
66
+ *
67
+ * Creating it does not connect — `pg.Pool` is lazy, and the store's first
68
+ * statement (the migration ledger) is what proves the database is reachable.
69
+ *
70
+ * `schema` puts cairnq's tables in a schema of their own rather than in whatever
71
+ * the connection's search_path leads with. The protocol's SQL names no schema, so
72
+ * this is a per-connection `search_path` and not one statement changes.
73
+ */
74
+ export async function createPoolExecutor(
75
+ dsn: string,
76
+ opts: { max?: number; schema?: string } = {},
77
+ ): Promise<PgExecutor> {
78
+ const pg = await loadPg();
79
+ const schema = opts.schema;
80
+ if (schema !== undefined && !PLAIN_IDENT.test(schema)) {
81
+ throw new Error(
82
+ `schema must be a plain identifier (letters, digits, _ and $, not starting ` +
83
+ `with a digit), got ${JSON.stringify(schema)}`,
84
+ );
85
+ }
86
+ const pool = new pg.Pool({ connectionString: dsn, max: opts.max });
87
+ if (schema) {
88
+ // Queued on the connection before it is handed out, so every statement this
89
+ // pool ever runs — migrations included — resolves in the right schema. Pooled
90
+ // connections come and go, which is why this is per-connection rather than a
91
+ // one-off at startup.
92
+ pool.on("connect", (client) => {
93
+ void client.query(`set search_path to "${schema}"`);
94
+ });
95
+ }
96
+ if (schema) {
97
+ // Created once, here, rather than from the connect handler (which would ask
98
+ // for CREATE privilege on every new connection) or from the migrations
99
+ // (which name no schema, by design). Without it the first `create table`
100
+ // fails with "no schema has been selected to create in", which says nothing
101
+ // about the cause. This is the one thing that makes building the executor
102
+ // connect; without `schema` it stays lazy.
103
+ try {
104
+ await pool.query(`create schema if not exists "${schema}"`);
105
+ } catch (e) {
106
+ await pool.end();
107
+ throw e;
108
+ }
109
+ }
110
+ const poolSession = session(pool);
111
+
112
+ return {
113
+ query: poolSession.query,
114
+ exec: poolSession.exec,
115
+
116
+ async tx<T>(fn: (s: PgSession) => Promise<T>): Promise<T> {
117
+ const client = await pool.connect();
118
+ try {
119
+ await client.query("begin");
120
+ const out = await fn(session(client));
121
+ await client.query("commit");
122
+ return out;
123
+ } catch (e) {
124
+ await rollbackQuietly(client);
125
+ throw e;
126
+ } finally {
127
+ client.release();
128
+ }
129
+ },
130
+
131
+ async listen(channels, onNotify, onClose): Promise<() => void> {
132
+ // Built from the raw DSN rather than taken from the pool: a listener holds
133
+ // its connection for its whole life, and a pooled one would be a slot the
134
+ // store never gives back. If `opts` ever grows connection-level settings
135
+ // (ssl, application_name), the listener must receive them too.
136
+ const client = new pg.Client({ connectionString: dsn });
137
+ await client.connect(); // failure here is transient: caller retries with backoff
138
+ client.on("notification", (msg) => onNotify(msg.channel, msg.payload));
139
+ // A dropped listener degrades to polling; the store reconnects on the next wake.
140
+ client.on("error", () => onClose());
141
+ try {
142
+ await client.query(channels.map((c) => `listen ${c}`).join("; "));
143
+ } catch (e) {
144
+ void client.end().catch(() => {});
145
+ // Connected, but LISTEN was refused (e.g. a transaction-mode pooler) —
146
+ // deterministic, so tell the store not to retry.
147
+ throw new ListenUnavailable(e instanceof Error ? e.message : undefined);
148
+ }
149
+ return () => void client.end().catch(() => {});
150
+ },
151
+
152
+ async close(): Promise<void> {
153
+ await pool.end();
154
+ },
155
+ };
156
+ }