import pg, { type QueryResult } from 'pg'; import { z } from 'zod'; export type QueryParams = Record | any[]; export interface CursorIterator { iterate: (batchSize: number) => AsyncGenerator; stream: (batchSize: number) => NodeJS.ReadWriteStream; } export interface PostgresPoolConfig extends pg.PoolConfig { errorOnUnusedParameters?: boolean; } /** @knipignore */ export declare class PostgresError extends Error { data: Record; constructor(message: string, data: Record); } /** * Escapes the given identifier for use in an SQL query. Useful for preventing * SQL injection. */ export declare function escapeIdentifier(identifier: string): string; export declare class PostgresPool { /** The pool from which clients will be acquired. */ private pool; /** * We use this to propagate the client associated with the current transaction * to any nested queries. In the past, we had some nasty bugs associated with * the fact that we tried to acquire new clients inside of transactions, which * ultimately lead to a deadlock. */ private alsClient; private searchSchema; /** Tracks the total number of queries executed by this pool. */ private _queryCount; private errorOnUnusedParameters; /** * Creates a new connection pool and attempts to connect to the database. */ initAsync(pgConfig: PostgresPoolConfig, idleErrorHandler: (error: Error, client: pg.PoolClient) => void): Promise; /** * Closes the connection pool. */ closeAsync(): Promise; /** * Gets a new client from the connection pool. The caller MUST call `release()` to * release the client, whether or not errors occurred while using * `client`. The client can call `done(truthy_value)` to force * destruction of the client, but this should not be used except in * unusual circumstances. */ getClientAsync(): Promise; /** * Performs a query with the given client. */ queryWithClientAsync(client: pg.PoolClient, sql: string, params: QueryParams): Promise; /** * Performs a query with the given client. Errors if the query returns more * than one row. */ queryWithClientOneRowAsync(client: pg.PoolClient, sql: string, params: QueryParams): Promise; /** * Performs a query with the given client. Errors if the query returns more * than one row. */ queryWithClientZeroOrOneRowAsync(client: pg.PoolClient, sql: string, params: QueryParams): Promise; /** * Rolls back the current transaction for the given client. */ rollbackWithClientAsync(client: pg.PoolClient): Promise; /** * Begins a new transaction. */ beginTransactionAsync(): Promise; /** * Commits the transaction if err is null, otherwise rollbacks the transaction. * Also releases the client. */ endTransactionAsync(client: pg.PoolClient, err: Error | null | undefined): Promise; /** * Runs the specified function inside of a transaction. The function will * receive a database client as an argument, but it can also make queries * as usual, and the correct client will be used automatically. * * The transaction will be rolled back if the function throws an error, and * will be committed otherwise. */ runInTransactionAsync(fn: (client: pg.PoolClient) => Promise): Promise; /** * Executes a query with the specified parameters. * * @deprecated Use {@link execute} instead. * * Using the return value of this function directly is not recommended. Instead, use * {@link queryRows}, {@link queryRow}, or {@link queryOptionalRow}. */ queryAsync(sql: string, params: QueryParams): Promise; /** * Executes a query with the specified parameters. Errors if the query does * not return exactly one row. * * @deprecated Use {@link executeRow} or {@link queryRow} instead. */ queryOneRowAsync(sql: string, params: QueryParams): Promise; /** * Executes a query with the specified parameters. Errors if the query * returns more than one row. */ queryZeroOrOneRowAsync(sql: string, params: QueryParams): Promise; /** * Calls the given sproc with the specified parameters. */ callAsync(functionName: string, params: any[]): Promise; /** * Calls the given sproc with the specified parameters. Errors if the * sproc does not return exactly one row. */ callOneRowAsync(functionName: string, params: any[]): Promise; /** * Calls the given sproc with the specified parameters. Errors if the * sproc returns more than one row. */ callZeroOrOneRowAsync(functionName: string, params: any[]): Promise; /** * Calls a sproc with the specified parameters using a specific client. */ callWithClientAsync(client: pg.PoolClient, functionName: string, params: any[]): Promise; /** * Calls a sproc with the specified parameters using a specific client. * Errors if the sproc does not return exactly one row. */ callWithClientOneRowAsync(client: pg.PoolClient, functionName: string, params: any[]): Promise; /** * Calls a function with the specified parameters using a specific client. * Errors if the sproc returns more than one row. */ callWithClientZeroOrOneRowAsync(client: pg.PoolClient, functionName: string, params: any[]): Promise; queryRows(sql: string, model: Model): Promise[]>; queryRows(sql: string, params: QueryParams, model: Model): Promise[]>; queryRow(sql: string, model: Model): Promise>; queryRow(sql: string, params: QueryParams, model: Model): Promise>; queryOptionalRow(sql: string, model: Model): Promise | null>; queryOptionalRow(sql: string, params: QueryParams, model: Model): Promise | null>; callRows(sql: string, model: Model): Promise[]>; callRows(sql: string, params: any[], model: Model): Promise[]>; callRow(sql: string, model: Model): Promise>; callRow(sql: string, params: any[], model: Model): Promise>; callOptionalRow(sql: string, model: Model): Promise | null>; callOptionalRow(sql: string, params: any[], model: Model): Promise | null>; queryScalars(sql: string, model: Model): Promise[]>; queryScalars(sql: string, params: QueryParams, model: Model): Promise[]>; queryScalar(sql: string, model: Model): Promise>; queryScalar(sql: string, params: QueryParams, model: Model): Promise>; queryOptionalScalar(sql: string, model: Model): Promise | null>; queryOptionalScalar(sql: string, params: QueryParams, model: Model): Promise | null>; callScalars(sql: string, model: Model): Promise[]>; callScalars(sql: string, params: any[], model: Model): Promise[]>; callScalar(sql: string, model: Model): Promise>; callScalar(sql: string, params: any[], model: Model): Promise>; callOptionalScalar(sql: string, model: Model): Promise | null>; callOptionalScalar(sql: string, params: any[], model: Model): Promise | null>; /** * Executes a query with the specified parameters. Returns the number of rows affected. */ execute(sql: string, params?: QueryParams): Promise; /** * Executes a query with the specified parameter, and errors if the query doesn't return exactly one row. */ executeRow(sql: string, params?: QueryParams): Promise; private queryCursorWithClient; queryCursor(sql: string, model: Model): Promise>>; queryCursor(sql: string, params: QueryParams, model: Model): Promise>>; private queryCursorInternal; /** * Set the schema to use for the search path. * * @param schema The schema name to use (can be "null" to unset the search path) */ setSearchSchema(schema: string | null): Promise; /** * Get the schema that is currently used for the search path. * * @returns schema in use (may be `null` to indicate no schema) */ getSearchSchema(): string | null; /** * Generate, set, and return a random schema name. * * @param prefix The prefix of the new schema, only the first 28 characters will be used (after lowercasing). * @returns The randomly-generated search schema. */ setRandomSearchSchemaAsync(prefix: string): Promise; /** * Deletes all schemas starting with the given prefix. * * @param prefix The prefix of the schemas to delete. */ clearSchemasStartingWith(prefix: string): Promise; /** The number of established connections. */ get totalCount(): number; /** The number of idle connections. */ get idleCount(): number; /** The number of queries waiting for a connection to become available. */ get waitingCount(): number; /** The total number of queries that have been executed by this pool. */ get queryCount(): number; } //# sourceMappingURL=pool.d.ts.map