pg-transactional-outbox
Version:
A PostgreSQL based transactional outbox and inbox pattern implementation to support exactly once message processing (with at least once message delivery).
72 lines • 4.16 kB
TypeScript
import { Pool, PoolClient } from 'pg';
import { DatabaseClient } from './database';
import { TransactionalLogger } from './logger';
/**
* Sleep for a given amount of milliseconds
* @param milliseconds The time in milliseconds to sleep
* @returns The (void) promise to await
*/
export declare const sleep: (milliseconds: number) => Promise<void>;
/**
* Run a promise but make sure to wait only a maximum amount of time for it to finish.
* @param promise The promise to execute
* @param timeoutInMs The amount of time in milliseconds to wait for the promise to finish
* @param failureMessage The message for the error if the timeout was reached
* @returns The promise return value or a timeout error is thrown
*/
export declare const awaitWithTimeout: <T>(promise: () => Promise<T>, timeoutInMs: number, failureMessage?: string) => Promise<T>;
/**
* PostgreSQL available isolation levels. The isolation level "Read uncommitted"
* is the same as "Read committed" in PostgreSQL. And the readonly variants are
* not usable as the message must be marked as processed or the
* "finished_attempts" counter is updated for the message.
*/
export declare enum IsolationLevel {
/** Highest protection - no serialization anomaly */
Serializable = "SERIALIZABLE",
/** Second highest protection - no non-repeatable read */
RepeatableRead = "REPEATABLE READ",
/** Lowest protection (same as read uncommitted in PG) - non-repeatable reads possible */
ReadCommitted = "READ COMMITTED"
}
/**
* Open a transaction and execute the callback as part of the transaction.
* @param client The PostgreSQL database client
* @param callback The callback to execute DB commands with.
* @param isolationLevel The database transaction isolation level. Falls back to the default PostgreSQL transaction level if not provided.
* @returns The result of the callback (if any).
* @throws Any error from the database or the callback.
*/
export declare const executeTransaction: <T>(client: DatabaseClient, callback: (client: DatabaseClient) => Promise<T>, isolationLevel?: IsolationLevel) => Promise<T>;
/**
* Creates a PoolClient from the given pool and attaches the logger for error logging.
* If the logger is provided and has the log level 'trace' the client will track
* all the queries that were done during its lifetime. The queries are appended
* to errors that are thrown from that client/connection in the custom property
* `queryStack` on the pg library error.
* @param pool A PostgreSQL database pool from which clients can be created.
* @param logger A logger that will be registered for the on-error event of the client.
* @returns The PoolClient object
*/
export declare const getClient: (pool: Pool, logger?: TransactionalLogger) => Promise<PoolClient>;
/**
* Process incoming promises in a pool with a maximum size. When that size is not
* reached it tries to fill the processing pool up to the `getBatchSize`
* number. It runs in an endless loop until the signal `stopped` variable is set
* to true.
* @param processingPool The promise items in the pool which are being worked on
* @param getNextBatch Get the next items to fill up the pool. Gets the batch size input parameter from the `getBatchSize` parameter
* @param getBatchSize Async function that gets the number of batch items that are currently processed
* @param signal A signal object to stop the processing. Setting the stopped property to true will stop the loop
* @param maxDelayInMs The maximum number of milliseconds to wait before checking if new items are there
*/
export declare const processPool: (processingPool: Set<Promise<void>>, getNextBatch: (batchSize: number) => Promise<Promise<void>[]>, getBatchSize: (currentlyProcessed: number) => Promise<number>, signal: {
stopped: boolean;
}, maxDelayInMs: number) => Promise<void>;
/**
* Execute a function in a safe way e.g. pool.end where multiple callers could
* have called it where the second one will throw an error.
* @param it the function to execute
*/
export declare const justDoIt: (it: (() => Promise<void>) | (() => void)) => Promise<void>;
//# sourceMappingURL=utils.d.ts.map