@portqueue/core 1.2.1 → 1.2.3

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.
Files changed (53) hide show
  1. package/dist/conformance/index.cjs +82 -2
  2. package/dist/conformance/index.cjs.map +1 -1
  3. package/dist/conformance/index.mjs +83 -3
  4. package/dist/conformance/index.mjs.map +1 -1
  5. package/dist/consumer/completion-batcher.d.ts +29 -0
  6. package/dist/consumer/completion.service.d.ts +5 -3
  7. package/dist/consumer/execution.service.d.ts +7 -2
  8. package/dist/consumer/worker.d.ts +16 -0
  9. package/dist/drivers/driver.interface.d.ts +26 -2
  10. package/dist/drivers/mysql/index.cjs +50 -3
  11. package/dist/drivers/mysql/index.cjs.map +1 -1
  12. package/dist/drivers/mysql/index.mjs +50 -3
  13. package/dist/drivers/mysql/index.mjs.map +1 -1
  14. package/dist/drivers/mysql/mysql-driver.d.ts +16 -3
  15. package/dist/drivers/oracle/index.cjs +50 -3
  16. package/dist/drivers/oracle/index.cjs.map +1 -1
  17. package/dist/drivers/oracle/index.mjs +50 -3
  18. package/dist/drivers/oracle/index.mjs.map +1 -1
  19. package/dist/drivers/oracle/oracle-driver.d.ts +14 -3
  20. package/dist/drivers/pglite/index.cjs +6 -3
  21. package/dist/drivers/pglite/index.cjs.map +1 -1
  22. package/dist/drivers/pglite/index.mjs +6 -3
  23. package/dist/drivers/pglite/index.mjs.map +1 -1
  24. package/dist/drivers/pglite/migrations.d.ts +2 -3
  25. package/dist/drivers/postgresql/index.cjs +1 -1
  26. package/dist/drivers/postgresql/index.mjs +1 -1
  27. package/dist/drivers/postgresql/migrations.d.ts +21 -0
  28. package/dist/drivers/postgresql/postgresql-driver.d.ts +28 -3
  29. package/dist/drivers/shared/base-driver.d.ts +28 -2
  30. package/dist/drivers/sqlite/index.cjs +36 -4
  31. package/dist/drivers/sqlite/index.cjs.map +1 -1
  32. package/dist/drivers/sqlite/index.mjs +36 -4
  33. package/dist/drivers/sqlite/index.mjs.map +1 -1
  34. package/dist/drivers/sqlite/sqlite-driver.d.ts +14 -3
  35. package/dist/index.cjs +107 -10
  36. package/dist/index.cjs.map +1 -1
  37. package/dist/index.d.ts +2 -2
  38. package/dist/index.mjs +108 -11
  39. package/dist/index.mjs.map +1 -1
  40. package/dist/jobs/job-record.d.ts +8 -0
  41. package/dist/{migration-Dx8eUthW.cjs → migration-CX7j4wVB.cjs} +59 -1
  42. package/dist/migration-CX7j4wVB.cjs.map +1 -0
  43. package/dist/{migration-D0IRP2oq.js → migration-VkNQT3a_.js} +60 -2
  44. package/dist/migration-VkNQT3a_.js.map +1 -0
  45. package/dist/{postgresql-driver-CPHv5SUT.js → postgresql-driver-DQRzg2NA.js} +179 -57
  46. package/dist/postgresql-driver-DQRzg2NA.js.map +1 -0
  47. package/dist/{postgresql-driver-DRT68TKb.cjs → postgresql-driver-xfqy9mi8.cjs} +179 -57
  48. package/dist/postgresql-driver-xfqy9mi8.cjs.map +1 -0
  49. package/package.json +1 -1
  50. package/dist/migration-D0IRP2oq.js.map +0 -1
  51. package/dist/migration-Dx8eUthW.cjs.map +0 -1
  52. package/dist/postgresql-driver-CPHv5SUT.js.map +0 -1
  53. package/dist/postgresql-driver-DRT68TKb.cjs.map +0 -1
@@ -2,8 +2,8 @@ import { DatabaseEngine } from '../../options/enums';
2
2
  import { JobStatus } from '../../jobs/job-state.enum';
3
3
  import { DriverCapabilitySet } from '../driver-capabilities';
4
4
  import { BaseDriver, RawResult, Session } from '../shared/base-driver';
5
- import { Driver, DriverProbe, InstanceListFilter, JobListFilter, JobRef, MigrationStep, PageWindow, QueueListFilter, RequeuedJobRef, ScheduleListFilter, ScheduleRef, StalledJobRef, Subscription } from '../driver.interface';
6
- import { InsertedJobRow, InstanceMetrics, InstanceRow, JobRecord, NewJobRecord, NewScheduleRow, QueueRow, ScheduleRow } from '../../jobs/job-record';
5
+ import { CompletedJobInput, Driver, DriverProbe, InstanceListFilter, JobListFilter, JobRef, MigrationStep, PageWindow, QueueListFilter, RequeuedJobRef, ScheduleListFilter, ScheduleRef, StalledJobRef, Subscription } from '../driver.interface';
6
+ import { ClaimedJob, InsertedJobRow, InstanceMetrics, InstanceRow, JobRecord, NewJobRecord, NewScheduleRow, QueueRow, ScheduleRow } from '../../jobs/job-record';
7
7
  import { QueueOptions } from '../../options/queue-options';
8
8
  import { TransactionHandle } from '../../tx/transaction-handle';
9
9
  export interface OracleDriverOptions {
@@ -175,7 +175,7 @@ export declare class OracleDriver extends BaseDriver implements Driver {
175
175
  lockDurationSeconds: number;
176
176
  now: number;
177
177
  instanceKey?: string | null;
178
- }): Promise<JobRecord[]>;
178
+ }): Promise<ClaimedJob[]>;
179
179
  /**
180
180
  * The fence a completion is guarded on: the row must still be `ACTIVE` and, when the caller names
181
181
  * the claim it is settling, still carry that claim's token.
@@ -199,6 +199,17 @@ export declare class OracleDriver extends BaseDriver implements Driver {
199
199
  lockToken?: string | null;
200
200
  tx?: TransactionHandle;
201
201
  }): Promise<boolean>;
202
+ /**
203
+ * Settle a batch per claim token (and per id chunk). Without `RETURNING`, the rows that still pass
204
+ * the fence (`ACTIVE` and the group's token, exactly like {@link completeJob}) are locked first
205
+ * with `SELECT … FOR UPDATE`, then exactly those are updated — both inside one transaction, so the
206
+ * ids returned are the ids settled. Parameters are pushed in placeholder order.
207
+ */
208
+ completeJobs(options: {
209
+ jobs: CompletedJobInput[];
210
+ now: number;
211
+ tx?: TransactionHandle;
212
+ }): Promise<string[]>;
202
213
  retryJob(options: {
203
214
  jobId: string;
204
215
  nextRunAt: number;
@@ -23,9 +23,12 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
23
23
  ));
24
24
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
25
25
  const storage_constants = require("../../storage.constants-BKeXcSQe.cjs");
