Skip to content

postgres ​

Classes ​

PostgresStorage ​

Defined in: src/storage/postgres.storage.ts:110

Postgres-backed implementation of IdempotencyStorage.

Stores each record as a row in idempotency_records (override via tableName). Atomic NX is enforced by the primary-key constraint on key combined with INSERT ... ON CONFLICT DO UPDATE WHERE expires_at <= now(). Token-based compare-and-set is enforced by WHERE token = $ clauses on complete() and delete(). Lazy expiration is enforced by WHERE expires_at > now() in get().

For active cleanup of expired rows see PostgresSweepService.

Implements ​

Constructors ​

Constructor ​
ts
new PostgresStorage(options): PostgresStorage;

Defined in: src/storage/postgres.storage.ts:116

Parameters ​
ParameterType
optionsPostgresStorageOptions
Returns ​

PostgresStorage

Methods ​

close() ​
ts
close(): Promise<void>;

Defined in: src/storage/postgres.storage.ts:265

Returns ​

Promise<void>

complete() ​
ts
complete(
   key,
   token,
   response,
ttlSeconds): Promise<MutateResult>;

Defined in: src/storage/postgres.storage.ts:201

Transitions a PROCESSING record to COMPLETED and stores the captured response, but ONLY if the record is unexpired and its token matches the caller's. Returns 'stale' for missing/expired records, token mismatches, and an already COMPLETED record (including the same token). A stale operation must not overwrite a response or refresh TTL.

On 'ok', implementations must refresh the TTL to ttlSeconds.

Parameters ​
ParameterTypeDescription
keystring-
tokenstring-
responseCompleteResponse-
ttlSecondsnumbercompleted record lifetime, an integer in [1, 2_147_483_647] seconds
Returns ​

Promise<MutateResult>

Throws ​

RangeError if ttlSeconds is invalid, before ownership or state checks

Implementation of ​

IdempotencyStorage.complete

create() ​
ts
create(
   key,
   fingerprint,
ttlSeconds): Promise<CreateResult>;

Defined in: src/storage/postgres.storage.ts:171

Atomically creates a PROCESSING record. On success, returns an opaque token that the caller MUST pass back to complete() / delete(). An expired record is absent for NX purposes, regardless of physical cleanup.

Parameters ​
ParameterTypeDescription
keystringthe idempotency key from the client header (already scoped by the interceptor to include endpoint identity)
fingerprintstring | undefinedSHA-256 of the request body, or undefined if fingerprinting is off
ttlSecondsnumberlifetime of the lock, an integer in [1, 2_147_483_647] seconds
Returns ​

Promise<CreateResult>

Throws ​

RangeError if ttlSeconds is invalid, before NX or expiry checks

Implementation of ​

IdempotencyStorage.create

createSchema() ​
ts
static createSchema(pool, tableName?): Promise<void>;

Defined in: src/storage/postgres.storage.ts:280

Idempotently creates the records table and supporting index. Safe to call multiple times. Used by autoCreateSchema=true and available as a public helper for code-driven migrations.

Parameters ​
ParameterTypeDefault value
poolPoolundefined
tableNamestringDEFAULT_TABLE_NAME
Returns ​

Promise<void>

delete() ​
ts
delete(key, token): Promise<MutateResult>;

Defined in: src/storage/postgres.storage.ts:240

Removes a record, but ONLY if the caller's token matches. Returns 'ok' if the record was removed OR was absent/expired (idempotent cleanup), and 'stale' only if a DIFFERENT record (with a different token) is currently stored and unexpired under this key. Expired physical rows may remain for later cleanup; delete success does not promise immediate physical removal.

Parameters ​
ParameterType
keystring
tokenstring
Returns ​

Promise<MutateResult>

Implementation of ​

IdempotencyStorage.delete

get() ​
ts
get(key): Promise<IdempotencyRecord | null>;

Defined in: src/storage/postgres.storage.ts:138

Fetches a record by key. Returns null if the key does not exist or has expired.

