由于经常使用 thinkjs 开发 web 项目,觉得 think-model-mysql 非常好用,所以想尝试在其它 Node.js 项目中使用。

安装相关依赖

npm install think-model --save
npm install think-model-mysql --save

实例化模型

const Model = require('think-model/lib/model');
const mysql = require('think-model-mysql');
// 配置信息
const config = {handle: mysql, host: '127.0.0.1', port: 3306, database: 'test', user: 'root', password: 'root' };
const db = new Model('', config);

// 查询用户表
const users = await db.model('user').where({is_delete: 0}).select();

模型使用文档:https://thinkjs.org/zh-cn/doc/3.0/relation_model.html
参考文档:https://github.com/thinkjs/thinkjs/issues/1398

标签: Node.js, MySQL