lazy-kit-mysql
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

lazy-kit-memory

mysql quick toolkit based on Nodejs

Functions

// 获取连接的方式, easy是普通的connection, pool是连接池
enum Mode{
  EASY = "easy", POOL = "pool"
}
// 数据类型
type Row = {
  [key: string]: any;
}
// 数据集类型
type RowSet = Row[]
// 查询参数
interface QueryOptions {
  sql: string;
  params?: Array<any>;
}
// 插入、更新、删除时的返回结果
interface Result {
  /**
   * The number of affected rows from an insert, update, or delete statement.
   */
  affectedRows: number;
  /**
   * The insert id after inserting a row into a table with an auto increment primary key.
   */
  insertId: number;
  /**
   * The number of changed rows from an update statement. "changedRows" differs from "affectedRows" in that it does not count updated rows whose values were not changed.
   */
  changedRows: number;
}


interface ConnectionConfig {
  host?: string | undefined;
  port?: number | undefined;
  user?: string | undefined;
  password?: string | undefined;
  database?: string | undefined;
  charset?: string | undefined;
  timeout?: number | undefined;
  connectTimeout?: number | undefined;    
}
interface PoolConfig extends ConnectionConfig {
  acquireTimeout?: number | undefined;
  waitForConnections?: boolean | undefined;
  connectionLimit?: number | undefined;
  queueLimit?: number | undefined;
}

/**
 * Mysql instance
 * @param options connection configuration
 * @returns Mysql
 */
function mysql(options?:ConnectionConfig, mode?:Mode): Mysql;

/**
 * Mysql
 */
class Mysql {
  /**
   * Mysql Instance
   * if mode is POOL, init pool as well
   * @param options ConnectionConfig | PoolConfig
   * @param mode ConnMode { EASY, POOL }
   */
  constructor(options?:ConnectionConfig | PoolConfig, mode?:Mode);

  /**
   * create a connection @ mysql instance
   * defaultly, the instance automaticly open the connection
   * @returns Mysql
   */
  connect(): Promise<this>;

  /**
   * manually close connection
   * if destroy=true, then destroy the pool connection
   * also, if fail to release & not-already-released, destroy it
   * @param destroy boolean:false
   * @returns this
   */
  close(destroy?:boolean): Promise<this>;

  /**
   * manually open connection if disconnected
   * @returns this
   */
  open(): Promise<this>;

  /**
   * whether connected
   * @returns boolean
   */
  isConnected(): boolean;

  /**
   * 执行SQL语句, CRUD
   * @param options {sql,params}
   * @param autoConn whether open connection and close after execution
   * @returns Row[]|Result
   */
  query(options: QueryOptions, autoConn?:boolean): Promise<Row[]|Result>;

  /**
   * 批处理
   * @param options [{ sql, params },...]
   * @returns Array<Row[]|Result>
   */
  batch(options: Array<QueryOptions>): Promise<Array<Row[]|Result>>;

  /**
   * 事务处理
   * manually connect at first, and manually close connection after execution
   * @param options [{ sql, params },...]
   * @returns 
   */
  transact(options: Array<QueryOptions>): Promise<Array<Row[]|Result>>;

}

Readme

Keywords

none

Package Sidebar

Install

npm i lazy-kit-mysql

Weekly Downloads

0

Version

1.0.0

License

none

Unpacked Size

18.2 kB

Total Files

5

Last publish

Collaborators

  • geo.pan