import pg from 'pg' import { PostgresTransaction, PostgresTransactionImpl } from './PostgresTransaction.js' import { Database } from '../../Database.js' import type { DbBaseConfig, DbConfig } from '../../types.js' /** * Represents a Postgres database connection that extends the Database class. * @extends Database */ export class PostgresDatabase extends Database { /** * A public static property that represents a connection pool for a PostgreSQL database. * This property is used to manage and provide connections to the PostgreSQL database. */ private static pgProvider = pg.Pool /** * Represents a connection pool to manage multiple client connections to the database. */ public readonly client: pg.Pool /** * A private property that represents a connection pool for clients. * @type {Pool | undefined} */ public readonly readClient?: pg.Pool /** * Constructor for creating a new instance of a database connection using Postgres. * @param {DbConfig<'pg'>} config - The configuration object for the Postgres database. * @returns None */ constructor(config: DbConfig<'pg'>) { super(config) this.client = this.providerFactory(config) if (config.readReplica) this.readClient = this.providerFactory(config.readReplica) } /** * Creates a new Postgres transaction using the provided client and read client. * @returns {Promise} A promise that resolves to a new PostgresTransaction object. */ public override async transaction(): Promise { return PostgresTransactionImpl.newTransaction(this.client, this, this.readClient) } private providerFactory(config: DbBaseConfig) { return new PostgresDatabase.pgProvider({ host: config.host, port: config.port, user: config.username, password: config.password, database: config.database, max: config.maxConnections, }) } }