1.1.9 • Published 3 months ago

@gas0324/mysql v1.1.9

Weekly downloads
-
License
MIT
Repository
github
Last release
3 months ago

mysql

适用于Node.js的MySQL生成器,项目基于mysql2,支持链式调用,请注意:本项目自动处理下划线与小驼峰命名方式的转换

目录

安装

npm install --save @gas0324/mysql mysql2

使用

import { init, mysql, Mysql } from '@gas0324/mysql';
init({
  prefix: 'as_',  // 表前缀
  host: 'localhost',
  user: 'root',
  database: 'test',
  waitForConnections: true,
  connectionLimit: 10,
  queueLimit: 0
  // 其他创建mysql2连接池的参数
});
// 方式一
mysql.table('{pre}menu').filed('id, name, parent_id').select();
// sql: select id, name, parent_id from as_menu

// 方式二
new Mysql('{pre}menu').filed('id, name, parent_id').select();
// sql: select id, name, parent_id from as_menu

// 以上查询返回结果 
[
  {
    id: 1,
    title: '菜单标题',
    parentId: null
  },{
    id: 2,
    title: '子菜单',
    parentId: 1
  }
  // ...
]

// PS: 两种方式结果完全相同, 注意field中的'parent_id' 与查询结果中的'parentId'会自动转换 

方法

使用下面的方法后,可直接链式调用本项目的其它方法

方法名说明参数
table表名(table: string)
field查询字段(field?: string)
join联合查询(table: string, way?: 'inner/left/right)
data插入/修改数据(data: Object)
where条件(where: WhereOptions)
order排序(order: string)
limit行数(limit: number)
pager分页(pageIndex: number, pageSize?: number): 页码、每页条数

使用下面的方法后,不可以继续使用链式方式调用本项目的其它方法

方法名说明参数
init初始化数据库链接参考: 使用
find数据库查询-单条-
select数据库查询-列表-
count数据库查询-行数-
insert数据库执行-插入-
update数据库执行-更新-
delete数据库执行-删除-
save数据库执行-插入或者更新-
makeSql生成sql语句(type?: string): type可为:'select/insert/update/delete/save/count'
getPool获取连接池-
getConnection获取链接-

where参数

// string
mysql.table('menu').where('parent_id = 1 or parent_id = 2').select();
// sql: select * from menu where parent_id = 1 or parent_id = 2;

// object: 基础模式
mysql.table('menu').where({parentId: 1, status: true}).select();
// sql: select * from menu where parent_id = 1 and status = true;

// object: 正常模式
mysql.table('menu').where({name: 'parent_id', value: [1,2], operator: 'in', _mode: 'full'}).select();
// sql: select * from menu where parent_id in (1,2);
 
// 数组:多个条件and拼接
mysql.table('menu').where([
  {name: 'parent_id', value: [1,2], operator: 'in', _mode: 'full'},
  {name: 'name', value: '标题', operator: 'like', _mode: 'full'}
  ]).select();
// sql: select * from menu where parent_id in (1,2) and name like '%标题%';

// 混合
mysql.table('menu').where([
  '(parent_id = 1 or parent_id = 2)',
  {status: true},
  {name: 'name', value: '标题', operator: 'like', _mode: 'full'}
  ]).select();
// sql: select * from menu where (parent_id = 1 or status = true) and status = ture and name like '%标题%';

日志

npm install --save log4js
import * as log4js from "log4js";

log4js.configure({
  appenders: {
    mysql: { type: "file", filename: "log/mysql.log" },
    other: { type: "file", filename: "log/other.log" }
  },
  categories: {
    mysql: { appenders: ["mysql"], level: "trace" },
    default: { appenders: ['other'], level: "trace" }
  },
});

其他

如果你需要更复杂的sql语句,使用本项目无法生成,也可以直接使用mysql2 注意:使用以下方式执行sql语句,无法通过本项目记录日志,请自行记录

// 使用连接池
import { mysql } from '@gas0324/mysql';
asd
const pool = mysql.getPool();
pool.query("SELECT field FROM atable", function(err, rows, fields) {
   // ...
});

// 或者使用链接
const conn = await mysql.getConnection();
conn.query(
  'SELECT * FROM `table` WHERE `name` = "Page" AND `age` > 45',
  function(err, results, fields) {
    console.log(results); // 结果集
    console.log(fields); // 额外的元数据(如果有的话)
  }
);

反馈

您拥有任何建议或想法,可以通过issues进行反馈

赞助

参考资料

1.1.9

3 months ago

1.1.8

3 months ago

1.1.7

3 months ago

1.1.6

3 months ago

1.1.5

4 months ago

1.1.1

1 year ago

1.1.4

1 year ago

1.1.3

1 year ago

1.1.2

1 year ago

1.1.0

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago