@relaybus/postgres 0.2.0 → 0.3.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
@@ -1,10 +1,20 @@
1
- import { P as PostgresExecutor, a as PostgresParameter, b as PostgresQueryOptions, c as PostgresRow } from './executor-VZGmsMX4.cjs';
2
- import { RelayBusLogger, EventStore, AppendEntry, AppendResult, ClaimRequest, ClaimedDelivery, LeaseReference, ErrorInfo, DeliveryQuery, DeliveryRecord, PurgeRequest, PurgeResult, EventStoreStatistics } from '@relaybus/core';
1
+ import { P as PostgresExecutor, a as PostgresParameter, b as PostgresQueryOptions, c as PostgresRow } from './executor-CfJgktC6.cjs';
2
+ import { RelayBusLogger, EventStore, EventStoreTransactionOptions, AppendEntry, AppendResult, ClaimRequest, ClaimedDelivery, LeaseReference, ErrorInfo, DeliveryQuery, DeliveryRecord, PurgeRequest, PurgeResult, EventStoreStatistics, CloseOptions, RelayBusError } from '@relaybus/core';
3
3
  import { PoolClient, Pool, PoolConfig } from 'pg';
4
4
 
5
5
  interface PgExecutorOptions {
6
6
  /** Receives the warning sent when prepared statements are switched off. Default `console`. */
7
7
  readonly logger?: RelayBusLogger;
8
+ /**
9
+ * The pool is this executor's alone (the store sets it for the pool it creates from
10
+ * `connectionString`). A transaction budget then becomes a session setting of the connection, sent
11
+ * with `BEGIN` only when the connection does not have it yet, and a transaction without a budget on
12
+ * such a connection puts the session defaults back in its `BEGIN`. Statements outside a
13
+ * transaction run under the budget the connection last got. Never set it for a pool other code
14
+ * uses, or behind a pooler in transaction mode (session settings would reach other clients); the
15
+ * executor stops it by itself when it notices a pooler (see prepared statements). Default `false`.
16
+ */
17
+ readonly sessionBudget?: boolean;
8
18
  }
9
19
  /**
10
20
  * Executor for a `pg` {@link Pool}. A transaction is a dedicated pool client
@@ -24,7 +34,15 @@ interface PgExecutorOptions {
24
34
  declare class PgExecutor implements PostgresExecutor<PoolClient> {
25
35
  #private;
26
36
  constructor(pool: Pool, options?: PgExecutorOptions);
37
+ /** The connection listens for `error` during the transaction, so a server-ended session fails the transaction instead of the process. */
38
+ readonly sessionTimeouts = true;
27
39
  transaction<T>(work: (transaction: PoolClient) => Promise<T>): Promise<T>;
40
+ /**
41
+ * Sends `BEGIN` and the budget settings as one simple query (the value as a literal: the simple
42
+ * query protocol has no parameters), so the budget costs no round trip. With `sessionBudget` a
43
+ * connection that already keeps this budget only gets `BEGIN`.
44
+ */
45
+ transactionWithBudget<T>(work: (transaction: PoolClient) => Promise<T>, timeoutMs: number): Promise<T>;
28
46
  query(sql: string, parameters: readonly PostgresParameter[], transaction?: PoolClient, options?: PostgresQueryOptions): Promise<readonly PostgresRow[]>;
29
47
  }
30
48
 
@@ -227,10 +245,18 @@ declare class PostgresEventStore<TTransaction = PoolClient> implements EventStor
227
245
  * `createPostgresSchemaSql()` instead.
228
246
  */
229
247
  setup(): Promise<void>;
