egg-sequelize
Sequelize plugin for Egg.js.
NOTE: This plugin just for integrate Sequelize into Egg.js, more documentation please visit http://sequelizejs.com.
Install
$ npm i --save egg-sequelize$ npm install --save mysql2 # For both mysql and mariadb dialects # Or use other database backend. $ npm install --save pg pg-hstore # PostgreSQL $ npm install --save tedious # MSSQL
Usage & configuration
config.default.js
exportssequelize = dialect: 'mysql' // support: mysql, mariadb, postgres, mssql database: 'test' host: 'localhost' port: '3306' username: 'root' password: '';
config/plugin.js
exportssequelize =enable: truepackage: 'egg-sequelize'
package.json
More documents please refer to Sequelize.js
Model files
Please put models under app/model
dir.
Conventions
model file | class name |
---|---|
user.js |
app.model.User |
person.js |
app.model.Person |
user_group.js |
app.model.UserGroup |
- Tables always has timestamp fields:
created_at datetime
,updated_at datetime
. - Use underscore style column name, for example:
user_id
,comments_count
.
Examples
Standard
Define a model first.
NOTE:
app.model
is an Instance of Sequelize, so you can use methods like:app.model.sync, app.model.query ...
// app/model/user.js module { const STRING INTEGER DATE = appSequelize; const User = appmodel; User { return this; } Userprototype { this; } return User;};
Now you can use it in your controller:
// app/controller/user.jsmodule { return Controller * { const users = thisctxmodelUser; thisctxbody = users; } * { const user = thisctxmodelUser; user; thisctxbody = user; } }
Full example
// app/model/post.js module { const STRING INTEGER DATE = appSequelize; const Post = appmodel; Post { appmodelPost; } return Post;};
// app/controller/post.jsmodule { return Controller * { const posts = thisctxmodelPost; thisctxbody = posts; } * { const post = thisctxmodelPost; const user = post; post; thisctxbody = post; } * { const post = thisctxmodelPost; post; thisctxbody = success: true ; } }
Sync model to db
We strongly recommend you to use migrations to create or migrate database.
This code should only be used in development.
// {app_root}/app.js module { if appconfigenv === 'local' app; };
Migrations
If you have added scripts of egg-sequelize into your package.json
, now you can:
Command | Description |
---|---|
npm run migrate:new | Generate a new Migration file to ./migrations/ |
npm run migrate:up | Run Migration |
npm run migrate:down | Rollback once Migration |
For example:
$ npm run migrate:up
For unittest
environment:
$ EGG_SERVER_ENV=unittest npm run migrate:up
or for prod
environment:
$ EGG_SERVER_ENV=prod npm run migrate:up
or for others environment:
$ EGG_SERVER_ENV=pre npm run migrate:up
This will load database config from config/config.pre.js
.
Write migrations with Generator friendly, you should use co.wrap
method:
'use strict';const co = ; moduleexports = up: co down: co;
And you may need to read Sequelize - Migrations to learn about how to write Migrations.
Recommended example
Questions & Suggestions
Please open an issue here.