lazy-kit-fastsql
v0.0.2
Published
> SQL quick toolkit based on Nodejs
Downloads
5
Readme
lazy-kit-fastsql
SQL quick toolkit based on Nodejs
Functions
type Row = {[field:string]:any};
type RowSet = Row[];
interface SelectOptions {
groupBy?: string|string[];
orderBy?:string|{[field:string]:("ASC"|"DESC")};
limit?:number|number[];
}
type Table2Union = { table: string; fields: Row; where?:Row|RowSet; };
/**
* 插入语句
* @param T 插入表
* @param set 插入字段 { 字段 : 值 }
* @param isReplace 主键重复时是否替换, 默认false
* @returns
*/
function C(T:string, set:Row, isReplace?:boolean): string;
/**
* 查询语句
* @param T 查询表
* @param fields 查询字段 { 原字段: 别名 }, 默认 *
* @param where 查询条件, 可以是JSON对象、也可以是数组
* - JSON时, { 字段 : 值, ... } 所有字段通过AND连接
* - 数组时, [{ 字段 : 值, ... },...] JSON对象内字段通过AND连接, 每个JSON对象产生的条件通过OR连接
* - 特殊用法:
* - 当字段设置为$CUSTOM时, 自定义条件语句
* - 值为空、NULL或者undefined的时候, 转换为SQL中的NULL值判断
* - 同样可以通过设置值为字符串形式的特殊表达式 “#{NULL}”、“#{ISNULL}”来判断值为空; 或者通过 “#{ISNOTNULL}”、“#{NOTNULL}”、“#{!NULL}”来判断非空;
* - 值如果是一个字符串或者数字, 默认做相等判断
* - 值是一个JSON对象, 值的KEY是一个操作符号, 支持IN、BETWEEN、LIKE、区间(>=_<、>_<=、>=_<=), 操作符都不是以上的情况下, 直接 “字段名 操作符 值”进行判断
* @param options { GROUP BY, ORDER BY, LIMIT }
* @returns
*/
function R(T:string, fields?:Row|"*", where?:Row|RowSet, options?:SelectOptions): string;
/**
* 更新语句
* @param T 更新表
* @param set 更新字段 { 字段名: 值 }
* @param where
* @returns
*/
function U(T:string, set:Row, where?:Row|RowSet): string;
/**
* 删除语句
* @param T 删除表
* @param where 条件
* @returns
*/
function D(T:string, where?:Row|RowSet): string;
namespace T {
/**
* 联表查询 - UNION(ALL)
* @param tables 要联合的表 [{
table: 表名
fields: {原字段名:别名, ...}, // 提取字段, 注意字段数量、别名要和其他查询保持一致
where: [ // 查询条件, 多个条件OR连接时数组, 全部AND连接时JSON
{ ... }, // 条件1: AND连接
// 此处将条件1 和 条件2 通过 OR 进行连接
{ ... } // 条件2
]
},...]
*/
function Union(...tables: Table2Union[]): string;
/**
* 联表查询 - UNION(ALL)
* @param TT 指定临时表名
* @param tables 要联合的表
*/
function Union(TT:string, ...tables: Table2Union[]): string;
/**
* 联表查询 - UNION(ALL)
* @param isDuplicate 是否允许重复, 默认true, 即UNION ALL
* @param tables 要联合的表
*/
function Union(isDuplicate: boolean, ...tables: Table2Union[]): string;
/**
*
* @param TT 指定临时表名
* @param isDuplicate 是否允许重复, 默认true, 即UNION ALL
* @param tables
*/
function Union(TT:string, isDuplicate: boolean, ...tables: Table2Union[]): string;
/**
* 联表查询 - (INNER|LEFT|RIGHT|FULL)JOIN
* @param M 主表
* @param J 联表
* @param ON 条件
* @param fields 提取字段, 一维数组或者二维数组只有一个值, 取主表; 二维数组存在两个值, 第一个数组取主表, 第二个取联表
* @param OP 操作符, 默认LEFT
* @returns SQL
*/
function Join(M:string, J:string, ON:Row, fields:string[]|string[][], OP?:string): string;
}