230
- transaction<T>(work: (transaction: TTransaction) => Promise<T>): Promise<T>;
248
+ /**
249
+ * Runs `work` in a transaction of the executor. With `options.timeoutMs` (from `bus.transaction`)
250
+ * the server holds the transaction to the budget: `statement_timeout`, and for executors with
251
+ * `sessionTimeouts` (`pg`) also `idle_in_transaction_session_timeout` and, on PostgreSQL 17+,
252
+ * `transaction_timeout`, all scoped to the transaction. An executor with `transactionWithBudget`
253
+ * (`PgExecutor`) sets them with `BEGIN`, so the budget costs no round trip; on a pool the store
254
+ * created they are kept per connection and sent only when the connection does not have them yet.
255
+ */
256
+ transaction<T>(work: (transaction: TTransaction) => Promise<T>, options?: EventStoreTransactionOptions): Promise<T>;
231
257
  /**
232
258
  * Writes the entries in `transaction`, normally with one statement
233
- * ({@link PostgresStatements.insertFreshEvents}): keyed events reserve their sequence numbers (locking
259
+ * (`AppendStatements.insertFreshEvents`): keyed events reserve their sequence numbers (locking
234
260
  * the key rows in sorted order, so publishers cannot deadlock inside one call), then the new events and
235
261
  * their deliveries are inserted, ordered by primary key, and one notification wakes the dispatchers
236
262
  * when a delivery is due now. Events that already existed get their missing handlers' deliveries in
@@ -242,14 +268,14 @@ declare class PostgresEventStore<TTransaction = PoolClient> implements EventStor
242
268
  append(transaction: TTransaction, entries: readonly AppendEntry[], now: Date): Promise<AppendResult>;
243
269
  /**
244
270
  * Claims up to `limit` due deliveries, normally with one statement
245
- * ({@link PostgresStatements.claimLanes}): from the lanes this owner already holds, then from free lanes
271
+ * (`LaneStatements.claimLanes`): from the lanes this owner already holds, then from free lanes
246
272
  * it takes in the same statement, recording the batch in the lane rows. A claim that would have to take
247
273
  * over a lane whose previous holder still runs deliveries under an expired lease continues in a
248
274
  * transaction that charges their lost attempts first, taking free lanes a few at a time (4, 8, 16, ...);
249
275
  * so does a claim whose statement kept seeing a lane it holds change under it, and one that came up
250
276
  * short with more free lanes left than it looked at. The step that fails after
251
277
  * the first one has recorded deliveries returns those instead of failing: a recorded delivery nobody
252
- * received would be held for a whole lease. See {@link PostgresStatements.claim} for the rules a delivery
278
+ * received would be held for a whole lease. See `ClaimStatements.claim` for the rules a delivery
253
279
  * must satisfy.
254
280
  */
255
281
  claim(request: ClaimRequest): Promise<ClaimedDelivery[]>;
@@ -285,8 +311,22 @@ declare class PostgresEventStore<TTransaction = PoolClient> implements EventStor
285
311
  */
286
312
  purge(request: PurgeRequest): Promise<PurgeResult>;
287
313
  statistics(now: Date): Promise<EventStoreStatistics>;
288
- /** Ends the pool this store created from `connectionString`. Pools and executors you passed in stay open. */
289
- close(): Promise<void>;
314
+ /**
315
+ * Ends the pool this store created from `connectionString`. Pools and executors you passed in stay
316
+ * open. Once `options.signal` aborts (the shutdown deadline passed), the connections the store opened
317
+ * itself are destroyed: queries still running on them fail at once instead of holding the shutdown.
318
+ */
319
+ close(options?: CloseOptions): Promise<void>;
320
+ }
321
+
322
+ /**
323
+ * A claim was refused because giving back the deliveries of an earlier, failed
324
+ * claim still fails. Claiming before that would take the owner's live lanes for
325
+ * foreign ones; the next claim tries the reconciliation again.
326
+ */
327
+ declare class ReconcileFailedError extends RelayBusError {
328
+ name: string;
329
+ readonly code: string;
290
330
  }
291
331
 
