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
Edit your own configurations in conif/config.{env}.js
exports.sequelize = {
dialect: 'mysql', // support: mysql, mariadb, postgres, mssql
database: 'test',
host: 'localhost',
port: 3306,
username: 'root',
password: '',
// delegate: 'myModel', // load all models to `app[delegate]` and `ctx[delegate]`, default to `model`
// baseDir: 'my_model', // load all files in `app/${baseDir}` as models, default to `model`
// exclude: 'index.js', // ignore `app/${baseDir}/index.js` when load models, support glob and array
// more sequelize options
};
You can also use the connection uri to configure the connection:
exports.sequelize = {
dialect: 'mysql', // support: mysql, mariadb, postgres, mssql
connectionUri: 'mysql://root:@127.0.0.1:3306/test',
// delegate: 'myModel', // load all models to `app[delegate]` and `ctx[delegate]`, default to `model`
// baseDir: 'my_model', // load all files in `app/${baseDir}` as models, default to `model`
// exclude: 'index.js', // ignore `app/${baseDir}/index.js` when load models, support glob and array
// more sequelize options
};
egg-sequelize has a default sequelize options below
If you define the same model for different datasource, the same model file will be excute twice for different database, so we can use the secound argument to get the sequelize instance:
// app/model/user.js
// if this file will load multiple times for different datasource
// we can use the secound argument to get the sequelize instance
module.exports = (app, model) => {
const { STRING, INTEGER, DATE } = app.Sequelize;
const User = model.define('user', {
login: STRING,
name: STRING(30),
password: STRING(32),
age: INTEGER,
last_sign_in_at: DATE,
created_at: DATE,
updated_at: DATE,
});
return User;
};
Customize Sequelize
By default, egg-sequelize will use sequelize@5, you can cusomize sequelize version by pass sequelize instance with config.sequelize.Sequelize like this:
egg-sequelize
Sequelize plugin for Egg.js.
Install
Usage & configuration
config/plugin.jsconif/config.{env}.jsYou can also use the
connection urito configure the connection:egg-sequelize has a default sequelize options below
More documents please refer to Sequelize.js
Model files
Please put models under
app/modeldir by default.Conventions
user.jsapp.model.Userperson.jsapp.model.Personuser_group.jsapp.model.UserGroupuser/profile.jsapp.model.User.Profilecreated_at datetime,updated_at datetime.user_id,comments_count.Examples
Standard
Define a model first.
Now you can use it in your controller:
Associate
Define all your associations in
Model.associate()and egg-sequelize will execute it after all models loaded. See example below.Multiple Datasources
egg-sequelize support load multiple datasources independently. You can use
config.sequelize.datasourcesto configure and load multiple datasources.Then we can define model like this:
If you define the same model for different datasource, the same model file will be excute twice for different database, so we can use the secound argument to get the sequelize instance:
Customize Sequelize
By default, egg-sequelize will use sequelize@5, you can cusomize sequelize version by pass sequelize instance with
config.sequelize.Sequelizelike this:Full example
Sync model to db
We strongly recommend you to use Sequelize - Migrations to create or migrate database.
This code should only be used in development.
Migration
Using sequelize-cli to help manage your database, data structures and seed data. Please read Sequelize - Migrations to learn more infomations.
Recommended example
Questions & Suggestions
Please open an issue here.
License
MIT