From aade70bce78afb39e8e9b3201261bbb8bcf26847 Mon Sep 17 00:00:00 2001 From: AntiMoron Date: Fri, 11 Feb 2022 15:52:34 +0800 Subject: [PATCH] feat: add complete typescript typings (#17) Co-authored-by: Boyuan Gao --- index.d.ts | 100 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 index.d.ts diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..89fa7ac --- /dev/null +++ b/index.d.ts @@ -0,0 +1,100 @@ +interface EggMySQLClientOption { + /// host e.g. 'mysql.com' + host: string; + /// port e.g. '3306' + port: string; + /// username e.g. 'test_user' + user: string; + // password e.g. 'test_password' + password: string; + // database e.g. 'test' + database: string; +} + +type Singleton = {}; + +interface EggMySQLClientsOption { + [clientName: string]: EggMySQLClientOption; +} + +interface EggMySqlConfig { + default?: object; + app?: boolean; + agent?: boolean; + client?: EggMySQLClientOption; + clients?: EggMySQLClientsOption; +} + +type EggMySQLInsertResult = object[]; +type EggMySQLSelectResult = object[]; +interface EggMySQLCondition { + where?: object; + orders?: [string, "desc" | "asc" | "DESC" | "ASC"][]; + limit?: number; + offset?: number; + [otherCondition: string]: any; +} +interface EggMySQLUpdateResult { + affectedRows: number; + fieldCount: number; + insertId: number; + serverStatus: number; + warningCount: number; + message: string; + protocol41: boolean; + changedRows: number; +} + +interface EggMySQLLiterals { + now: number; + Literal: Function; +} + +interface EggMySQL { + literals: EggMySQLLiterals; + /// Returns mysql client instance. + get: (dbName: string) => EggMySQL; + /// execute sql e.g. + /// query('update posts set hits = (hits + ?) where id = ?', [1, postId]) + query: ( + sql: string, + values?: any[] + ) => Promise< + EggMySQLSelectResult | EggMySQLUpdateResult | EggMySQLInsertResult + >; + /// create object into table + create: (table: string, values: object) => Promise; + /// update object of table + update: (table: string, values: object) => Promise; + /// delete objects from table + delete: (table: string, values: object) => Promise; + /// insert object into table + insert: (table: string, values: object) => Promise; + /// select objects from table + select: ( + table: string, + condition?: EggMySQLCondition + ) => Promise; + /// begin a transaction + beginTransaction: () => Promise; + /// begin a scoped transaction + beginTransactionScope: ( + codeBlock: (conn: EggMySQLTransation) => Promise, + ctx: object + ) => Promise; +} + +interface EggMySQLTransation extends Omit { + commit: () => Promise; + rollback: () => Promise; +} + +declare module "egg" { + interface Application { + mysql: EggMySQL & Singleton; + } + + interface EggAppConfig { + mysql: EggMySqlConfig; + } +}