/// import { Readable } from 'stream'; import { QueryableType } from '@databases/shared'; import { SQL, SQLQuery } from '@databases/sql'; import AbortSignal from './AbortSignal'; import TransactionOptions from './TransactionOptions'; export default interface Queryable { readonly type: QueryableType; readonly sql: SQL; query(query: SQLQuery): Promise; query(query: SQLQuery[]): Promise; queryStream(query: SQLQuery, { batchSize, signal }: { batchSize?: number; signal?: AbortSignal; }): AsyncIterable; queryNodeStream(query: SQLQuery, options?: { highWaterMark?: number; batchSize?: number; }): Readable; task(fn: (connection: Connection | Transaction) => Promise): Promise; tx(fn: (connection: Transaction) => Promise, options?: TransactionOptions): Promise; addPostCommitStep(fn: () => Promise): Promise; } /** * A "Transaction" on the database will be committed once * the async function returns, and will be aborted if any * queries fail or if the function throws an exception. */ export interface Transaction extends Queryable { readonly type: QueryableType.Transaction; task(fn: (connection: Transaction) => Promise): Promise; tx(fn: (connection: Transaction) => Promise): Promise; } /** * A "Connection" represents a single connection to the database, * although you can send queries in parallel, if they share * one "Connection" they will actually be run one at a time on the * underlying database. * * Most of the time you can ignore these and just focus on * Transactions and ConnectionPools, or the more general * "Queryable". */ export interface Connection extends Queryable { readonly type: QueryableType.Connection; task(fn: (connection: Connection) => Promise): Promise; } /** * A "ConnectionPool" represents a collection of database * connections that are managed for you automatically. You can * query this directly without worrying about allocating connections. * You can also use a Transaction to provide better guarantees about * behaviour when running many operations in parallel. */ export interface ConnectionPool extends Queryable { readonly type: QueryableType.ConnectionPool; task(fn: (connection: Connection) => Promise): Promise; dispose(): Promise; registerTypeParser(type: number | string, parser: (value: string) => T): Promise<(value: string) => T>; getTypeParser(type: number | string): Promise<(value: string) => any>; /** * Parses an n-dimensional array * * @param value The string value from the database * @param entryParser A transform function to apply to each string */ parseArray(value: string, entryParser?: (entry: string | null) => any): any[]; /** * Parse a composite value and get a tuple of strings where * each string represents one attribute. * * @param value The raw string. */ parseComposite(value: string): string[]; } export declare function isConnectionPool(queryable: Queryable): queryable is ConnectionPool; export declare function isConnection(queryable: Queryable): queryable is Connection; export declare function isTransaction(queryable: Queryable): queryable is Transaction;