import { QualifiedTablename } from '../util/tablename.js'; import { Row, Statement } from '../util/types.js'; export type UncoordinatedDatabaseAdapter = Pick; export interface DatabaseAdapter { readonly defaultNamespace: 'main' | 'public'; run(statement: Statement): Promise; runInTransaction(...statements: Statement[]): Promise; /** * This method is useful to execute several queries in isolation from any other queries/transactions executed through this adapter. * Useful to execute queries that cannot be executed inside a transaction (e.g. SQLite does not allow the `foreign_keys` PRAGMA to be modified in a transaction). * In that case we can use this `group` method: * ``` * await adapter.runExclusively(async (adapter) => { * await adapter.run({ sql: 'PRAGMA foreign_keys = OFF;' }) * ... * await adapter.run({ sql: 'PRAGMA foreign_keys = ON;' }) * }) * ``` * This snippet above ensures that no other query/transaction will be interleaved when the foreign keys are disabled. * @param f Function that is guaranteed to be executed in isolation from other queries/transactions executed by this adapter. */ runExclusively(f: (adapter: UncoordinatedDatabaseAdapter) => Promise | T): Promise; query(statement: Statement): Promise; /** * Runs the provided __non-async__ function inside a transaction. * * The function may not use async/await otherwise the transaction may commit before * the queries are actually executed. This is a limitation of some adapters, that the * function passed to the transaction runs "synchronously" through callbacks without * releasing the event loop. */ transaction(f: (tx: Transaction, setResult: (res: T) => void) => void): Promise; tableNames(statement: Statement): QualifiedTablename[]; } export declare class TableNameImpl { tableNames({ sql }: Statement): QualifiedTablename[]; } export interface Transaction { run(statement: Statement, successCallback?: (tx: Transaction, result: RunResult) => void, errorCallback?: (error: any) => void): void; query(statement: Statement, successCallback: (tx: Transaction, res: Row[]) => void, errorCallback?: (error: any) => void): void; } export interface RunResult { rowsAffected: number; }