26
- const migration = require("../../migration-Dx8eUthW.cjs");
27
- const postgresqlDriver = require("../../postgresql-driver-DRT68TKb.cjs");
28
- const PGLITE_MIGRATIONS = [{ version: 1, statements: postgresqlDriver.v1 }];
26
+ const migration = require("../../migration-CX7j4wVB.cjs");
27
+ const postgresqlDriver = require("../../postgresql-driver-xfqy9mi8.cjs");
28
+ const PGLITE_MIGRATIONS = [
29
+ { version: 1, statements: postgresqlDriver.v1 },
30
+ { version: 2, statements: postgresqlDriver.v2 }
31
+ ];
29
32
  class PgliteDriver extends postgresqlDriver.PostgresqlDriver {
30
33
  engine = storage_constants.DatabaseEngine.PGLITE;
31
34
  /** Same as PostgreSQL minus LISTEN/NOTIFY and advisory locks — neither is meaningful in-process. */
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs","sources":["../../../src/drivers/pglite/migrations.ts","../../../src/drivers/pglite/pglite-driver.ts"],"sourcesContent":["import type { FlavourMigration } from '../shared/migration';\n// --- Compatible with PostgreSQL: reused verbatim ---\n// PGLite is PostgreSQL compiled to WASM, so every version below emits byte-for-byte the DDL the\n// PostgreSQL flavour emits. Rather than keep a second copy in sync by hand, the statement builders\n// are imported from the PostgreSQL flavour and referenced here.\nimport { v1 as postgresqlV1 } from '../postgresql/migrations';\n\n/**\n * Predefined PGLite DDL, version by version. Authored by hand for this engine; not generated at\n * runtime.\n *\n * `PGLITE_MIGRATIONS` is PGLite's own array — the runner only ever reads this one — but each entry\n * points at the PostgreSQL statement builder because the two dialects are identical, keys, partial\n * indexes and `CREATE SCHEMA IF NOT EXISTS` included. There is a single version so far; a future\n * change appends `{ version: 2, ... }` rather than editing v1, which is a frozen record of what has\n * already been applied.\n *\n * --- Custom to PGLite ---\n * Nothing yet. When a version needs to diverge (e.g. dropping a clause the WASM build rejects),\n * author a local `v<n>(names: MigrationNames): string[]` here (`MigrationNames` comes from\n * `../shared/migration`) and swap it into the array for that version only; every other version\n * keeps reusing PostgreSQL. Spell its column names as raw string literals, never the `*Column` enums\n * — renaming an enum member must never retroactively rewrite a past migration's DDL.\n */\n\nexport const PGLITE_MIGRATIONS: FlavourMigration[] = [{ version: 1, statements: postgresqlV1 }];\n","import { DatabaseEngine } from '../../options/enums';\nimport { capabilities, DriverCapability, type DriverCapabilitySet } from '../driver-capabilities';\nimport {\n DriverConnectionError,\n MissingPeerDependencyError,\n UnsupportedCapabilityError,\n} from '../../errors/errors';\nimport { toMigrationSteps } from '../shared/migration';\nimport { PGLITE_MIGRATIONS } from './migrations';\nimport type { Driver, DriverProbe, MigrationStep, Subscription } from '../driver.interface';\n// --- Compatible with PostgreSQL: the whole SQL surface is inherited ---\n// PGLite is PostgreSQL compiled to WASM and speaks the same wire protocol and dialect, so every\n// query method (jobs, queues, schedules, instances, claiming, retention, paging, the tx handle)\n// comes from `PostgresqlDriver` unchanged. `RawResult`/`Session` are its own connection-level types,\n// imported so the overrides below stay signature-compatible.\nimport { PostgresqlDriver, type RawResult, type Session } from '../postgresql/postgresql-driver';\n\n/**\n * Options for {@link PgliteDriver} — entirely custom to PGLite: there is no host, port, user or\n * pool, only an optional on-disk directory. `schema`/`tablePrefix`/`namespace` mean exactly what\n * they mean for PostgreSQL and are forwarded to the base driver's `DriverContext`.\n */\nexport interface PGliteDriverOptions {\n /** Data directory for a file-backed DB; omit for in-memory. */\n dataDir?: string;\n /** DB schema for all objects. Created by the migration and used to qualify every table name. */\n schema?: string;\n /** Prefix for all created tables (default 'pq_'). */\n tablePrefix?: string;\n /** Logical prefix for pub/sub channels when multiple PortQueues share one DB. */\n namespace?: string;\n}\n\n// --- Custom to PGLite: the client shape ---\n// The `@electric-sql/pglite` client is nothing like `pg` — one in-process instance instead of a\n// pool, a callback-style `transaction()` instead of BEGIN/COMMIT on a checked-out connection, and\n// `affectedRows` instead of `rowCount`.\nexport interface PGliteQueryable {\n query(\n sql: string,\n params?: readonly unknown[],\n ): Promise<{ rows: Array<Record<string, unknown>>; affectedRows?: number }>;\n}\nexport interface PGliteInstance extends PGliteQueryable {\n transaction<T>(fn: (tx: PGliteQueryable) => Promise<T>): Promise<T>;\n close(): Promise<void>;\n}\n\n/**\n * PGLite driver — PostgreSQL compiled to WASM, running in-process. Primarily for embedded/edge use\n * and tests.\n *\n * Because PGLite is byte-for-byte PG-wire compatible, this class **extends `PostgresqlDriver`** and\n * inherits its entire SQL surface: `$1`-style placeholders, SKIP LOCKED claiming, RETURNING\n * everywhere, the keys and indexes its migration creates, no relational JOIN (relationships resolve\n * by name/key lookups), and the in-code enforcement of idempotency/singleton dedup. A change to a\n * query in `PostgresqlDriver` therefore changes PGLite too; anything that must differ is overridden\n * here.\n *\n * What is custom to PGLite and overridden below:\n * - `engine`/`capabilities`/`migrations` — its own identity, capability set and migration array.\n * - `connect`/`disconnect` — boots one in-process WASM instance from `@electric-sql/pglite`\n * (an optional peer) instead of opening a `pg.Pool`.\n * - `execute`/`transaction` — talks to that instance and uses its callback-style `transaction()`.\n * - `probe` — no `SHOW server_version` round-trip; the version is the fixed string `'pglite'`.\n * - `notify`/`subscribe` — LISTEN/NOTIFY is off: PGLite is in-process with no shared bus, so the\n * interval poller is the backstop.\n * - `tryAdvisoryLock`/`releaseAdvisoryLock` — inert, since a single in-process DB has nothing to\n * coordinate with.\n */\nexport class PgliteDriver extends PostgresqlDriver implements Driver {\n override readonly engine: DatabaseEngine = DatabaseEngine.PGLITE;\n /** Same as PostgreSQL minus LISTEN/NOTIFY and advisory locks — neither is meaningful in-process. */\n override readonly capabilities: DriverCapabilitySet = capabilities({\n [DriverCapability.SKIP_LOCKED]: true,\n [DriverCapability.RETURNING]: true,\n [DriverCapability.TRANSACTIONS]: true,\n });\n override readonly migrations: MigrationStep[] = toMigrationSteps(PGLITE_MIGRATIONS);\n\n private db: PGliteInstance | null = null;\n private readonly dataDir: string | undefined;\n\n constructor(options: PGliteDriverOptions = {}) {\n // Forward only the naming options; PGLite has no connection string, pool or pg config.\n super({\n ...(options.tablePrefix !== undefined ? { tablePrefix: options.tablePrefix } : {}),\n ...(options.schema !== undefined ? { schema: options.schema } : {}),\n ...(options.namespace !== undefined ? { namespace: options.namespace } : {}),\n });\n this.dataDir = options.dataDir;\n }\n\n // --- Connection management (custom to PGLite) ---\n override async connect(): Promise<void> {\n let mod: { PGlite: new (dataDir?: string) => PGliteInstance };\n try {\n mod = (await import('@electric-sql/pglite')) as unknown as {\n PGlite: new (dataDir?: string) => PGliteInstance;\n };\n } catch {\n throw new MissingPeerDependencyError('@electric-sql/pglite', 'PGLite');\n }\n try {\n this.db = this.dataDir ? new mod.PGlite(this.dataDir) : new mod.PGlite();\n // Force the WASM instance to finish booting with a trivial query.\n await this.db.query('SELECT 1');\n } catch (err) {\n throw new DriverConnectionError('Failed to initialize PGLite.', err);\n }\n }\n\n override async disconnect(): Promise<void> {\n await this.db?.close();\n this.db = null;\n }\n\n protected override async execute(\n sql: string,\n params: readonly unknown[],\n session?: Session,\n ): Promise<RawResult> {\n const target = (session as PGliteQueryable | undefined) ?? this.db;\n if (!target) throw new DriverConnectionError('PGLite driver is not connected.');\n const res = await target.query(sql, params);\n // PGLite reports mutations as `affectedRows`, not `rowCount`.\n return { rows: res.rows ?? [], rowCount: res.affectedRows ?? res.rows?.length ?? 0 };\n }\n\n protected override async transaction<T>(fn: (session: Session) => Promise<T>): Promise<T> {\n if (!this.db) throw new DriverConnectionError('PGLite driver is not connected.');\n // The client owns BEGIN/COMMIT/ROLLBACK — no connection to check out and release.\n return this.db.transaction((tx) => fn(tx));\n }\n\n // --- Probe (custom to PGLite: no server to ask for a version) ---\n override async probe(): Promise<DriverProbe> {\n return { serverVersion: 'pglite', verifiedCapabilities: this.enabledCapabilities() };\n }\n\n // --- Pub/sub (custom to PGLite: LISTEN/NOTIFY disabled — an interval poll is the backstop) ---\n // No-op: PGLite runs in-process without a shared LISTEN/NOTIFY bus; the poller drives progress.\n override async notify(_options: { channel: string; payload: string }): Promise<void> {}\n\n override async subscribe(_options: {\n channel: string;\n onNotification: (payload: string) => void;\n }): Promise<Subscription> {\n throw new UnsupportedCapabilityError(\n DriverCapability.LISTEN_NOTIFY,\n 'PGLite has no shared notification bus; rely on interval polling instead.',\n );\n }\n\n // --- Advisory locks (custom to PGLite: inert) ---\n // A single in-process DB has no second process to coordinate with, so the migration runner takes\n // no lock — it skips these entirely because ADVISORY_LOCK is off in `capabilities`. They stay\n // declared, and inert, so PostgreSQL' pool-backed implementations can never be inherited.\n override async tryAdvisoryLock(_options: { key: string }): Promise<boolean> {\n return false;\n }\n\n override async releaseAdvisoryLock(_options: { key: string }): Promise<void> {}\n}\n"],"names":["postgresqlV1","PostgresqlDriver","DatabaseEngine","capabilities","DriverCapability","toMigrationSteps","MissingPeerDependencyError","DriverConnectionError","UnsupportedCapabilityError"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;AAyBO,MAAM,oBAAwC,CAAC,EAAE,SAAS,GAAG,YAAYA,iBAAAA,IAAc;AC6CvF,MAAM,qBAAqBC,iBAAAA,iBAAmC;AAAA,EACjD,SAAyBC,kBAAAA,eAAe;AAAA;AAAA,EAExC,eAAoCC,kBAAAA,aAAa;AAAA,IACjE,CAACC,kBAAAA,iBAAiB,WAAW,GAAG;AAAA,IAChC,CAACA,kBAAAA,iBAAiB,SAAS,GAAG;AAAA,IAC9B,CAACA,kBAAAA,iBAAiB,YAAY,GAAG;AAAA,EAAA,CAClC;AAAA,EACiB,aAA8BC,UAAAA,iBAAiB,iBAAiB;AAAA,EAE1E,KAA4B;AAAA,EACnB;AAAA,EAEjB,YAAY,UAA+B,IAAI;AAE7C,UAAM;AAAA,MACJ,GAAI,QAAQ,gBAAgB,SAAY,EAAE,aAAa,QAAQ,YAAA,IAAgB,CAAA;AAAA,MAC/E,GAAI,QAAQ,WAAW,SAAY,EAAE,QAAQ,QAAQ,OAAA,IAAW,CAAA;AAAA,MAChE,GAAI,QAAQ,cAAc,SAAY,EAAE,WAAW,QAAQ,cAAc,CAAA;AAAA,IAAC,CAC3E;AACD,SAAK,UAAU,QAAQ;AAAA,EACzB;AAAA;AAAA,EAGA,MAAe,UAAyB;AACtC,QAAI;AACJ,QAAI;AACF,YAAO,MAAM,OAAO,sBAAsB;AAAA,IAG5C,QAAQ;AACN,YAAM,IAAIC,UAAAA,2BAA2B,wBAAwB,QAAQ;AAAA,IACvE;AACA,QAAI;AACF,WAAK,KAAK,KAAK,UAAU,IAAI,IAAI,OAAO,KAAK,OAAO,IAAI,IAAI,IAAI,OAAA;AAEhE,YAAM,KAAK,GAAG,MAAM,UAAU;AAAA,IAChC,SAAS,KAAK;AACZ,YAAM,IAAIC,UAAAA,sBAAsB,gCAAgC,GAAG;AAAA,IACrE;AAAA,EACF;AAAA,EAEA,MAAe,aAA4B;AACzC,UAAM,KAAK,IAAI,MAAA;AACf,SAAK,KAAK;AAAA,EACZ;AAAA,EAEA,MAAyB,QACvB,KACA,QACA,SACoB;AACpB,UAAM,SAAU,WAA2C,KAAK;AAChE,QAAI,CAAC,OAAQ,OAAM,IAAIA,UAAAA,sBAAsB,iCAAiC;AAC9E,UAAM,MAAM,MAAM,OAAO,MAAM,KAAK,MAAM;AAE1C,WAAO,EAAE,MAAM,IAAI,QAAQ,CAAA,GAAI,UAAU,IAAI,gBAAgB,IAAI,MAAM,UAAU,EAAA;AAAA,EACnF;AAAA,EAEA,MAAyB,YAAe,IAAkD;AACxF,QAAI,CAAC,KAAK,GAAI,OAAM,IAAIA,UAAAA,sBAAsB,iCAAiC;AAE/E,WAAO,KAAK,GAAG,YAAY,CAAC,OAAO,GAAG,EAAE,CAAC;AAAA,EAC3C;AAAA;AAAA,EAGA,MAAe,QAA8B;AAC3C,WAAO,EAAE,eAAe,UAAU,sBAAsB,KAAK,sBAAoB;AAAA,EACnF;AAAA;AAAA;AAAA,EAIA,MAAe,OAAO,UAA+D;AAAA,EAAC;AAAA,EAEtF,MAAe,UAAU,UAGC;AACxB,UAAM,IAAIC,UAAAA;AAAAA,MACRJ,kBAAAA,iBAAiB;AAAA,MACjB;AAAA,IAAA;AAAA,EAEJ;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,MAAe,gBAAgB,UAA6C;AAC1E,WAAO;AAAA,EACT;AAAA,EAEA,MAAe,oBAAoB,UAA0C;AAAA,EAAC;AAChF;;"}
1
+ {"version":3,"file":"index.cjs","sources":["../../../src/drivers/pglite/migrations.ts","../../../src/drivers/pglite/pglite-driver.ts"],"sourcesContent":["import type { FlavourMigration } from '../shared/migration';\n// --- Compatible with PostgreSQL: reused verbatim ---\n// PGLite is PostgreSQL compiled to WASM, so every version below emits byte-for-byte the DDL the\n// PostgreSQL flavour emits. Rather than keep a second copy in sync by hand, the statement builders\n// are imported from the PostgreSQL flavour and referenced here.\nimport { v1 as postgresqlV1, v2 as postgresqlV2 } from '../postgresql/migrations';\n\n/**\n * Predefined PGLite DDL, version by version. Authored by hand for this engine; not generated at\n * runtime.\n *\n * `PGLITE_MIGRATIONS` is PGLite's own array — the runner only ever reads this one — but each entry\n * points at the PostgreSQL statement builder because the two dialects are identical, keys, partial\n * indexes and `CREATE SCHEMA IF NOT EXISTS` included. A new version is appended, never edited\n * into an earlier one, which is a frozen record of what has already been applied.\n *\n * --- Custom to PGLite ---\n * Nothing yet. When a version needs to diverge (e.g. dropping a clause the WASM build rejects),\n * author a local `v<n>(names: MigrationNames): string[]` here (`MigrationNames` comes from\n * `../shared/migration`) and swap it into the array for that version only; every other version\n * keeps reusing PostgreSQL. Spell its column names as raw string literals, never the `*Column` enums\n * — renaming an enum member must never retroactively rewrite a past migration's DDL.\n */\n\nexport const PGLITE_MIGRATIONS: FlavourMigration[] = [\n { version: 1, statements: postgresqlV1 },\n { version: 2, statements: postgresqlV2 },\n];\n","import { DatabaseEngine } from '../../options/enums';\nimport { capabilities, DriverCapability, type DriverCapabilitySet } from '../driver-capabilities';\nimport {\n DriverConnectionError,\n MissingPeerDependencyError,\n UnsupportedCapabilityError,\n} from '../../errors/errors';\nimport { toMigrationSteps } from '../shared/migration';\nimport { PGLITE_MIGRATIONS } from './migrations';\nimport type { Driver, DriverProbe, MigrationStep, Subscription } from '../driver.interface';\n// --- Compatible with PostgreSQL: the whole SQL surface is inherited ---\n// PGLite is PostgreSQL compiled to WASM and speaks the same wire protocol and dialect, so every\n// query method (jobs, queues, schedules, instances, claiming, retention, paging, the tx handle)\n// comes from `PostgresqlDriver` unchanged. `RawResult`/`Session` are its own connection-level types,\n// imported so the overrides below stay signature-compatible.\nimport { PostgresqlDriver, type RawResult, type Session } from '../postgresql/postgresql-driver';\n\n/**\n * Options for {@link PgliteDriver} — entirely custom to PGLite: there is no host, port, user or\n * pool, only an optional on-disk directory. `schema`/`tablePrefix`/`namespace` mean exactly what\n * they mean for PostgreSQL and are forwarded to the base driver's `DriverContext`.\n */\nexport interface PGliteDriverOptions {\n /** Data directory for a file-backed DB; omit for in-memory. */\n dataDir?: string;\n /** DB schema for all objects. Created by the migration and used to qualify every table name. */\n schema?: string;\n /** Prefix for all created tables (default 'pq_'). */\n tablePrefix?: string;\n /** Logical prefix for pub/sub channels when multiple PortQueues share one DB. */\n namespace?: string;\n}\n\n// --- Custom to PGLite: the client shape ---\n// The `@electric-sql/pglite` client is nothing like `pg` — one in-process instance instead of a\n// pool, a callback-style `transaction()` instead of BEGIN/COMMIT on a checked-out connection, and\n// `affectedRows` instead of `rowCount`.\nexport interface PGliteQueryable {\n query(\n sql: string,\n params?: readonly unknown[],\n ): Promise<{ rows: Array<Record<string, unknown>>; affectedRows?: number }>;\n}\nexport interface PGliteInstance extends PGliteQueryable {\n transaction<T>(fn: (tx: PGliteQueryable) => Promise<T>): Promise<T>;\n close(): Promise<void>;\n}\n\n/**\n * PGLite driver — PostgreSQL compiled to WASM, running in-process. Primarily for embedded/edge use\n * and tests.\n *\n * Because PGLite is byte-for-byte PG-wire compatible, this class **extends `PostgresqlDriver`** and\n * inherits its entire SQL surface: `$1`-style placeholders, SKIP LOCKED claiming, RETURNING\n * everywhere, the keys and indexes its migration creates, no relational JOIN (relationships resolve\n * by name/key lookups), and the in-code enforcement of idempotency/singleton dedup. A change to a\n * query in `PostgresqlDriver` therefore changes PGLite too; anything that must differ is overridden\n * here.\n *\n * What is custom to PGLite and overridden below:\n * - `engine`/`capabilities`/`migrations` — its own identity, capability set and migration array.\n * - `connect`/`disconnect` — boots one in-process WASM instance from `@electric-sql/pglite`\n * (an optional peer) instead of opening a `pg.Pool`.\n * - `execute`/`transaction` — talks to that instance and uses its callback-style `transaction()`.\n * - `probe` — no `SHOW server_version` round-trip; the version is the fixed string `'pglite'`.\n * - `notify`/`subscribe` — LISTEN/NOTIFY is off: PGLite is in-process with no shared bus, so the\n * interval poller is the backstop.\n * - `tryAdvisoryLock`/`releaseAdvisoryLock` — inert, since a single in-process DB has nothing to\n * coordinate with.\n */\nexport class PgliteDriver extends PostgresqlDriver implements Driver {\n override readonly engine: DatabaseEngine = DatabaseEngine.PGLITE;\n /** Same as PostgreSQL minus LISTEN/NOTIFY and advisory locks — neither is meaningful in-process. */\n override readonly capabilities: DriverCapabilitySet = capabilities({\n [DriverCapability.SKIP_LOCKED]: true,\n [DriverCapability.RETURNING]: true,\n [DriverCapability.TRANSACTIONS]: true,\n });\n override readonly migrations: MigrationStep[] = toMigrationSteps(PGLITE_MIGRATIONS);\n\n private db: PGliteInstance | null = null;\n private readonly dataDir: string | undefined;\n\n constructor(options: PGliteDriverOptions = {}) {\n // Forward only the naming options; PGLite has no connection string, pool or pg config.\n super({\n ...(options.tablePrefix !== undefined ? { tablePrefix: options.tablePrefix } : {}),\n ...(options.schema !== undefined ? { schema: options.schema } : {}),\n ...(options.namespace !== undefined ? { namespace: options.namespace } : {}),\n });\n this.dataDir = options.dataDir;\n }\n\n // --- Connection management (custom to PGLite) ---\n override async connect(): Promise<void> {\n let mod: { PGlite: new (dataDir?: string) => PGliteInstance };\n try {\n mod = (await import('@electric-sql/pglite')) as unknown as {\n PGlite: new (dataDir?: string) => PGliteInstance;\n };\n } catch {\n throw new MissingPeerDependencyError('@electric-sql/pglite', 'PGLite');\n }\n try {\n this.db = this.dataDir ? new mod.PGlite(this.dataDir) : new mod.PGlite();\n // Force the WASM instance to finish booting with a trivial query.\n await this.db.query('SELECT 1');\n } catch (err) {\n throw new DriverConnectionError('Failed to initialize PGLite.', err);\n }\n }\n\n override async disconnect(): Promise<void> {\n await this.db?.close();\n this.db = null;\n }\n\n protected override async execute(\n sql: string,\n params: readonly unknown[],\n session?: Session,\n ): Promise<RawResult> {\n const target = (session as PGliteQueryable | undefined) ?? this.db;\n if (!target) throw new DriverConnectionError('PGLite driver is not connected.');\n const res = await target.query(sql, params);\n // PGLite reports mutations as `affectedRows`, not `rowCount`.\n return { rows: res.rows ?? [], rowCount: res.affectedRows ?? res.rows?.length ?? 0 };\n }\n\n protected override async transaction<T>(fn: (session: Session) => Promise<T>): Promise<T> {\n if (!this.db) throw new DriverConnectionError('PGLite driver is not connected.');\n // The client owns BEGIN/COMMIT/ROLLBACK — no connection to check out and release.\n return this.db.transaction((tx) => fn(tx));\n }\n\n // --- Probe (custom to PGLite: no server to ask for a version) ---\n override async probe(): Promise<DriverProbe> {\n return { serverVersion: 'pglite', verifiedCapabilities: this.enabledCapabilities() };\n }\n\n // --- Pub/sub (custom to PGLite: LISTEN/NOTIFY disabled — an interval poll is the backstop) ---\n // No-op: PGLite runs in-process without a shared LISTEN/NOTIFY bus; the poller drives progress.\n override async notify(_options: { channel: string; payload: string }): Promise<void> {}\n\n override async subscribe(_options: {\n channel: string;\n onNotification: (payload: string) => void;\n }): Promise<Subscription> {\n throw new UnsupportedCapabilityError(\n DriverCapability.LISTEN_NOTIFY,\n 'PGLite has no shared notification bus; rely on interval polling instead.',\n );\n }\n\n // --- Advisory locks (custom to PGLite: inert) ---\n // A single in-process DB has no second process to coordinate with, so the migration runner takes\n // no lock — it skips these entirely because ADVISORY_LOCK is off in `capabilities`. They stay\n // declared, and inert, so PostgreSQL' pool-backed implementations can never be inherited.\n override async tryAdvisoryLock(_options: { key: string }): Promise<boolean> {\n return false;\n }\n\n override async releaseAdvisoryLock(_options: { key: string }): Promise<void> {}\n}\n"],"names":["postgresqlV1","postgresqlV2","PostgresqlDriver","DatabaseEngine","capabilities","DriverCapability","toMigrationSteps","MissingPeerDependencyError","DriverConnectionError","UnsupportedCapabilityError"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;AAwBO,MAAM,oBAAwC;AAAA,EACnD,EAAE,SAAS,GAAG,YAAYA,oBAAA;AAAA,EAC1B,EAAE,SAAS,GAAG,YAAYC,iBAAAA,GAAA;AAC5B;AC2CO,MAAM,qBAAqBC,iBAAAA,iBAAmC;AAAA,EACjD,SAAyBC,kBAAAA,eAAe;AAAA;AAAA,EAExC,eAAoCC,kBAAAA,aAAa;AAAA,IACjE,CAACC,kBAAAA,iBAAiB,WAAW,GAAG;AAAA,IAChC,CAACA,kBAAAA,iBAAiB,SAAS,GAAG;AAAA,IAC9B,CAACA,kBAAAA,iBAAiB,YAAY,GAAG;AAAA,EAAA,CAClC;AAAA,EACiB,aAA8BC,UAAAA,iBAAiB,iBAAiB;AAAA,EAE1E,KAA4B;AAAA,EACnB;AAAA,EAEjB,YAAY,UAA+B,IAAI;AAE7C,UAAM;AAAA,MACJ,GAAI,QAAQ,gBAAgB,SAAY,EAAE,aAAa,QAAQ,YAAA,IAAgB,CAAA;AAAA,MAC/E,GAAI,QAAQ,WAAW,SAAY,EAAE,QAAQ,QAAQ,OAAA,IAAW,CAAA;AAAA,MAChE,GAAI,QAAQ,cAAc,SAAY,EAAE,WAAW,QAAQ,cAAc,CAAA;AAAA,IAAC,CAC3E;AACD,SAAK,UAAU,QAAQ;AAAA,EACzB;AAAA;AAAA,EAGA,MAAe,UAAyB;AACtC,QAAI;AACJ,QAAI;AACF,YAAO,MAAM,OAAO,sBAAsB;AAAA,IAG5C,QAAQ;AACN,YAAM,IAAIC,UAAAA,2BAA2B,wBAAwB,QAAQ;AAAA,IACvE;AACA,QAAI;AACF,WAAK,KAAK,KAAK,UAAU,IAAI,IAAI,OAAO,KAAK,OAAO,IAAI,IAAI,IAAI,OAAA;AAEhE,YAAM,KAAK,GAAG,MAAM,UAAU;AAAA,IAChC,SAAS,KAAK;AACZ,YAAM,IAAIC,UAAAA,sBAAsB,gCAAgC,GAAG;AAAA,IACrE;AAAA,EACF;AAAA,EAEA,MAAe,aAA4B;AACzC,UAAM,KAAK,IAAI,MAAA;AACf,SAAK,KAAK;AAAA,EACZ;AAAA,EAEA,MAAyB,QACvB,KACA,QACA,SACoB;AACpB,UAAM,SAAU,WAA2C,KAAK;AAChE,QAAI,CAAC,OAAQ,OAAM,IAAIA,UAAAA,sBAAsB,iCAAiC;AAC9E,UAAM,MAAM,MAAM,OAAO,MAAM,KAAK,MAAM;AAE1C,WAAO,EAAE,MAAM,IAAI,QAAQ,CAAA,GAAI,UAAU,IAAI,gBAAgB,IAAI,MAAM,UAAU,EAAA;AAAA,EACnF;AAAA,EAEA,MAAyB,YAAe,IAAkD;AACxF,QAAI,CAAC,KAAK,GAAI,OAAM,IAAIA,UAAAA,sBAAsB,iCAAiC;AAE/E,WAAO,KAAK,GAAG,YAAY,CAAC,OAAO,GAAG,EAAE,CAAC;AAAA,EAC3C;AAAA;AAAA,EAGA,MAAe,QAA8B;AAC3C,WAAO,EAAE,eAAe,UAAU,sBAAsB,KAAK,sBAAoB;AAAA,EACnF;AAAA;AAAA;AAAA,EAIA,MAAe,OAAO,UAA+D;AAAA,EAAC;AAAA,EAEtF,MAAe,UAAU,UAGC;AACxB,UAAM,IAAIC,UAAAA;AAAAA,MACRJ,kBAAAA,iBAAiB;AAAA,MACjB;AAAA,IAAA;AAAA,EAEJ;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,MAAe,gBAAgB,UAA6C;AAC1E,WAAO;AAAA,EACT;AAAA,EAEA,MAAe,oBAAoB,UAA0C;AAAA,EAAC;AAChF;;"}
@@ -1,7 +1,10 @@
1
1
  import { a as DatabaseEngine, c as capabilities, D as DriverCapability } from "../../storage.constants-DYEQUmc8.js";
2
- import { t as toMigrationSteps, M as MissingPeerDependencyError, D as DriverConnectionError, U as UnsupportedCapabilityError } from "../../migration-D0IRP2oq.js";
3
- import { v as v1, P as PostgresqlDriver } from "../../postgresql-driver-CPHv5SUT.js";
4
- const PGLITE_MIGRATIONS = [{ version: 1, statements: v1 }];
2
+ import { t as toMigrationSteps, M as MissingPeerDependencyError, D as DriverConnectionError, U as UnsupportedCapabilityError } from "../../migration-VkNQT3a_.js";
3
+ import { v as v1, a as v2, P as PostgresqlDriver } from "../../postgresql-driver-DQRzg2NA.js";
4
+ const PGLITE_MIGRATIONS = [
5
+ { version: 1, statements: v1 },
6
+ { version: 2, statements: v2 }
7
+ ];
5
8
  class PgliteDriver extends PostgresqlDriver {
6
9
  engine = DatabaseEngine.PGLITE;
7
10
  /** Same as PostgreSQL minus LISTEN/NOTIFY and advisory locks — neither is meaningful in-process. */
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","sources":["../../../src/drivers/pglite/migrations.ts","../../../src/drivers/pglite/pglite-driver.ts"],"sourcesContent":["import type { FlavourMigration } from '../shared/migration';\n// --- Compatible with PostgreSQL: reused verbatim ---\n// PGLite is PostgreSQL compiled to WASM, so every version below emits byte-for-byte the DDL the\n// PostgreSQL flavour emits. Rather than keep a second copy in sync by hand, the statement builders\n// are imported from the PostgreSQL flavour and referenced here.\nimport { v1 as postgresqlV1 } from '../postgresql/migrations';\n\n/**\n * Predefined PGLite DDL, version by version. Authored by hand for this engine; not generated at\n * runtime.\n *\n * `PGLITE_MIGRATIONS` is PGLite's own array — the runner only ever reads this one — but each entry\n * points at the PostgreSQL statement builder because the two dialects are identical, keys, partial\n * indexes and `CREATE SCHEMA IF NOT EXISTS` included. There is a single version so far; a future\n * change appends `{ version: 2, ... }` rather than editing v1, which is a frozen record of what has\n * already been applied.\n *\n * --- Custom to PGLite ---\n * Nothing yet. When a version needs to diverge (e.g. dropping a clause the WASM build rejects),\n * author a local `v<n>(names: MigrationNames): string[]` here (`MigrationNames` comes from\n * `../shared/migration`) and swap it into the array for that version only; every other version\n * keeps reusing PostgreSQL. Spell its column names as raw string literals, never the `*Column` enums\n * — renaming an enum member must never retroactively rewrite a past migration's DDL.\n */\n\nexport const PGLITE_MIGRATIONS: FlavourMigration[] = [{ version: 1, statements: postgresqlV1 }];\n","import { DatabaseEngine } from '../../options/enums';\nimport { capabilities, DriverCapability, type DriverCapabilitySet } from '../driver-capabilities';\nimport {\n DriverConnectionError,\n MissingPeerDependencyError,\n UnsupportedCapabilityError,\n} from '../../errors/errors';\nimport { toMigrationSteps } from '../shared/migration';\nimport { PGLITE_MIGRATIONS } from './migrations';\nimport type { Driver, DriverProbe, MigrationStep, Subscription } from '../driver.interface';\n// --- Compatible with PostgreSQL: the whole SQL surface is inherited ---\n// PGLite is PostgreSQL compiled to WASM and speaks the same wire protocol and dialect, so every\n// query method (jobs, queues, schedules, instances, claiming, retention, paging, the tx handle)\n// comes from `PostgresqlDriver` unchanged. `RawResult`/`Session` are its own connection-level types,\n// imported so the overrides below stay signature-compatible.\nimport { PostgresqlDriver, type RawResult, type Session } from '../postgresql/postgresql-driver';\n\n/**\n * Options for {@link PgliteDriver} — entirely custom to PGLite: there is no host, port, user or\n * pool, only an optional on-disk directory. `schema`/`tablePrefix`/`namespace` mean exactly what\n * they mean for PostgreSQL and are forwarded to the base driver's `DriverContext`.\n */\nexport interface PGliteDriverOptions {\n /** Data directory for a file-backed DB; omit for in-memory. */\n dataDir?: string;\n /** DB schema for all objects. Created by the migration and used to qualify every table name. */\n schema?: string;\n /** Prefix for all created tables (default 'pq_'). */\n tablePrefix?: string;\n /** Logical prefix for pub/sub channels when multiple PortQueues share one DB. */\n namespace?: string;\n}\n\n// --- Custom to PGLite: the client shape ---\n// The `@electric-sql/pglite` client is nothing like `pg` — one in-process instance instead of a\n// pool, a callback-style `transaction()` instead of BEGIN/COMMIT on a checked-out connection, and\n// `affectedRows` instead of `rowCount`.\nexport interface PGliteQueryable {\n query(\n sql: string,\n params?: readonly unknown[],\n ): Promise<{ rows: Array<Record<string, unknown>>; affectedRows?: number }>;\n}\nexport interface PGliteInstance extends PGliteQueryable {\n transaction<T>(fn: (tx: PGliteQueryable) => Promise<T>): Promise<T>;\n close(): Promise<void>;\n}\n\n/**\n * PGLite driver — PostgreSQL compiled to WASM, running in-process. Primarily for embedded/edge use\n * and tests.\n *\n * Because PGLite is byte-for-byte PG-wire compatible, this class **extends `PostgresqlDriver`** and\n * inherits its entire SQL surface: `$1`-style placeholders, SKIP LOCKED claiming, RETURNING\n * everywhere, the keys and indexes its migration creates, no relational JOIN (relationships resolve\n * by name/key lookups), and the in-code enforcement of idempotency/singleton dedup. A change to a\n * query in `PostgresqlDriver` therefore changes PGLite too; anything that must differ is overridden\n * here.\n *\n * What is custom to PGLite and overridden below:\n * - `engine`/`capabilities`/`migrations` — its own identity, capability set and migration array.\n * - `connect`/`disconnect` — boots one in-process WASM instance from `@electric-sql/pglite`\n * (an optional peer) instead of opening a `pg.Pool`.\n * - `execute`/`transaction` — talks to that instance and uses its callback-style `transaction()`.\n * - `probe` — no `SHOW server_version` round-trip; the version is the fixed string `'pglite'`.\n * - `notify`/`subscribe` — LISTEN/NOTIFY is off: PGLite is in-process with no shared bus, so the\n * interval poller is the backstop.\n * - `tryAdvisoryLock`/`releaseAdvisoryLock` — inert, since a single in-process DB has nothing to\n * coordinate with.\n */\nexport class PgliteDriver extends PostgresqlDriver implements Driver {\n override readonly engine: DatabaseEngine = DatabaseEngine.PGLITE;\n /** Same as PostgreSQL minus LISTEN/NOTIFY and advisory locks — neither is meaningful in-process. */\n override readonly capabilities: DriverCapabilitySet = capabilities({\n [DriverCapability.SKIP_LOCKED]: true,\n [DriverCapability.RETURNING]: true,\n [DriverCapability.TRANSACTIONS]: true,\n });\n override readonly migrations: MigrationStep[] = toMigrationSteps(PGLITE_MIGRATIONS);\n\n private db: PGliteInstance | null = null;\n private readonly dataDir: string | undefined;\n\n constructor(options: PGliteDriverOptions = {}) {\n // Forward only the naming options; PGLite has no connection string, pool or pg config.\n super({\n ...(options.tablePrefix !== undefined ? { tablePrefix: options.tablePrefix } : {}),\n ...(options.schema !== undefined ? { schema: options.schema } : {}),\n ...(options.namespace !== undefined ? { namespace: options.namespace } : {}),\n });\n this.dataDir = options.dataDir;\n }\n\n // --- Connection management (custom to PGLite) ---\n override async connect(): Promise<void> {\n let mod: { PGlite: new (dataDir?: string) => PGliteInstance };\n try {\n mod = (await import('@electric-sql/pglite')) as unknown as {\n PGlite: new (dataDir?: string) => PGliteInstance;\n };\n } catch {\n throw new MissingPeerDependencyError('@electric-sql/pglite', 'PGLite');\n }\n try {\n this.db = this.dataDir ? new mod.PGlite(this.dataDir) : new mod.PGlite();\n // Force the WASM instance to finish booting with a trivial query.\n await this.db.query('SELECT 1');\n } catch (err) {\n throw new DriverConnectionError('Failed to initialize PGLite.', err);\n }\n }\n\n override async disconnect(): Promise<void> {\n await this.db?.close();\n this.db = null;\n }\n\n protected override async execute(\n sql: string,\n params: readonly unknown[],\n session?: Session,\n ): Promise<RawResult> {\n const target = (session as PGliteQueryable | undefined) ?? this.db;\n if (!target) throw new DriverConnectionError('PGLite driver is not connected.');\n const res = await target.query(sql, params);\n // PGLite reports mutations as `affectedRows`, not `rowCount`.\n return { rows: res.rows ?? [], rowCount: res.affectedRows ?? res.rows?.length ?? 0 };\n }\n\n protected override async transaction<T>(fn: (session: Session) => Promise<T>): Promise<T> {\n if (!this.db) throw new DriverConnectionError('PGLite driver is not connected.');\n // The client owns BEGIN/COMMIT/ROLLBACK — no connection to check out and release.\n return this.db.transaction((tx) => fn(tx));\n }\n\n // --- Probe (custom to PGLite: no server to ask for a version) ---\n override async probe(): Promise<DriverProbe> {\n return { serverVersion: 'pglite', verifiedCapabilities: this.enabledCapabilities() };\n }\n\n // --- Pub/sub (custom to PGLite: LISTEN/NOTIFY disabled — an interval poll is the backstop) ---\n // No-op: PGLite runs in-process without a shared LISTEN/NOTIFY bus; the poller drives progress.\n override async notify(_options: { channel: string; payload: string }): Promise<void> {}\n\n override async subscribe(_options: {\n channel: string;\n onNotification: (payload: string) => void;\n }): Promise<Subscription> {\n throw new UnsupportedCapabilityError(\n DriverCapability.LISTEN_NOTIFY,\n 'PGLite has no shared notification bus; rely on interval polling instead.',\n );\n }\n\n // --- Advisory locks (custom to PGLite: inert) ---\n // A single in-process DB has no second process to coordinate with, so the migration runner takes\n // no lock — it skips these entirely because ADVISORY_LOCK is off in `capabilities`. They stay\n // declared, and inert, so PostgreSQL' pool-backed implementations can never be inherited.\n override async tryAdvisoryLock(_options: { key: string }): Promise<boolean> {\n return false;\n }\n\n override async releaseAdvisoryLock(_options: { key: string }): Promise<void> {}\n}\n"],"names":["postgresqlV1"],"mappings":";;;AAyBO,MAAM,oBAAwC,CAAC,EAAE,SAAS,GAAG,YAAYA,IAAc;AC6CvF,MAAM,qBAAqB,iBAAmC;AAAA,EACjD,SAAyB,eAAe;AAAA;AAAA,EAExC,eAAoC,aAAa;AAAA,IACjE,CAAC,iBAAiB,WAAW,GAAG;AAAA,IAChC,CAAC,iBAAiB,SAAS,GAAG;AAAA,IAC9B,CAAC,iBAAiB,YAAY,GAAG;AAAA,EAAA,CAClC;AAAA,EACiB,aAA8B,iBAAiB,iBAAiB;AAAA,EAE1E,KAA4B;AAAA,EACnB;AAAA,EAEjB,YAAY,UAA+B,IAAI;AAE7C,UAAM;AAAA,MACJ,GAAI,QAAQ,gBAAgB,SAAY,EAAE,aAAa,QAAQ,YAAA,IAAgB,CAAA;AAAA,MAC/E,GAAI,QAAQ,WAAW,SAAY,EAAE,QAAQ,QAAQ,OAAA,IAAW,CAAA;AAAA,MAChE,GAAI,QAAQ,cAAc,SAAY,EAAE,WAAW,QAAQ,cAAc,CAAA;AAAA,IAAC,CAC3E;AACD,SAAK,UAAU,QAAQ;AAAA,EACzB;AAAA;AAAA,EAGA,MAAe,UAAyB;AACtC,QAAI;AACJ,QAAI;AACF,YAAO,MAAM,OAAO,sBAAsB;AAAA,IAG5C,QAAQ;AACN,YAAM,IAAI,2BAA2B,wBAAwB,QAAQ;AAAA,IACvE;AACA,QAAI;AACF,WAAK,KAAK,KAAK,UAAU,IAAI,IAAI,OAAO,KAAK,OAAO,IAAI,IAAI,IAAI,OAAA;AAEhE,YAAM,KAAK,GAAG,MAAM,UAAU;AAAA,IAChC,SAAS,KAAK;AACZ,YAAM,IAAI,sBAAsB,gCAAgC,GAAG;AAAA,IACrE;AAAA,EACF;AAAA,EAEA,MAAe,aAA4B;AACzC,UAAM,KAAK,IAAI,MAAA;AACf,SAAK,KAAK;AAAA,EACZ;AAAA,EAEA,MAAyB,QACvB,KACA,QACA,SACoB;AACpB,UAAM,SAAU,WAA2C,KAAK;AAChE,QAAI,CAAC,OAAQ,OAAM,IAAI,sBAAsB,iCAAiC;AAC9E,UAAM,MAAM,MAAM,OAAO,MAAM,KAAK,MAAM;AAE1C,WAAO,EAAE,MAAM,IAAI,QAAQ,CAAA,GAAI,UAAU,IAAI,gBAAgB,IAAI,MAAM,UAAU,EAAA;AAAA,EACnF;AAAA,EAEA,MAAyB,YAAe,IAAkD;AACxF,QAAI,CAAC,KAAK,GAAI,OAAM,IAAI,sBAAsB,iCAAiC;AAE/E,WAAO,KAAK,GAAG,YAAY,CAAC,OAAO,GAAG,EAAE,CAAC;AAAA,EAC3C;AAAA;AAAA,EAGA,MAAe,QAA8B;AAC3C,WAAO,EAAE,eAAe,UAAU,sBAAsB,KAAK,sBAAoB;AAAA,EACnF;AAAA;AAAA;AAAA,EAIA,MAAe,OAAO,UAA+D;AAAA,EAAC;AAAA,EAEtF,MAAe,UAAU,UAGC;AACxB,UAAM,IAAI;AAAA,MACR,iBAAiB;AAAA,MACjB;AAAA,IAAA;AAAA,EAEJ;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,MAAe,gBAAgB,UAA6C;AAC1E,WAAO;AAAA,EACT;AAAA,EAEA,MAAe,oBAAoB,UAA0C;AAAA,EAAC;AAChF;"}
1
+ {"version":3,"file":"index.mjs","sources":["../../../src/drivers/pglite/migrations.ts","../../../src/drivers/pglite/pglite-driver.ts"],"sourcesContent":["import type { FlavourMigration } from '../shared/migration';\n// --- Compatible with PostgreSQL: reused verbatim ---\n// PGLite is PostgreSQL compiled to WASM, so every version below emits byte-for-byte the DDL the\n// PostgreSQL flavour emits. Rather than keep a second copy in sync by hand, the statement builders\n// are imported from the PostgreSQL flavour and referenced here.\nimport { v1 as postgresqlV1, v2 as postgresqlV2 } from '../postgresql/migrations';\n\n/**\n * Predefined PGLite DDL, version by version. Authored by hand for this engine; not generated at\n * runtime.\n *\n * `PGLITE_MIGRATIONS` is PGLite's own array — the runner only ever reads this one — but each entry\n * points at the PostgreSQL statement builder because the two dialects are identical, keys, partial\n * indexes and `CREATE SCHEMA IF NOT EXISTS` included. A new version is appended, never edited\n * into an earlier one, which is a frozen record of what has already been applied.\n *\n * --- Custom to PGLite ---\n * Nothing yet. When a version needs to diverge (e.g. dropping a clause the WASM build rejects),\n * author a local `v<n>(names: MigrationNames): string[]` here (`MigrationNames` comes from\n * `../shared/migration`) and swap it into the array for that version only; every other version\n * keeps reusing PostgreSQL. Spell its column names as raw string literals, never the `*Column` enums\n * — renaming an enum member must never retroactively rewrite a past migration's DDL.\n */\n\nexport const PGLITE_MIGRATIONS: FlavourMigration[] = [\n { version: 1, statements: postgresqlV1 },\n { version: 2, statements: postgresqlV2 },\n];\n","import { DatabaseEngine } from '../../options/enums';\nimport { capabilities, DriverCapability, type DriverCapabilitySet } from '../driver-capabilities';\nimport {\n DriverConnectionError,\n MissingPeerDependencyError,\n UnsupportedCapabilityError,\n} from '../../errors/errors';\nimport { toMigrationSteps } from '../shared/migration';\nimport { PGLITE_MIGRATIONS } from './migrations';\nimport type { Driver, DriverProbe, MigrationStep, Subscription } from '../driver.interface';\n// --- Compatible with PostgreSQL: the whole SQL surface is inherited ---\n// PGLite is PostgreSQL compiled to WASM and speaks the same wire protocol and dialect, so every\n// query method (jobs, queues, schedules, instances, claiming, retention, paging, the tx handle)\n// comes from `PostgresqlDriver` unchanged. `RawResult`/`Session` are its own connection-level types,\n// imported so the overrides below stay signature-compatible.\nimport { PostgresqlDriver, type RawResult, type Session } from '../postgresql/postgresql-driver';\n\n/**\n * Options for {@link PgliteDriver} — entirely custom to PGLite: there is no host, port, user or\n * pool, only an optional on-disk directory. `schema`/`tablePrefix`/`namespace` mean exactly what\n * they mean for PostgreSQL and are forwarded to the base driver's `DriverContext`.\n */\nexport interface PGliteDriverOptions {\n /** Data directory for a file-backed DB; omit for in-memory. */\n dataDir?: string;\n /** DB schema for all objects. Created by the migration and used to qualify every table name. */\n schema?: string;\n /** Prefix for all created tables (default 'pq_'). */\n tablePrefix?: string;\n /** Logical prefix for pub/sub channels when multiple PortQueues share one DB. */\n namespace?: string;\n}\n\n// --- Custom to PGLite: the client shape ---\n// The `@electric-sql/pglite` client is nothing like `pg` — one in-process instance instead of a\n// pool, a callback-style `transaction()` instead of BEGIN/COMMIT on a checked-out connection, and\n// `affectedRows` instead of `rowCount`.\nexport interface PGliteQueryable {\n query(\n sql: string,\n params?: readonly unknown[],\n ): Promise<{ rows: Array<Record<string, unknown>>; affectedRows?: number }>;\n}\nexport interface PGliteInstance extends PGliteQueryable {\n transaction<T>(fn: (tx: PGliteQueryable) => Promise<T>): Promise<T>;\n close(): Promise<void>;\n}\n\n/**\n * PGLite driver — PostgreSQL compiled to WASM, running in-process. Primarily for embedded/edge use\n * and tests.\n *\n * Because PGLite is byte-for-byte PG-wire compatible, this class **extends `PostgresqlDriver`** and\n * inherits its entire SQL surface: `$1`-style placeholders, SKIP LOCKED claiming, RETURNING\n * everywhere, the keys and indexes its migration creates, no relational JOIN (relationships resolve\n * by name/key lookups), and the in-code enforcement of idempotency/singleton dedup. A change to a\n * query in `PostgresqlDriver` therefore changes PGLite too; anything that must differ is overridden\n * here.\n *\n * What is custom to PGLite and overridden below:\n * - `engine`/`capabilities`/`migrations` — its own identity, capability set and migration array.\n * - `connect`/`disconnect` — boots one in-process WASM instance from `@electric-sql/pglite`\n * (an optional peer) instead of opening a `pg.Pool`.\n * - `execute`/`transaction` — talks to that instance and uses its callback-style `transaction()`.\n * - `probe` — no `SHOW server_version` round-trip; the version is the fixed string `'pglite'`.\n * - `notify`/`subscribe` — LISTEN/NOTIFY is off: PGLite is in-process with no shared bus, so the\n * interval poller is the backstop.\n * - `tryAdvisoryLock`/`releaseAdvisoryLock` — inert, since a single in-process DB has nothing to\n * coordinate with.\n */\nexport class PgliteDriver extends PostgresqlDriver implements Driver {\n override readonly engine: DatabaseEngine = DatabaseEngine.PGLITE;\n /** Same as PostgreSQL minus LISTEN/NOTIFY and advisory locks — neither is meaningful in-process. */\n override readonly capabilities: DriverCapabilitySet = capabilities({\n [DriverCapability.SKIP_LOCKED]: true,\n [DriverCapability.RETURNING]: true,\n [DriverCapability.TRANSACTIONS]: true,\n });\n override readonly migrations: MigrationStep[] = toMigrationSteps(PGLITE_MIGRATIONS);\n\n private db: PGliteInstance | null = null;\n private readonly dataDir: string | undefined;\n\n constructor(options: PGliteDriverOptions = {}) {\n // Forward only the naming options; PGLite has no connection string, pool or pg config.\n super({\n ...(options.tablePrefix !== undefined ? { tablePrefix: options.tablePrefix } : {}),\n ...(options.schema !== undefined ? { schema: options.schema } : {}),\n ...(options.namespace !== undefined ? { namespace: options.namespace } : {}),\n });\n this.dataDir = options.dataDir;\n }\n\n // --- Connection management (custom to PGLite) ---\n override async connect(): Promise<void> {\n let mod: { PGlite: new (dataDir?: string) => PGliteInstance };\n try {\n mod = (await import('@electric-sql/pglite')) as unknown as {\n PGlite: new (dataDir?: string) => PGliteInstance;\n };\n } catch {\n throw new MissingPeerDependencyError('@electric-sql/pglite', 'PGLite');\n }\n try {\n this.db = this.dataDir ? new mod.PGlite(this.dataDir) : new mod.PGlite();\n // Force the WASM instance to finish booting with a trivial query.\n await this.db.query('SELECT 1');\n } catch (err) {\n throw new DriverConnectionError('Failed to initialize PGLite.', err);\n }\n }\n\n override async disconnect(): Promise<void> {\n await this.db?.close();\n this.db = null;\n }\n\n protected override async execute(\n sql: string,\n params: readonly unknown[],\n session?: Session,\n ): Promise<RawResult> {\n const target = (session as PGliteQueryable | undefined) ?? this.db;\n if (!target) throw new DriverConnectionError('PGLite driver is not connected.');\n const res = await target.query(sql, params);\n // PGLite reports mutations as `affectedRows`, not `rowCount`.\n return { rows: res.rows ?? [], rowCount: res.affectedRows ?? res.rows?.length ?? 0 };\n }\n\n protected override async transaction<T>(fn: (session: Session) => Promise<T>): Promise<T> {\n if (!this.db) throw new DriverConnectionError('PGLite driver is not connected.');\n // The client owns BEGIN/COMMIT/ROLLBACK — no connection to check out and release.\n return this.db.transaction((tx) => fn(tx));\n }\n\n // --- Probe (custom to PGLite: no server to ask for a version) ---\n override async probe(): Promise<DriverProbe> {\n return { serverVersion: 'pglite', verifiedCapabilities: this.enabledCapabilities() };\n }\n\n // --- Pub/sub (custom to PGLite: LISTEN/NOTIFY disabled — an interval poll is the backstop) ---\n // No-op: PGLite runs in-process without a shared LISTEN/NOTIFY bus; the poller drives progress.\n override async notify(_options: { channel: string; payload: string }): Promise<void> {}\n\n override async subscribe(_options: {\n channel: string;\n onNotification: (payload: string) => void;\n }): Promise<Subscription> {\n throw new UnsupportedCapabilityError(\n DriverCapability.LISTEN_NOTIFY,\n 'PGLite has no shared notification bus; rely on interval polling instead.',\n );\n }\n\n // --- Advisory locks (custom to PGLite: inert) ---\n // A single in-process DB has no second process to coordinate with, so the migration runner takes\n // no lock — it skips these entirely because ADVISORY_LOCK is off in `capabilities`. They stay\n // declared, and inert, so PostgreSQL' pool-backed implementations can never be inherited.\n override async tryAdvisoryLock(_options: { key: string }): Promise<boolean> {\n return false;\n }\n\n override async releaseAdvisoryLock(_options: { key: string }): Promise<void> {}\n}\n"],"names":["postgresqlV1","postgresqlV2"],"mappings":";;;AAwBO,MAAM,oBAAwC;AAAA,EACnD,EAAE,SAAS,GAAG,YAAYA,GAAA;AAAA,EAC1B,EAAE,SAAS,GAAG,YAAYC,GAAA;AAC5B;AC2CO,MAAM,qBAAqB,iBAAmC;AAAA,EACjD,SAAyB,eAAe;AAAA;AAAA,EAExC,eAAoC,aAAa;AAAA,IACjE,CAAC,iBAAiB,WAAW,GAAG;AAAA,IAChC,CAAC,iBAAiB,SAAS,GAAG;AAAA,IAC9B,CAAC,iBAAiB,YAAY,GAAG;AAAA,EAAA,CAClC;AAAA,EACiB,aAA8B,iBAAiB,iBAAiB;AAAA,EAE1E,KAA4B;AAAA,EACnB;AAAA,EAEjB,YAAY,UAA+B,IAAI;AAE7C,UAAM;AAAA,MACJ,GAAI,QAAQ,gBAAgB,SAAY,EAAE,aAAa,QAAQ,YAAA,IAAgB,CAAA;AAAA,MAC/E,GAAI,QAAQ,WAAW,SAAY,EAAE,QAAQ,QAAQ,OAAA,IAAW,CAAA;AAAA,MAChE,GAAI,QAAQ,cAAc,SAAY,EAAE,WAAW,QAAQ,cAAc,CAAA;AAAA,IAAC,CAC3E;AACD,SAAK,UAAU,QAAQ;AAAA,EACzB;AAAA;AAAA,EAGA,MAAe,UAAyB;AACtC,QAAI;AACJ,QAAI;AACF,YAAO,MAAM,OAAO,sBAAsB;AAAA,IAG5C,QAAQ;AACN,YAAM,IAAI,2BAA2B,wBAAwB,QAAQ;AAAA,IACvE;AACA,QAAI;AACF,WAAK,KAAK,KAAK,UAAU,IAAI,IAAI,OAAO,KAAK,OAAO,IAAI,IAAI,IAAI,OAAA;AAEhE,YAAM,KAAK,GAAG,MAAM,UAAU;AAAA,IAChC,SAAS,KAAK;AACZ,YAAM,IAAI,sBAAsB,gCAAgC,GAAG;AAAA,IACrE;AAAA,EACF;AAAA,EAEA,MAAe,aAA4B;AACzC,UAAM,KAAK,IAAI,MAAA;AACf,SAAK,KAAK;AAAA,EACZ;AAAA,EAEA,MAAyB,QACvB,KACA,QACA,SACoB;AACpB,UAAM,SAAU,WAA2C,KAAK;AAChE,QAAI,CAAC,OAAQ,OAAM,IAAI,sBAAsB,iCAAiC;AAC9E,UAAM,MAAM,MAAM,OAAO,MAAM,KAAK,MAAM;AAE1C,WAAO,EAAE,MAAM,IAAI,QAAQ,CAAA,GAAI,UAAU,IAAI,gBAAgB,IAAI,MAAM,UAAU,EAAA;AAAA,EACnF;AAAA,EAEA,MAAyB,YAAe,IAAkD;AACxF,QAAI,CAAC,KAAK,GAAI,OAAM,IAAI,sBAAsB,iCAAiC;AAE/E,WAAO,KAAK,GAAG,YAAY,CAAC,OAAO,GAAG,EAAE,CAAC;AAAA,EAC3C;AAAA;AAAA,EAGA,MAAe,QAA8B;AAC3C,WAAO,EAAE,eAAe,UAAU,sBAAsB,KAAK,sBAAoB;AAAA,EACnF;AAAA;AAAA;AAAA,EAIA,MAAe,OAAO,UAA+D;AAAA,EAAC;AAAA,EAEtF,MAAe,UAAU,UAGC;AACxB,UAAM,IAAI;AAAA,MACR,iBAAiB;AAAA,MACjB;AAAA,IAAA;AAAA,EAEJ;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,MAAe,gBAAgB,UAA6C;AAC1E,WAAO;AAAA,EACT;AAAA,EAEA,MAAe,oBAAoB,UAA0C;AAAA,EAAC;AAChF;"}
@@ -5,9 +5,8 @@ import { FlavourMigration } from '../shared/migration';
5
5
  *
6
6
  * `PGLITE_MIGRATIONS` is PGLite's own array — the runner only ever reads this one — but each entry
7
7
  * points at the PostgreSQL statement builder because the two dialects are identical, keys, partial
8
- * indexes and `CREATE SCHEMA IF NOT EXISTS` included. There is a single version so far; a future
9
- * change appends `{ version: 2, ... }` rather than editing v1, which is a frozen record of what has
10
- * already been applied.
8
+ * indexes and `CREATE SCHEMA IF NOT EXISTS` included. A new version is appended, never edited
9
+ * into an earlier one, which is a frozen record of what has already been applied.
11
10
  *
12
11
  * --- Custom to PGLite ---
13
12
  * Nothing yet. When a version needs to diverge (e.g. dropping a clause the WASM build rejects),
@@ -1,5 +1,5 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
- const postgresqlDriver = require("../../postgresql-driver-DRT68TKb.cjs");
3
+ const postgresqlDriver = require("../../postgresql-driver-xfqy9mi8.cjs");
4
4
  exports.PostgresqlDriver = postgresqlDriver.PostgresqlDriver;
5
5
  //# sourceMappingURL=index.cjs.map
@@ -1,4 +1,4 @@
1
- import { P } from "../../postgresql-driver-CPHv5SUT.js";
1
+ import { P } from "../../postgresql-driver-DQRzg2NA.js";
2
2
  export {
3
3
  P as PostgresqlDriver
4
4
  };
@@ -19,4 +19,25 @@ import { FlavourMigration, MigrationNames } from '../shared/migration';
19
19
  */
20
20
  /** Version 1 — the full base schema, its keys and its indexes. */
21
21
  export declare function v1(names: MigrationNames): string[];
22
+ /**
23
+ * Version 2 — partial indexes for the three hot predicates.
24
+ *
25
+ * - **The claim.** v1's `claim_idx` keyed on `status`, so the claim's `status IN ('PENDING','RETRY')`
26
+ * matched two ranges of the index and PostgreSQL had to read and sort *every* waiting row before
27
+ * it could apply the `LIMIT` — a claim got slower with every job in the backlog (10 ms at 10 000
28
+ * waiting rows). Moving the status test into the index predicate leaves `(queue_name, priority
29
+ * DESC, start_after)` as the key, which is exactly the claim's `ORDER BY`, so the scan stops after
30
+ * `LIMIT` rows (0.2 ms at the same backlog). The claim binds its statuses as parameters; an
31
+ * unnamed statement is planned with their values, which is what lets the planner prove the
32
+ * predicate.
33
+ * - **The stalled sweep** only ever looks at `ACTIVE` rows, and **the expiry sweep** only at rows
34
+ * that carry an `expire_at`, so both become partial too.
35
+ *
36
+ * Fewer rows in each index is also fewer index writes: every claim and settle changes `status`, which
37
+ * PostgreSQL cannot do as a HOT update, so each status change writes an entry into every index whose
38
+ * predicate the row satisfies. A finished row now sits in none of these three.
39
+ *
40
+ * Column names are literals, never the `*Column` enums: this is a frozen record of applied DDL.
41
+ */
42
+ export declare function v2(names: MigrationNames): string[];
22
43
  export declare const POSTGRESQL_MIGRATIONS: FlavourMigration[];
@@ -2,8 +2,8 @@ import { DatabaseEngine, DeduplicationReason } from '../../options/enums';
2
2
  import { JobStatus } from '../../jobs/job-state.enum';
3
3
  import { DriverCapabilitySet } from '../driver-capabilities';
4
4
  import { BaseDriver, RawResult, Row, Session } from '../shared/base-driver';
5
- import { Driver, DriverProbe, InstanceListFilter, JobListFilter, JobRef, MigrationStep, PageWindow, QueueListFilter, RequeuedJobRef, ScheduleListFilter, ScheduleRef, StalledJobRef, Subscription } from '../driver.interface';
6
- import { InsertedJobRow, InstanceMetrics, InstanceRow, JobRecord, NewJobRecord, NewScheduleRow, QueueRow, ScheduleRow } from '../../jobs/job-record';
5
+ import { CompletedJobInput, Driver, DriverProbe, InstanceListFilter, JobListFilter, JobRef, MigrationStep, PageWindow, QueueListFilter, RequeuedJobRef, ScheduleListFilter, ScheduleRef, StalledJobRef, Subscription } from '../driver.interface';
6
+ import { ClaimedJob, InsertedJobRow, InstanceMetrics, InstanceRow, JobRecord, NewJobRecord, NewScheduleRow, QueueRow, ScheduleRow } from '../../jobs/job-record';
7
7
  import { QueueOptions } from '../../options/queue-options';
8
8
  import { TransactionHandle } from '../../tx/transaction-handle';
9
9
  export interface PostgresqlDriverOptions {
@@ -99,6 +99,16 @@ export declare class PostgresqlDriver extends BaseDriver implements Driver {
99
99
  jobs: NewJobRecord[];
100
100
  tx?: TransactionHandle;
101
101
  }): Promise<InsertedJobRow[]>;
102
+ /**
103
+ * Insert rows that passed dedup, filling `results` at each row's input index. The whole chunk
104
+ * travels as **one** JSON parameter that `json_to_recordset` unpacks into rows, rather than a
105
+ * multi-row VALUES binding every column of every row: 34 columns × 1000 rows was 34 000 bound
106
+ * parameters, split across statements by the parameter limit, and parsing that many binds cost
107
+ * more than the insert itself — one JSON value inserts about twice as fast. No RETURNING either:
108
+ * the id, status and start time of every row are the values just sent, so the row count is the
109
+ * only thing worth checking.
110
+ */
111
+ private insertFresh;
102
112
  /**
103
113
  * Serialize the dedup-lookup-then-insert pair against another producer's, per queue.
104
114
  *
@@ -153,7 +163,7 @@ export declare class PostgresqlDriver extends BaseDriver implements Driver {
153
163
  lockDurationSeconds: number;
154
164
  now: number;
155
165
  instanceKey?: string | null;
156
- }): Promise<JobRecord[]>;
166
+ }): Promise<ClaimedJob[]>;
157
167
  /**
158
168
  * The fence a completion is guarded on: the row must still be `ACTIVE` and, when the caller names
159
169
  * the claim it is settling, still carry that claim's token.
@@ -177,6 +187,21 @@ export declare class PostgresqlDriver extends BaseDriver implements Driver {
177
187
  lockToken?: string | null;
178
188
  tx?: TransactionHandle;
179
189
  }): Promise<boolean>;
190
+ /**
191
+ * One guarded `UPDATE … RETURNING id` per claim token (and per id chunk): every row in the group is
192
+ * fenced on that token and on `ACTIVE`, exactly like {@link completeJob}, and `RETURNING` names the
193
+ * rows that were settled. A lone statement per group, so no explicit transaction.
194
+ */
195
+ completeJobs(options: {
196
+ jobs: CompletedJobInput[];
197
+ now: number;
198
+ tx?: TransactionHandle;
199
+ }): Promise<string[]>;
200
+ /**
201
+ * The `return_value` a batch settle writes: `NULL` when no job in the chunk returned anything (the
202
+ * common case — a handler that returns nothing), otherwise a `CASE` on the id.
203
+ */
204
+ private returnValueExpression;
180
205
  retryJob(options: {
181
206
  jobId: string;
182
207
  nextRunAt: number;
@@ -2,7 +2,7 @@ import { JobStatus } from '../../jobs/job-state.enum';
2
2
  import { DriverCapability, DriverCapabilitySet } from '../driver-capabilities';
3
3
  import { DriverContext } from '../driver-context';
4
4
  import { JobColumn, ScheduleColumn, TableName } from '../table-names';
5
- import { InstanceRow, JobRecord, NewJobRecord, QueueRow, ScheduleRow } from '../../jobs/job-record';
5
+ import { ClaimedJob, InstanceRow, JobRecord, NewJobRecord, QueueRow, ScheduleRow } from '../../jobs/job-record';
6
6
  import { PageWindow } from '../driver.interface';
7
7
  import { TransactionHandle } from '../../tx/transaction-handle';
8
8
  /** A raw query result, normalized across every client library. */
@@ -20,6 +20,12 @@ export type Row = Record<string, unknown>;
20
20
  * insert of every engine, and one list makes that impossible to forget.
21
21
  */
22
22
  export declare const JOB_COLUMNS: readonly JobColumn[];
23
+ /**
24
+ * The columns a claim returns — what {@link BaseDriver.mapClaimedJob} reads. `queue_name`, `status`
25
+ * and `lock_token` are absent on purpose: the claim set or filtered on all three, so the mapper takes
26
+ * them from the claim instead of the row.
27
+ */
28
+ export declare const CLAIM_COLUMNS: readonly JobColumn[];
23
29
  /** The columns a schedule insert writes, in {@link BaseDriver.scheduleInsertValues} order. */
24
30
  export declare const SCHEDULE_INSERT_COLUMNS: readonly ScheduleColumn[];
25
31
  /**
@@ -135,6 +141,21 @@ export declare abstract class BaseDriver {
135
141
  * is the same trade every bounded sweep in the tool makes.
136
142
  */
137
143
  protected overIdChunks<K, R>(keys: readonly K[], perChunk: (chunk: K[]) => Promise<R[]>): Promise<R[]>;
144
+ /**
145
+ * Group a settle batch by the claim token each job carries, preserving input order within a group.
146
+ *
147
+ * A claim stamps one token on every row it takes, so a batch of completions spans only as many
148
+ * tokens as claims produced it — usually one or two. Grouping lets a driver fence a whole group with
149
+ * a single `lock_token = ?` instead of a per-row condition, which keeps the statement short and
150
+ * index-friendly. Entries without a token form their own group, fenced on status alone (exactly
151
+ * what the singular settle does for them).
152
+ */
153
+ protected groupByLockToken<T extends {
154
+ lockToken?: string | null;
155
+ }>(items: readonly T[]): Array<{
156
+ lockToken: string | null;
157
+ items: T[];
158
+ }>;
138
159
  protected toJson(value: unknown): string | null;
139
160
  protected fromJson(value: unknown): unknown;
140
161
  protected boolIn(value: boolean): number;
@@ -167,6 +188,11 @@ export declare abstract class BaseDriver {
167
188
  */
168
189
  protected emptyStatusCounts(): Record<JobStatus, number>;
169
190
  protected mapJob(row: Row): JobRecord;
191
+ /** A claimed row ({@link CLAIM_COLUMNS}) plus what the claim itself knows about it. */
192
+ protected mapClaimedJob(row: Row, claim: {
193
+ queueName: string;
194
+ lockToken: string;
195
+ }): ClaimedJob;
170
196
  protected mapQueue(row: Row): QueueRow;
171
197
  protected mapInstance(row: Row): InstanceRow;
172
198
  protected mapSchedule(row: Row): ScheduleRow;
@@ -212,5 +238,5 @@ export declare abstract class BaseDriver {
212
238
  */
213
239
  protected collectAffinityMatches(rows: Row[], instanceKey: string, limit: number, into: Set<string>): void;
214
240
  /** Claim order: highest priority first, then oldest due. Applied after a claim returns rows. */
215
- protected sortClaimed(jobs: JobRecord[]): JobRecord[];
241
+ protected sortClaimed<T extends Pick<JobRecord, 'priority' | 'startAfter'>>(jobs: T[]): T[];
216
242
  }
@@ -23,7 +23,7 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
23
23
  ));
24
24
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
25
25
  const storage_constants = require("../../storage.constants-BKeXcSQe.cjs");
26
- const migration = require("../../migration-Dx8eUthW.cjs");
26
+ const migration = require("../../migration-CX7j4wVB.cjs");
27
27
  function v1(names) {
28
28
  const t = names.name;
29
29
  const idx = names.derived;
@@ -472,7 +472,7 @@ class SqliteDriver extends migration.BaseDriver {
472
472
  const { queueName, limit, lockDurationSeconds, now, instanceKey } = options;
473
473
  const lockedUntil = this.lockDeadline(now, lockDurationSeconds);
474
474
  const lockToken = this.newId();
475
- const cols = migration.JOB_COLUMNS.join(", ");
475
+ const cols = migration.CLAIM_COLUMNS.join(", ");
476
476
  if (!instanceKey) {
477
477
  const res = await this.runMaybeTx(
478
478
  `UPDATE ${this.t(migration.TableName.JOB)} SET ${migration.JobColumn.STATUS} = ${this.ph(0)}, ${migration.JobColumn.STARTED_AT} = ${this.ph(1)}, ${migration.JobColumn.LOCKED_UNTIL} = ${this.ph(2)}, ${migration.JobColumn.LOCK_TOKEN} = ${this.ph(3)}, ${migration.JobColumn.UPDATED_AT} = ${this.ph(4)} WHERE ${migration.JobColumn.ID} IN (SELECT ${migration.JobColumn.ID} FROM ${this.t(migration.TableName.JOB)} WHERE ${migration.JobColumn.QUEUE_NAME} = ${this.ph(5)} AND ${migration.JobColumn.STATUS} IN (${this.ph(6)}, ${this.ph(7)}) AND ${migration.JobColumn.START_AFTER} <= ${this.ph(8)} AND (${migration.JobColumn.EXPIRE_AT} IS NULL OR ${migration.JobColumn.EXPIRE_AT} > ${this.ph(9)}) ORDER BY ${migration.JobColumn.PRIORITY} DESC, ${migration.JobColumn.START_AFTER} ASC LIMIT ${this.ph(10)}) RETURNING ${cols}`,
@@ -490,7 +490,7 @@ class SqliteDriver extends migration.BaseDriver {
490
490
  limit
491
491
  ]
492
492
  );
493
- return this.sortClaimed(res.rows.map((r) => this.mapJob(r)));
493
+ return this.sortClaimed(res.rows.map((r) => this.mapClaimedJob(r, { queueName, lockToken })));
494
494
  }
495
495
  const { pageSize, maxScan } = this.affinityPlan(limit);
496
496
  return this.transaction(async (session) => {
@@ -513,7 +513,7 @@ class SqliteDriver extends migration.BaseDriver {
513
513
  [...head, ...idList, storage_constants.JobStatus.PENDING, storage_constants.JobStatus.RETRY],
514
514
  session
515
515
  );
516
- return this.sortClaimed(upd.rows.map((r) => this.mapJob(r)));
516
+ return this.sortClaimed(upd.rows.map((r) => this.mapClaimedJob(r, { queueName, lockToken })));
517
517
  });
518
518
  }
519
519
  /**
@@ -561,6 +561,38 @@ class SqliteDriver extends migration.BaseDriver {
561
561
  );
562
562
  return res.rowCount > 0;
563
563
  }
564
+ /**
565
+ * One guarded `UPDATE … RETURNING id` per claim token (and per id chunk), fenced per row like
566
+ * {@link completeJob}. Parameters are pushed in the order their `?` placeholders appear.
567
+ */
568
+ async completeJobs(options) {
569
+ const settled = [];
570
+ for (const group of this.groupByLockToken(options.jobs)) {
571
+ const ids = await this.overIdChunks(group.items, async (chunk) => {
572
+ const params = [storage_constants.JobStatus.COMPLETED];
573
+ const returnValue = this.returnValueExpression(chunk, params);
574
+ params.push(options.now, options.now, ...chunk.map((job) => job.jobId));
575
+ const res = await this.runMaybeTx(
576
+ `UPDATE ${this.t(migration.TableName.JOB)} SET ${migration.JobColumn.STATUS} = ?, ${migration.JobColumn.RETURN_VALUE} = ${returnValue}, ${migration.JobColumn.COMPLETED_AT} = ?, ${migration.JobColumn.UPDATED_AT} = ?, ${migration.JobColumn.LOCKED_UNTIL} = NULL, ${migration.JobColumn.LOCK_TOKEN} = NULL WHERE ${migration.JobColumn.ID} IN (${this.phList(chunk.length)})${this.claimFence(params, group.lockToken)} RETURNING ${migration.JobColumn.ID}`,
577
+ params,
578
+ options.tx
579
+ );
580
+ return res.rows.map((row) => String(row[migration.JobColumn.ID]));
581
+ });
582
+ settled.push(...ids);
583
+ }
584
+ return settled;
585
+ }
586
+ /** `NULL` when no job in the chunk returned anything, otherwise a `CASE` on the id. */
587
+ returnValueExpression(chunk, params) {
588
+ const values = chunk.map((job) => this.toJson(job.returnValue));
589
+ if (values.every((value) => value === null)) return "NULL";
590
+ const arms = chunk.map((job, i) => {
591
+ params.push(job.jobId, values[i]);
592
+ return "WHEN ? THEN ?";
593
+ });
594
+ return `CASE ${migration.JobColumn.ID} ${arms.join(" ")} ELSE NULL END`;
595
+ }
564
596
  async retryJob(options) {
565
597
  const params = [
566
598
  storage_constants.JobStatus.RETRY,