@relaybus/postgres 0.3.1 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.cts CHANGED
@@ -190,7 +190,18 @@ interface PostgresConnectionOptions extends PostgresEventStoreSettings {
190
190
  /** Use another database library through an adapter. The store never closes it. */
191
191
  interface PostgresExecutorOptions<TTransaction> extends PostgresEventStoreSettings {
192
192
  readonly executor: PostgresExecutor<TTransaction>;
193
- readonly pool?: never;
193
+ /**
194
+ * Optional `pg` pool on the same database, for example the one an ORM's `pg` driver adapter uses
195
+ * (`new PrismaPg(pool)`, Kysely's `PostgresDialect`, Drizzle's `node-postgres`). The store never
196
+ * closes it. With it:
197
+ * - the statements the store sends outside a transaction (the dispatcher's claims, completions and
198
+ * lease renewals, maintenance) run on it as prepared statements (`preparedStatements`), which ORM
199
+ * raw queries cannot do; statements in a transaction still go through `executor`;
200
+ * - with `wakeUp: 'notify'` the store listens for wake-ups with its connection settings, so a
201
+ * delivery published in an ORM transaction is dispatched right after that transaction commits,
202
+ * instead of after a fixed delay or the next poll.
203
+ */
204
+ readonly pool?: Pool;
194
205
  readonly connectionString?: never;
195
206
  readonly poolConfig?: never;
196
207
  }
@@ -330,4 +341,5 @@ declare class ReconcileFailedError extends RelayBusError {
330
341
  readonly code: string;
331
342
  }
332
343
 
333
- export { PgExecutor, type PgExecutorOptions, type PostgresConnectionOptions, PostgresEventStore, type PostgresEventStoreOptions, type PostgresEventStoreSettings, PostgresExecutor, type PostgresExecutorOptions, type PostgresNames, PostgresParameter, type PostgresPoolOptions, PostgresQueryOptions, PostgresRow, type PostgresSchemaOptions, type PostgresWakeUp, ReconcileFailedError, createPostgresSchemaSql, createPostgresSchemaStatements, resolvePostgresNames };
344
+ export { PgExecutor, PostgresEventStore, PostgresExecutor, PostgresParameter, PostgresQueryOptions, PostgresRow, ReconcileFailedError, createPostgresSchemaSql, createPostgresSchemaStatements, resolvePostgresNames };
345
+ export type { PgExecutorOptions, PostgresConnectionOptions, PostgresEventStoreOptions, PostgresEventStoreSettings, PostgresExecutorOptions, PostgresNames, PostgresPoolOptions, PostgresSchemaOptions, PostgresWakeUp };
package/dist/index.d.ts CHANGED
@@ -190,7 +190,18 @@ interface PostgresConnectionOptions extends PostgresEventStoreSettings {
190
190
  /** Use another database library through an adapter. The store never closes it. */
191
191
  interface PostgresExecutorOptions<TTransaction> extends PostgresEventStoreSettings {
192
192
  readonly executor: PostgresExecutor<TTransaction>;
193
- readonly pool?: never;
193
+ /**
194
+ * Optional `pg` pool on the same database, for example the one an ORM's `pg` driver adapter uses
195
+ * (`new PrismaPg(pool)`, Kysely's `PostgresDialect`, Drizzle's `node-postgres`). The store never
196
+ * closes it. With it:
197
+ * - the statements the store sends outside a transaction (the dispatcher's claims, completions and
198
+ * lease renewals, maintenance) run on it as prepared statements (`preparedStatements`), which ORM
199
+ * raw queries cannot do; statements in a transaction still go through `executor`;
200
+ * - with `wakeUp: 'notify'` the store listens for wake-ups with its connection settings, so a
201
+ * delivery published in an ORM transaction is dispatched right after that transaction commits,
202
+ * instead of after a fixed delay or the next poll.
203
+ */
204
+ readonly pool?: Pool;
194
205
  readonly connectionString?: never;
195
206
  readonly poolConfig?: never;
196
207
  }
@@ -330,4 +341,5 @@ declare class ReconcileFailedError extends RelayBusError {
330
341
  readonly code: string;
331
342
  }
332
343
 
333
- export { PgExecutor, type PgExecutorOptions, type PostgresConnectionOptions, PostgresEventStore, type PostgresEventStoreOptions, type PostgresEventStoreSettings, PostgresExecutor, type PostgresExecutorOptions, type PostgresNames, PostgresParameter, type PostgresPoolOptions, PostgresQueryOptions, PostgresRow, type PostgresSchemaOptions, type PostgresWakeUp, ReconcileFailedError, createPostgresSchemaSql, createPostgresSchemaStatements, resolvePostgresNames };
344
+ export { PgExecutor, PostgresEventStore, PostgresExecutor, PostgresParameter, PostgresQueryOptions, PostgresRow, ReconcileFailedError, createPostgresSchemaSql, createPostgresSchemaStatements, resolvePostgresNames };
345
+ export type { PgExecutorOptions, PostgresConnectionOptions, PostgresEventStoreOptions, PostgresEventStoreSettings, PostgresExecutorOptions, PostgresNames, PostgresPoolOptions, PostgresSchemaOptions, PostgresWakeUp };
package/dist/index.js CHANGED
@@ -491,6 +491,19 @@ var NotificationListener = class {
491
491
  await drop?.();
492
492
  }
493
493
  };
494
+
495
+ // src/root-pool-executor.ts
496
+ function withRootPool(executor, root) {
497
+ return {
498
+ ...executor.sessionTimeouts === void 0 ? {} : { sessionTimeouts: executor.sessionTimeouts },
499
+ transaction: (work) => executor.transaction(work),
500
+ ...executor.transactionWithBudget === void 0 ? {} : {
501
+ transactionWithBudget: (work, timeoutMs) => executor.transactionWithBudget(work, timeoutMs)
502
+ },
503
+ // eslint-disable-next-line relaybus/max-two-params -- PostgresExecutor port signature
504
+ query: (sql, parameters, transaction, options) => transaction === void 0 ? root.query(sql, parameters, void 0, options) : executor.query(sql, parameters, transaction, options)
505
+ };
506
+ }
494
507
  function text(row, column) {
495
508
  return row[column];
496
509
  }
@@ -1434,7 +1447,7 @@ var PostgresMaintenance = class {
1434
1447
  var WAKE_UPS = ["notify", "poll"];
1435
1448
  var DEFAULT_PURGE_BATCH_SIZE = 1e3;
1436
1449
  function resolveStoreSettings(options) {
1437
- const sources = [options.pool, options.connectionString, options.executor].filter(
1450
+ const sources = [options.executor ?? options.pool, options.connectionString].filter(
1438
1451
  (source) => source !== void 0
1439
1452
  );
1440
1453
  if (sources.length !== 1) {
@@ -1540,9 +1553,13 @@ WHERE (SELECT count(*) FROM woken) >= 0`;
1540
1553
  RETURNING available_at
1541
1554
  )`;
1542
1555
  }
1543
- /** One wake-up notification on {@link PostgresNames.notificationChannel}, outside any publishing transaction. */
1556
+ /**
1557
+ * One wake-up notification on {@link PostgresNames.notificationChannel}, outside any publishing
1558
+ * transaction. Returns a boolean column rather than the `void` of `pg_notify`, which some libraries
1559
+ * cannot read (Prisma's raw queries reject a `void` column).
1560
+ */
1544
1561
  get notify() {
1545
- return `SELECT pg_notify('${this.#names.notificationChannel}', '')`;
1562
+ return `SELECT true AS notified FROM pg_notify('${this.#names.notificationChannel}', '')`;
1546
1563
  }
1547
1564
  /**
1548
1565
  * A CTE `woken` that sends one notification on {@link PostgresNames.notificationChannel} when a row of
@@ -2832,8 +2849,12 @@ var SetupStatements = class {
2832
2849
  WHERE n.nspname = '${this.#names.schema}' AND c.relname = '${this.#names.lanesTableName}' AND d.description = $1::text
2833
2850
  ) AS current`;
2834
2851
  }
2852
+ /**
2853
+ * Takes the setup lock. Returns a boolean column rather than the `void` of the lock function, which
2854
+ * some libraries cannot read (Prisma's raw queries reject a `void` column).
2855
+ */
2835
2856
  get setupLock() {
2836
- return "SELECT pg_advisory_xact_lock(hashtextextended($1::text, 0))";
2857
+ return "SELECT true AS locked FROM pg_advisory_xact_lock(hashtextextended($1::text, 0))";
2837
2858
  }
2838
2859
  };
2839
2860
 
@@ -3102,7 +3123,12 @@ var PostgresEventStore = class {
3102
3123
  };
3103
3124
  function connect(options, { logger, owned }) {
3104
3125
  if (options.executor !== void 0) {
3105
- return { executor: options.executor, pool: void 0, ownedPool: void 0 };
3126
+ const { executor: executor2, pool: pool2 } = options;
3127
+ return {
3128
+ executor: pool2 === void 0 ? executor2 : withRootPool(executor2, new PgExecutor(pool2, { logger })),
3129
+ pool: pool2,
3130
+ ownedPool: void 0
3131
+ };
3106
3132
  }
3107
3133
  const pool = options.pool ?? new Pool({
3108
3134
  ...options.poolConfig,