对 mysql 进行简单的封装,再也不用担心写 sql 语句啦,当然你也可以把它作为一个 sql 语句生成器来使用
具体使用方法,直接贴代码:
import {
sqlInit,
createSqlForm,
sqlEnd,
checkSqlKey,
massql,
getsqlFormData,
} from "../src/index";
import type { createSqlFormType } from "../src/index";
const sqlConfig = {
host: "127.0.0.1",
user: "root",
// password: "123456",
// database: "test666",
database: "test",
password: "123456",
};
const createsqlForm: createSqlFormType = {
student: [
{
key: "name",
type: "str",
des: "姓名",
},
{
key: "age",
type: "int",
des: "年龄",
},
],
};
// 创建表
function createForm() {
createSqlForm(sqlConfig, createsqlForm);
}
async function main() {
await sqlInit(sqlConfig);
await checkSqlKey();
getsqlFormData(__dirname + "/sqlform.json");
const sql = new massql();
// 设置调试模式
sql.debugMode = 2;
// 查询
// await sql
// .use(sqlform.student)
// .select()
// .whereOR("id", 3)
// .inOR("id", [1, 2])
// .key(["id", "name"])
// .getDelete()
// .go();
// 自定义语句
// await sql
// .use(sqlform.student)
// .define(
// "SELECT `id`,`name` from `student` WHER is_delete = 0 AND ((`id`= 1))"
// )
// .go();
// await sql.use(sqlform.student).update({ name: "a" }, [1]).go();
// await sql.use(sqlform.student).delete([1, 2, 3, 4]).go();
// await sql.use(sqlform.student).insert({ name: "xxx", age: "11" }).go();
// await sql
// .use(sqlform.student)
// .select()
// .sort([
// // { key: "id", mode: "DESC" },
// { key: "age", mode: "DESC" },
// ])
// .key(["id", "name", "age"])
// .go();
// await sql
// .use(sqlform.student)
// .distinct("age")
// .in("id", [1, 2, 3, 4])
// .like("des", "一")
// .go();
// await sql.join( TODO Highlight
// sql.use(sqlform.student).select().asJoin(),
// sql.use(sqlform.student).select().asJoin(),
// ["", ""]
// );
// await sql
// .use(sqlform.student)
// .key(["id", "name", "age"])
// .select()
// .gte("age", 99)
// .go();
// await sql
// .use(sqlform.student)
// .count("count")
// .in("id", [1, 2])
// .like("des", "一")
// .go();
// await sql.use(sqlform.student).update({ name: "first", id: 99 }, [1]).go();
// const updateRecords = [
// { id: 1, name: "ss6", age: 10 },
// { name: "tt2", age: 18 },
// { name: "666", age: 18 },
// ];
// await sql.use(sqlform.student).set(updateRecords).go();
// await sql.use(sqlform.student).set({ id: 1, age: 0 }).go();
// 断开连接
sqlEnd();
}
main();
// createForm();
对 Massql 类的说明
interface Massql {
/** 调试模式,0为关闭,1的时候打印sql语句,2的时候打印结果 */
debugMode: number;
/** mysql对象 */
mysql: any;
/** 条件转义 */
escape(str: string | number): string | number;
/** 表名转义 */
escapeId(str: string): string;
/** 版本查看 */
version(): this;
/** 字段 */
key(key: string[]): this;
/** 使用表 */
use(form: string | string[]): this;
/** where条件 */
where(key: {} | string, value?: string | number): this;
/** 大于等于条件 */
gte(key: {} | string, value?: string | number): this;
/** 大于条件 */
gt(key: {} | string, value?: string | number): this;
/** 小于条件 */
lt(key: {} | string, value?: string | number): this;
/** 小于等于条件 */
lte(key: {} | string, value?: string | number): this;
/** where条件,用or连接 */
whereOR(key: {} | string, value?: string | number): this;
/** like条件 */
like(key: {} | string, value?: string | number): this;
/** like条件 */
likeOR(key: {} | string, value?: string | number): this;
/** in条件 */
in(key: string, value: string[] | number[]): this;
/** in条件 */
inOR(key: string, value?: string[] | number[]): this;
/** 只查一个 */
one(): this;
/** 分页,在select时传入pageNum和pageSize */
paging(): this;
/** 获得时间 */
getTime(): this;
/** 排序方法,mode中 DESC:降序,最新(大)的放前面 | ASC:升序,最早(小)的前面 */
sort(
obj: string | { key: string; mode: "DESC" | "ASC" }[],
mode?: "DESC" | "ASC"
): this;
/** 统计数量 */
count(name: string): this;
/** 去重 */
distinct(key: string): this;
/** 连表查询,暂未实现 */
join(data: any, data2: any, d: any): this;
/** 连表查询,暂未实现 */
asJoin(): this;
/**
* @param data 为string和number时,通过id查询,为数组时通过id批量查询,为json时,通过and关联查询
* @param likeSearch dataObj为json时是否进行模糊查询,默认开启
* @param filter dataObj为json时是否对其进行过滤,默认开启
*/
select(
dataObj?: [] | {} | string | number,
likeSearch?: 1 | 0,
filter?: 0 | 1
): this;
/** 获得删除的数据 */
getDelete(): this;
/** 自定义语句 */
define(sqlStr: any): this;
/** 更新 */
update(obj: object, id: string | number | (string | number[])): this;
/** 批量更新 */
batchUpdate(obj: {}[]): this;
/** 更新或插入,通过有无id判断 */
set(obj: {} | {}[]): this;
/** 插入 */
insert(obj: {} | {}[]): this;
/** 删除 */
delete(id: string | number | (string | number)[]): this;
/**
* 执行
* @param mode 0执行,-2返回sql语句,-1打印sql语句
*/
go(mode?: 0 | -1 | -2): Promise<any>;
/**
* 直接执行语句
* @param sqlStr 执行的语句
*/
runSql(sqlStr: any): any;
}
export type createSqlForm = {
/** 字段名 */
key: string;
/** 字段类型 */
type: "str" | "int" | "float";
/** 字段长度 */
length?: number;
/** 是否不能为空 */
notNull?: boolean;
/** 字段描述 */
des?: string;
}[];
export type sqlForm = {
[key: string]: string[];
};
export type sqlState = {
/** mysql配置 */
config?: {
host: string;
user: string;
password: string;
database: string;
};
/** 是否需要重启 */
status: 0 | 1;
/** mysql connection对象 */
connection?: any;
/** 数据库表 */
sqlForm?: sqlForm;
/** 数据库表 */
listKey?: any;
};