/// import { Readable } from 'stream'; import { QueryableType } from '@databases/shared'; import { SQL, SQLQuery } from '@databases/sql'; import TransactionOptions from './TransactionOptions'; import QueryStreamOptions from './QueryStreamOptions'; export default interface Queryable { readonly type: QueryableType; readonly sql: SQL; query(query: SQLQuery): Promise; query(query: SQLQuery[]): Promise; queryStream(query: SQLQuery, options?: QueryStreamOptions): AsyncIterable; queryNodeStream(query: SQLQuery, options?: QueryStreamOptions): 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; } 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;