Parameters ​
ParameterType
keystring
Returns ​

Promise<IdempotencyRecord | null>

Implementation of ​

IdempotencyStorage.get

onModuleDestroy() ​
ts
onModuleDestroy(): Promise<void>;

Defined in: src/storage/postgres.storage.ts:271

Returns ​

Promise<void>

Implementation of ​
ts
OnModuleDestroy.onModuleDestroy

onModuleInit() ​
ts
onModuleInit(): Promise<void>;

Defined in: src/storage/postgres.storage.ts:132

Returns ​

Promise<void>


PostgresSweepService ​

Defined in: src/services/postgres-sweep.service.ts:38

Optional service that periodically deletes expired idempotency records.

Lazy expiration in PostgresStorage.get already guarantees correctness; this service exists only to keep disk usage and dead tuples bounded in long-running deployments.

Multi-instance safety: each sweep wraps DELETE in pg_try_advisory_lock(hashtext('idempotency-sweep')). Concurrent replicas will see a lock contention and skip — no DELETE storms.

Implements ​

  • OnModuleInit
  • OnModuleDestroy

Constructors ​

Constructor ​
ts
new PostgresSweepService(storage, options?): PostgresSweepService;

Defined in: src/services/postgres-sweep.service.ts:42

Parameters ​
ParameterType
storagePostgresStorage
optionsSweepOptions
Returns ​

PostgresSweepService

Methods ​

onModuleDestroy() ​
ts
onModuleDestroy(): Promise<void>;

Defined in: src/services/postgres-sweep.service.ts:61

Returns ​

Promise<void>

Implementation of ​
ts
OnModuleDestroy.onModuleDestroy

onModuleInit() ​
ts
onModuleInit(): Promise<void>;

Defined in: src/services/postgres-sweep.service.ts:52

Returns ​

Promise<void>

Implementation of ​
ts
OnModuleInit.onModuleInit

sweep() ​
ts
sweep(): Promise<{
  deleted: number;
}>;

Defined in: src/services/postgres-sweep.service.ts:69

Runs one sweep cycle. Returns the number of rows deleted (0 if another replica holds the advisory lock for this cycle).

Returns ​

Promise<{ deleted: number; }>

Interfaces ​

PostgresStorageOptions ​

Defined in: src/storage/postgres.storage.ts:22

Constructor options for PostgresStorage.

Provide either a pre-built pool (recommended — lets the consumer manage connection lifecycle) OR a connection config that the storage uses to lazily build its own pool.

Properties ​

autoCreateSchema? ​
ts
optional autoCreateSchema?: boolean;

Defined in: src/storage/postgres.storage.ts:38

If true, run CREATE TABLE IF NOT EXISTS and matching index on module init. Defaults to false. Recommended only for development.

connection? ​
ts
optional connection?: PoolConfig;

Defined in: src/storage/postgres.storage.ts:26

pg PoolConfig used to lazily construct an internal pool.

pool? ​
ts
optional pool?: Pool;

Defined in: src/storage/postgres.storage.ts:24

A pre-built pg Pool. Wins over connection if both are supplied.

poolFactory? ​
ts
optional poolFactory?: (connection) => Pool;

Defined in: src/storage/postgres.storage.ts:28

Test-only seam: custom factory used in place of new Pool(connection).

Parameters ​
ParameterType
connectionPoolConfig
Returns ​

Pool

tableName? ​
ts
optional tableName?: string;

Defined in: src/storage/postgres.storage.ts:33

Table name used for idempotency records.

Default ​
ts
'idempotency_records'

SweepOptions ​

Defined in: src/services/postgres-sweep.service.ts:17

Properties ​

enabled ​
ts
enabled: boolean;

Defined in: src/services/postgres-sweep.service.ts:19

When false, the service is wired up but never schedules a sweep.

intervalMs? ​
ts
optional intervalMs?: number;

Defined in: src/services/postgres-sweep.service.ts:21

Sweep cadence. Defaults to 60_000 (1 minute).

Released under the MIT License.