@decaf-ts/for-postgres
Version:
template for ts projects
109 lines (108 loc) • 3.92 kB
TypeScript
import { Dispatch } from "@decaf-ts/core";
import { Pool, Notification } from "pg";
/**
* @description Dispatcher for PostgreSQL database change events
* @summary Handles the subscription to and processing of database change events from a PostgreSQL database,
* notifying observers when records are created, updated, or deleted
* @template Pool - The pg Pool type
* @param {number} [timeout=5000] - Timeout in milliseconds for notification requests
* @class PostgresDispatch
* @example
* ```typescript
* // Create a dispatcher for a PostgreSQL database
* const pool = new Pool({
* user: 'postgres',
* password: 'password',
* host: 'localhost',
* port: 5432,
* database: 'mydb'
* });
* const adapter = new PostgreSQLAdapterImpl(pool);
* const dispatch = new PostgreSQLDispatch();
*
* // The dispatcher will automatically subscribe to notifications
* // and notify observers when records change
* ```
* @mermaid
* classDiagram
* class Dispatch {
* +initialize()
* +updateObservers()
* }
* class PostgreSQLDispatch {
* -observerLastUpdate?: string
* -attemptCounter: number
* -timeout: number
* -client?: PoolClient
* +constructor(timeout)
* #notificationHandler()
* #initialize()
* }
* Dispatch <|-- PostgreSQLDispatch
*/
export declare class PostgresDispatch extends Dispatch<Pool> {
private timeout;
private observerLastUpdate?;
private attemptCounter;
private client?;
constructor(timeout?: number);
/**
* @description Processes database notification events
* @summary Handles the notifications from PostgreSQL LISTEN/NOTIFY mechanism,
* and notifies observers about record changes
* @param {Notification} notification - The notification from PostgreSQL
* @return {Promise<void>} A promise that resolves when all notifications have been processed
* @mermaid
* sequenceDiagram
* participant D as PostgreSQLDispatch
* participant L as Logger
* participant O as Observers
* Note over D: Receive notification from PostgreSQL
* D->>D: Parse notification payload
* D->>D: Extract table, operation, and ids
* D->>O: updateObservers(table, operation, ids)
* D->>D: Update observerLastUpdate
* D->>L: Log successful dispatch
*/
protected notificationHandler(notification: Notification): Promise<void>;
/**
* @description Initializes the dispatcher and subscribes to database notifications
* @summary Sets up the LISTEN mechanism to subscribe to PostgreSQL notifications
* and handles reconnection attempts if the connection fails
* @return {Promise<void>} A promise that resolves when the subscription is established
* @mermaid
* sequenceDiagram
* participant D as PostgreSQLDispatch
* participant S as subscribeToPostgreSQL
* participant DB as PostgreSQL Database
* participant L as Logger
* D->>S: Call subscribeToPostgreSQL
* S->>S: Check adapter and native
* alt No adapter or native
* S-->>S: throw InternalError
* end
* S->>DB: Connect client from pool
* S->>DB: LISTEN table_changes
* alt Success
* DB-->>S: Subscription established
* S-->>D: Promise resolves
* D->>L: Log successful subscription
* else Error
* DB-->>S: Error
* S->>S: Increment attemptCounter
* alt attemptCounter > 3
* S->>L: Log error
* S-->>D: Promise rejects
* else attemptCounter <= 3
* S->>L: Log retry
* S->>S: Wait timeout
* S->>S: Recursive call to subscribeToPostgreSQL
* end
* end
*/
protected initialize(): Promise<void>;
/**
* Cleanup method to release resources when the dispatcher is no longer needed
*/
cleanup(): void;
}