Sequelize plugin in egg
Egg's sequelize plugin.
$ npm i --save egg-sequelize
# And one of the following:
$ npm install --save pg pg-hstore
$ npm install --save mysql # For both mysql and mariadb dialects
$ npm install --save tedious # MSSQL
config.default.js
exports.sequelize = {
port: '3306',
host: 'localhost',
username: 'root',
password: '',
database: 'test',
dialect: 'mysql', // support: mysql, mariadb, postgres, mssql
};
More documents please refer to Sequelize.js
Please set sequelize models under app/model
app/model/test.js
'use strict'
module.exports = function (sequelize) {
return sequelize.define('test', {
name: {
type: sequelize.Sequelize.STRING(30),
}
})
}
app/controller/user.js
'use strict'
module.exports = function* () {
this.body = yield this.model.test.find({
'name':'aaa'
})
}
app/model/user.js
'use strict'
module.exports = function (sequelize) {
return sequelize.define('user', {
name: {
type: sequelize.Sequelize.STRING(30),
}
})
}
app/model/post.js
'use strict'
module.exports = function (sequelize) {
return sequelize.define('post', {
name: {
type: sequelize.Sequelize.STRING(30),
},
userId: {
type: sequelize.Sequelize.INTEGER,
field: 'user_id'
}
}, {
classMethods: {
associate(models) {
models.post.belongsTo(models.user);
}
}
})
}
app/controller/post.js
'use strict'
module.exports = function* () {
this.body = yield this.model.post.find({
'name':'aaa'
});
}