UNPKG

2.37 kBTypeScriptView Raw
1import { Factory } from 'generic-pool';
2import { DBTransaction } from './DBTransaction';
3import { ConnectionPool, ConnectionConfig, PoolConfig } from './ConnectionPool';
4/**
5 * Configuration for a DBClient, this includes master and slave connection pool configurations.
6 * Only the master pool config is necessary, if a slave pool is specified, any readonly transactions will use it. If not,
7 * all transactions will use the master connection pool.
8 */
9export interface DBClientConfig<C extends ConnectionConfig> {
10 name: string;
11 master: PoolConfig<C>;
12 slave?: PoolConfig<C>;
13}
14export declare abstract class DBClient<C, T extends DBTransaction<C>, CC extends ConnectionConfig, PC extends DBClientConfig<CC>> {
15 static startMethod: string;
16 static stopMethod: string;
17 clientConfiguration: PC;
18 name: string;
19 masterPool: ConnectionPool<C>;
20 slavePool: ConnectionPool<C>;
21 constructor(clientConfiguration: PC);
22 abstract getNewDBTransaction(readonly: boolean, connectionPool: ConnectionPool<C>): T;
23 abstract getDefaultConnectionConfig(): CC;
24 /**
25 * Runs a function in a transaction. The function must receive one parameter that will be of class
26 * {MysqlTransaction} and that you need to use to run all queries in this transaction
27 *
28 * @param {boolean} readonly Whether the transaction needs to be readonly or not
29 * @param {Function} func A function that returns a promise that will execute all the queries wanted in this transaction
30 * @returns {Promise} A promise that will execute the whole transaction
31 */
32 runInTransaction(readonly: boolean, func: (transaction: DBTransaction<C>) => Promise<any>): Promise<any>;
33 getPoolForReadonly(readonly: boolean): ConnectionPool<C>;
34 /**
35 * Shorthand for a single readonly query
36 * @param sql query to run
37 * @param binds binds
38 */
39 read<P>(sql: string, ...binds: any[]): Promise<P[]>;
40 /**
41 * Shorthand to run a single sql.
42 * Easy to mock
43 * @param readonly
44 * @param sql
45 * @param binds
46 */
47 executeSql(readonly: boolean, sql: string, ...binds: any[]): Promise<any>;
48 abstract getConnectionFactory(name: string, connectionConfig: CC): Factory<C>;
49 initialise(): Promise<void>;
50 stop(): Promise<void>;
51 ping(readonly: boolean): Promise<void>;
52 abstract getPingQuery(): string;
53}