UNPKG

lia-mysql

Version:

JavaScript library of data standards.

58 lines (57 loc) 2.62 kB
import { DeleteResult, InsertOption, InsertResult, SelectOption, UpdateOption, UpdateResult, UpdateRow } from "../types"; import { Operator } from "./operator"; import { Transaction } from "./transaction"; import { Pool } from "mysql2"; import { QueryResult } from 'mysql2/typings/mysql/lib/protocol/packets'; export declare class Client extends Operator { private pool; constructor(pool: Pool); getTransaction(): Promise<Transaction>; query<T extends QueryResult>(sql: string, params?: any[] | object): Promise<T>; queryOne<T extends QueryResult>(sql: string, params?: any[] | object): Promise<T | undefined>; count(table: string, where?: object): Promise<number>; /** * Select rows from a table * * @param {String} table table name * @param {Object} [option] optional params * - {Object} where query condition object * - {Array|String} columns select columns, default is `'*'` * - {Array|String} orders result rows sort condition * - {Number} limit result limit count, default is no limit * - {Number} offset result offset, default is `0` * @return {Array} result rows */ select<T = any>(table: string, option?: SelectOption): Promise<T[]>; get<T = any>(table: string, where?: object, option?: SelectOption): Promise<T>; insert(table: string, rows: object | object[], option?: InsertOption): Promise<InsertResult>; update(table: string, row: object, option?: UpdateOption): Promise<UpdateResult>; /** * Update multiple rows from a table * * UPDATE `table_name` SET * `column1` CASE * WHEN condition1 THEN 'value11' * WHEN condition2 THEN 'value12' * WHEN condition3 THEN 'value13' * ELSE `column1` END, * `column2` CASE * WHEN condition1 THEN 'value21' * WHEN condition2 THEN 'value22' * WHEN condition3 THEN 'value23' * ELSE `column2` END * WHERE condition * * See MySQL Case Syntax: https://dev.mysql.com/doc/refman/5.7/en/case.html * * @param {String} table table name * @param {Array<Object>} updateRows Object Arrays * each Object needs a primary key `id`, or each Object has `row` and `where` properties * e.g. * [{ id: 1, name: 'fengmk21' }] * or [{ row: { name: 'fengmk21' }, where: { id: 1 } }] * @return {object} update result */ updateRows(table: string, updateRows: UpdateRow[]): Promise<UpdateResult>; delete(table: string, where?: object | null): Promise<DeleteResult>; }