292
- 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, createPostgresSchemaSql, createPostgresSchemaStatements, resolvePostgresNames };
332
+ 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 };
package/dist/index.d.ts CHANGED
@@ -1,10 +1,20 @@
1
- import { P as PostgresExecutor, a as PostgresParameter, b as PostgresQueryOptions, c as PostgresRow } from './executor-VZGmsMX4.js';
2
- import { RelayBusLogger, EventStore, AppendEntry, AppendResult, ClaimRequest, ClaimedDelivery, LeaseReference, ErrorInfo, DeliveryQuery, DeliveryRecord, PurgeRequest, PurgeResult, EventStoreStatistics } from '@relaybus/core';
1
+ import { P as PostgresExecutor, a as PostgresParameter, b as PostgresQueryOptions, c as PostgresRow } from './executor-CfJgktC6.js';
2
+ import { RelayBusLogger, EventStore, EventStoreTransactionOptions, AppendEntry, AppendResult, ClaimRequest, ClaimedDelivery, LeaseReference, ErrorInfo, DeliveryQuery, DeliveryRecord, PurgeRequest, PurgeResult, EventStoreStatistics, CloseOptions, RelayBusError } from '@relaybus/core';
3
3
  import { PoolClient, Pool, PoolConfig } from 'pg';
4
4
 
5
5
  interface PgExecutorOptions {
6
6
  /** Receives the warning sent when prepared statements are switched off. Default `console`. */
7
7
  readonly logger?: RelayBusLogger;
8
+ /**
9
+ * The pool is this executor's alone (the store sets it for the pool it creates from
10
+ * `connectionString`). A transaction budget then becomes a session setting of the connection, sent
11
+ * with `BEGIN` only when the connection does not have it yet, and a transaction without a budget on
12
+ * such a connection puts the session defaults back in its `BEGIN`. Statements outside a
13
+ * transaction run under the budget the connection last got. Never set it for a pool other code
14
+ * uses, or behind a pooler in transaction mode (session settings would reach other clients); the
15
+ * executor stops it by itself when it notices a pooler (see prepared statements). Default `false`.
16
+ */
17
+ readonly sessionBudget?: boolean;
8
18
  }
9
19
  /**
10
20
  * Executor for a `pg` {@link Pool}. A transaction is a dedicated pool client
@@ -24,7 +34,15 @@ interface PgExecutorOptions {
24
34
  declare class PgExecutor implements PostgresExecutor<PoolClient> {
25
35
  #private;
26
36
  constructor(pool: Pool, options?: PgExecutorOptions);
37
+ /** The connection listens for `error` during the transaction, so a server-ended session fails the transaction instead of the process. */
38
+ readonly sessionTimeouts = true;
27
39
  transaction<T>(work: (transaction: PoolClient) => Promise<T>): Promise<T>;
40
+ /**
41
+ * Sends `BEGIN` and the budget settings as one simple query (the value as a literal: the simple
42
+ * query protocol has no parameters), so the budget costs no round trip. With `sessionBudget` a
43
+ * connection that already keeps this budget only gets `BEGIN`.
44
+ */
45
+ transactionWithBudget<T>(work: (transaction: PoolClient) => Promise<T>, timeoutMs: number): Promise<T>;
28
46
  query(sql: string, parameters: readonly PostgresParameter[], transaction?: PoolClient, options?: PostgresQueryOptions): Promise<readonly PostgresRow[]>;
29
47
  }
30
48
 
@@ -227,10 +245,18 @@ declare class PostgresEventStore<TTransaction = PoolClient> implements EventStor
227
245
  * `createPostgresSchemaSql()` instead.
228
246
  */
229
247
  setup(): Promise<void>;
