UNPKG

yoyosqlite

Version:

一个链式连接sqlite数据库的扩展包

83 lines (82 loc) 3.19 kB
import sqlite3 from 'sqlite3'; type Order = 'ASC' | 'DESC'; type whereArr = Array<[string, operator, any]>; type resultSet = { data: any[]; count: number; maxCount: number; fields: any[]; }; type resultSetPages = { data: any[]; count: number; maxCount: number; fields: any[]; maxPage?: number; }; type operator = '=' | '<>' | '<' | '<=' | '>' | '>=' | 'LIKE' | 'IN' | 'NOT IN' | 'IS NULL' | 'IS NOT NULL' | 'BETWEEN' | 'NOT BETWEEN' | 'REGEXP' | 'NOT REGEXP' | 'NOT LIKE'; type TransactionConn = { table: (table: string) => YoyoMysqlClass; execute: (sql: string, values?: any) => Promise<[any, any[]]>; query: (sql: string, values?: any) => Promise<[any, any[]]>; }; export default class YoyoMysql { private static _DB; static createDatabase(dbPath: string): typeof YoyoMysql; static table(table: string): YoyoMysqlClass; static beginTransaction(callback: (conn: TransactionConn) => Promise<any>): Promise<void>; static query(sql: string, values?: any): Promise<[any, any[]]>; static execute(sql: string, values?: any): Promise<[any, any[]]>; } declare class YoyoMysqlClass { private _link; private _table?; private _fields; private _clogic; private _where; private _group; private _having; private _order; private _join; private _limit; constructor(link: sqlite3.Database); table(table: string): this; field(field: string): this; field(fields: Array<string>): this; order(field: string, order?: Order): this; order(fields: Record<string, Order> | string): this; group(fieldArr: Array<string>): this; group(field: string): this; having(where: string): this; limit(limit: number, offset?: number): this; leftJoin(table: string, where: string): this; rightJoin(table: string, where: string): this; join(table: string, where: string): this; fullJoin(table: string, where: string): this; private _whereComposite; whereIn(field: string, callback: () => this): this; where(field: string, operator: operator, value: any): this; where(field: string, value: (string | number) | Record<string, (string | number)>): this; where(field: string, callback: () => this): this; where(whereArr: whereArr): this; whereOr(field: string, operator: operator, value: any): this; whereOr(field: string, value: (string | number) | Record<string, (string | number)>): this; whereOr(field: string, callback: () => this): this; whereOr(whereArr: whereArr): this; find(): Promise<Record<string, any> | null>; select(): Promise<resultSet>; pages(page?: number, count?: number): Promise<resultSetPages>; delete(): Promise<any>; insert(data: any[][] | Record<string, any> | Record<string, any>[] | any[]): Promise<any>; update(data: Record<string, (number | string)> | [string, string][] | [string, string]): Promise<any>; private buildWhere; private buildSelect; private buildDelete; private buildInsert; private buildUpdate; buildSql(type?: 'select' | 'insert' | 'update' | 'delete', data?: any): { sql: string; params: any[]; }; } export {};