UNPKG

@naturalcycles/mysql-lib

Version:

MySQL client implementing CommonDB interface

76 lines (75 loc) 3.3 kB
import type { CommonDB, CommonDBCreateOptions, CommonDBOptions, CommonDBSaveOptions, CommonDBSupport, RunQueryResult } from '@naturalcycles/db-lib'; import { BaseCommonDB, DBQuery } from '@naturalcycles/db-lib'; import type { JsonSchemaObject, JsonSchemaRootObject } from '@naturalcycles/js-lib/json-schema'; import type { CommonLogger } from '@naturalcycles/js-lib/log'; import type { ObjectWithId } from '@naturalcycles/js-lib/types'; import type { ReadableTyped } from '@naturalcycles/nodejs-lib/stream'; import type { Connection, Pool, PoolConfig, PoolConnection, QueryOptions } from 'mysql'; export interface MysqlDBOptions extends CommonDBOptions { } export interface MysqlDBSaveOptions<ROW extends ObjectWithId> extends CommonDBSaveOptions<ROW> { } /** * @default false / undefined */ export interface MysqlDBCfg extends PoolConfig { /** * If true - will log all produced SQL queries. */ logSQL?: boolean; /** * If true - will emit logs of connection events. */ debugConnections?: boolean; /** * Default to `console` */ logger?: CommonLogger; } export declare class MysqlDB extends BaseCommonDB implements CommonDB { dbType: any; support: CommonDBSupport; constructor(cfg?: MysqlDBCfg); cfg: MysqlDBCfg & { logger: CommonLogger; }; ping(): Promise<void>; pool(): Pool; close(): Promise<void>; /** * Be careful to always call `con.release()` when you get connection with this method. */ getConnection(): Promise<PoolConnection>; /** * Manually create a single (not pool) connection. * Be careful to manage this connection yourself (open, release, etc). */ createSingleConnection(): Promise<Connection>; getByIds<ROW extends ObjectWithId>(table: string, ids: string[], opt?: MysqlDBOptions): Promise<ROW[]>; runQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>, _opt?: MysqlDBOptions): Promise<RunQueryResult<ROW>>; runSQL<RESULT>(q: QueryOptions): Promise<RESULT>; /** * Allows to run semicolon-separated "SQL file". * E.g "ddl reset script". */ runSQLString(s: string): Promise<void>; runQueryCount<ROW extends ObjectWithId>(q: DBQuery<ROW>, _opt?: CommonDBOptions): Promise<number>; streamQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>, _opt?: MysqlDBOptions): ReadableTyped<ROW>; saveBatch<ROW extends ObjectWithId>(table: string, rowsInput: ROW[], opt?: MysqlDBSaveOptions<ROW>): Promise<void>; /** * Limitation: always returns [], regardless of which rows are actually deleted */ deleteByIds(table: string, ids: string[], _opt?: MysqlDBOptions): Promise<number>; deleteByQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>, _opt?: CommonDBOptions): Promise<number>; /** * Use with caution! */ dropTable(table: string): Promise<void>; /** * dropIfExists=true needed as a safety check */ createTable<ROW extends ObjectWithId>(table: string, schema: JsonSchemaObject<ROW>, opt?: CommonDBCreateOptions): Promise<void>; getTables(): Promise<string[]>; getTableSchema<ROW extends ObjectWithId>(table: string): Promise<JsonSchemaRootObject<ROW>>; patchByQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>, patch: Partial<ROW>): Promise<number>; }