230
- transaction<T>(work: (transaction: TTransaction) => Promise<T>): Promise<T>;
248
+ /**
249
+ * Runs `work` in a transaction of the executor. With `options.timeoutMs` (from `bus.transaction`)
250
+ * the server holds the transaction to the budget: `statement_timeout`, and for executors with
251
+ * `sessionTimeouts` (`pg`) also `idle_in_transaction_session_timeout` and, on PostgreSQL 17+,
252
+ * `transaction_timeout`, all scoped to the transaction. An executor with `transactionWithBudget`
253
+ * (`PgExecutor`) sets them with `BEGIN`, so the budget costs no round trip; on a pool the store
254
+ * created they are kept per connection and sent only when the connection does not have them yet.
255
+ */
256
+ transaction<T>(work: (transaction: TTransaction) => Promise<T>, options?: EventStoreTransactionOptions): Promise<T>;
231
257
  /**
232
258
  * Writes the entries in `transaction`, normally with one statement
233
- * ({@link PostgresStatements.insertFreshEvents}): keyed events reserve their sequence numbers (locking
259
+ * (`AppendStatements.insertFreshEvents`): keyed events reserve their sequence numbers (locking
234
260
  * the key rows in sorted order, so publishers cannot deadlock inside one call), then the new events and
235
261
  * their deliveries are inserted, ordered by primary key, and one notification wakes the dispatchers
236
262
  * when a delivery is due now. Events that already existed get their missing handlers' deliveries in
@@ -242,14 +268,14 @@ declare class PostgresEventStore<TTransaction = PoolClient> implements EventStor
242
268
  append(transaction: TTransaction, entries: readonly AppendEntry[], now: Date): Promise<AppendResult>;
243
269
  /**
244
270
  * Claims up to `limit` due deliveries, normally with one statement
245
- * ({@link PostgresStatements.claimLanes}): from the lanes this owner already holds, then from free lanes
271
+ * (`LaneStatements.claimLanes`): from the lanes this owner already holds, then from free lanes
246
272
  * it takes in the same statement, recording the batch in the lane rows. A claim that would have to take
247
273
  * over a lane whose previous holder still runs deliveries under an expired lease continues in a
248
274
  * transaction that charges their lost attempts first, taking free lanes a few at a time (4, 8, 16, ...);
249
275
  * so does a claim whose statement kept seeing a lane it holds change under it, and one that came up
250
276
  * short with more free lanes left than it looked at. The step that fails after
251
277
  * the first one has recorded deliveries returns those instead of failing: a recorded delivery nobody
252
- * received would be held for a whole lease. See {@link PostgresStatements.claim} for the rules a delivery
278
+ * received would be held for a whole lease. See `ClaimStatements.claim` for the rules a delivery
253
279
  * must satisfy.
254
280
  */
255
281
  claim(request: ClaimRequest): Promise<ClaimedDelivery[]>;
@@ -285,8 +311,22 @@ declare class PostgresEventStore<TTransaction = PoolClient> implements EventStor
285
311
  */
286
312
  purge(request: PurgeRequest): Promise<PurgeResult>;
287
313
  statistics(now: Date): Promise<EventStoreStatistics>;
288
- /** Ends the pool this store created from `connectionString`. Pools and executors you passed in stay open. */
289
- close(): Promise<void>;
314
+ /**
315
+ * Ends the pool this store created from `connectionString`. Pools and executors you passed in stay
316
+ * open. Once `options.signal` aborts (the shutdown deadline passed), the connections the store opened
317
+ * itself are destroyed: queries still running on them fail at once instead of holding the shutdown.
318
+ */
319
+ close(options?: CloseOptions): Promise<void>;
320
+ }
321
+
322
+ /**
323
+ * A claim was refused because giving back the deliveries of an earlier, failed
324
+ * claim still fails. Claiming before that would take the owner's live lanes for
325
+ * foreign ones; the next claim tries the reconciliation again.
326
+ */
327
+ declare class ReconcileFailedError extends RelayBusError {
328
+ name: string;
329
+ readonly code: string;
290
330
  }
291
331
 
292
- 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, createPostgresSchemaSql, createPostgresSchemaStatements, resolvePostgresNames };
332
+ 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 };