Wondering what’s next for npm?Check out our public roadmap! »

egg-sequelize

1.0.0 • Public • Published

egg-sequelize

Sequelize plugin in egg

NPM version build status Test coverage David deps Known Vulnerabilities npm download

Egg's sequelize plugin.

Install

$ 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 

Usage & configuration

  • 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

models

Please set sequelize models under app/model

Examples

Standard

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'
  })
}

Associate

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'
  });
}

Install

npm i [email protected]

Version

1.0.0

License

MIT

Last publish

Collaborators

  • avatar
  • avatar
  • avatar
  • avatar
  • avatar
  • avatar
  • avatar