假设有一张表 t_user:
| id | name | age | addr | createAt | updateAt |
|---|---|---|---|---|---|
int |
varchar |
varchar |
varchar |
datetime |
datetime |
const { Orm } = require('mysql-orm')()
const User = Orm.define('t_user', ['id', 'name', 'age', 'addr'])await User.insert({
name: 'INSERT test',
age: 11,
addr: 'hell'
});User.find({ // SELECT all columns defined in `define()`
where: { id: 2 }
});
User.select([{id: 'uid'}, 'name'], { // SELECT id as uid, name
where: { id: 2 }
});User.update({ name: 'new name' }, { id: 2 })User.delete({ id: 2 })const { transaction } = require('mysql-orm')();
const t = await transaction();
const [{ insertId }] = await User.insert(
{ name: "TRANSACTION test", age: 23}, t
);
await t.commit(); // 提交
// await t.rollback(); // 回滚
t.release(); // 回收资源const { Orm } = require('mysql-orm')()
const model = Orm.define('your_table', ['table', 'column'])Orm.define(tableName: String, ?columns: String[])
定义表结构:表名和表字段列表(字段名可以不是表的全量字段),支持别名。
Orm.define('t_user', [
{ id: 'uid' }, // 别名
{ column: 'createAt', alias: 'ctime' }, // 别名
'name', 'age'
}])
// model.find() 查询结果
// [{ uid: 1, ctime: '2019-03-27 09:00:00', name: 'jonge', age: 44 }, ...]model.insert(data, ?transaction)
- data: 要插入数据的 key-value 对象
- transaction: 执行事务时会用到
model.insert({
name: 'jonge',
age: 99
})model.select(columns, condition, ?transaction)
- columns: 要查询的表字段,格式参考
Orm.define接口 - condition: { where, limit, orderBy, groupBy }
- transaction: 执行事务时会用到
model.select(
['name','age'],
{
where: [{ key: 'name', op: 'LIKE', value: '%Jon' }],
orderBy: 'age', // 或 ['age'] 或 [{ by: ['age'], type: 'ASC' }]
limit: 10, // 或 [0, 10]
// groupBy: 'age'
})model.find(condition, ?transaction) 等于 model.select(columns, condition, ?transaction),其中 columns 是 Orm.define() 中定义的字段。
model.delete(condition, ?transaction)
- condition: 参考builder 的
.where()。 - transaction: 执行事务时会用到
model.select({
id: 2
})