Back to Node.js |
See Also MySQL、JavaScript Testing Overview |
NodeJS ORM Solutions
ORMs help to abstract data mappings between your code and the database, easing data querying and manipulation. It can also help to easily change the underlying database engine without (mostly) changing any code.
1. Comparison
1.1. Trends
Download statistics for packages sequelize, knex, bookshelf, waterline
https://www.codediesel.com/javascript/nodejs-mysql-orms/ Sequelize vs. orm vs. Bookshelf
sequelize vs knex vs waterline vs bookshelf
Sequelize 依然是使用占比最高的 ORM 解决方案,但 knex 追赶的速度也是十分惊人的。
2. Sequelize
https://github.com/demopark/sequelize-docs-Zh-CN An easy-to-use multi SQL dialect ORM for Node.js
Starting from 4.0.0 Sequelize will only support Node v4 and above to use ES6 features.
2.1. Getting Started
# 引入到当前工程
$ npm install --save sequelize
$ npm install --save mysql2
2.2. Sample Code
https://github.com/twotwo/practice-nodejs/tree/master/hello_sequelize test/sequelize_user.test.js
仿照conf/orm.js.sample 写数据库配置文件 conf/orm.js
运行测试用例: DEBUG=sequelize:dao:* jest test/sequelize_user.test.js
3. Knex.js
3.1. Getting Started