import type { DatabaseConnection } from '../../driver/database-connection.js'; import type { AbortableOperationOptions } from '../../util/abort.js'; /** * Config for the PostgreSQL dialect. */ export interface PostgresDialectConfig { /** * A `pg` `Client` constructor, to be used for connecting to the database * outside of the `pool` to avoid waiting for an idle connection. * * This is useful for cancelling queries on the database side. * * Defaults to the `Pool`'s undocumented `Client` member, if it exists. */ controlClient?: PostgresClientConstructor; /** * https://github.com/brianc/node-postgres/tree/master/packages/pg-cursor * * ```ts * import { PostgresDialect } from 'kysely' * import { Pool } from 'pg' * import Cursor from 'pg-cursor' * // or import * as Cursor from 'pg-cursor' * * new PostgresDialect({ * cursor: Cursor, * pool: new Pool('postgres://localhost:5432/mydb') * }) * ``` */ cursor?: PostgresCursorConstructor; /** * Called once for each created connection. */ onCreateConnection?: (connection: DatabaseConnection, options?: AbortableOperationOptions) => Promise; /** * Called every time a connection is acquired from the pool. */ onReserveConnection?: (connection: DatabaseConnection, options?: AbortableOperationOptions) => Promise; /** * A postgres `Pool` instance or a function that returns one. * * If a function is provided, it's called once when the first query is executed. * * https://node-postgres.com/apis/pool */ pool: PostgresPool | ((options?: AbortableOperationOptions) => Promise); } /** * This interface is the subset of pg driver's `Pool` class that * kysely needs. * * We don't use the type from `pg` here to not have a dependency to it. * * https://node-postgres.com/apis/pool */ export interface PostgresPool { Client?: PostgresClientConstructor; connect(): Promise; end(): Promise; options: object; } /** * This interface is the subset of pg driver's `Client` class that * kysely needs. * * We don't use the type from `pg` here to not have a dependency to it. * * https://node-postgres.com/apis/client */ export interface PostgresClient { connect(): Promise; end(): void; processID?: number; query(sql: string, parameters: ReadonlyArray): Promise>; query(cursor: PostgresCursor): PostgresCursor; } export type PostgresClientConstructor = new (options: any) => PostgresClient; /** * This interface is the subset of pg driver's `Client` class that * is returned by the `Pool` class, and that kysely needs. * * We don't use the type from `pg` here to not have a dependency to it. * * https://node-postgres.com/apis/pool#releasing-clients */ export interface PostgresPoolClient extends Omit { release(): void; } /** * This interface is pg driver's `Cursor` class that kysely needs. * * We don't use the type from `pg-cursor` here to not have a dependency to it. * * https://node-postgres.com/apis/cursor */ export interface PostgresCursor { read(rowsCount: number): Promise; close(): Promise; } /** * This interface is pg driver's `Cursor` class constructor that kysely needs. * * We don't use the type from `pg-cursor` here to not have a dependency to it. * * https://node-postgres.com/apis/cursor#constructor */ export type PostgresCursorConstructor = new (sql: string, parameters: unknown[]) => PostgresCursor; /** * This interface is the subset of pg driver's `Result` shape that kysely needs. * * We don't use the type from `pg` here to not have a dependency to it. * * https://node-postgres.com/apis/result */ export interface PostgresQueryResult { command: 'UPDATE' | 'DELETE' | 'INSERT' | 'SELECT' | 'MERGE'; rowCount: number; rows: R[]; } export interface PostgresStream { [Symbol.asyncIterator](): AsyncIterableIterator; }