eggjs/egg-sequelize

Name: egg-sequelize

Owner: egg

Description: Sequelize for Egg.js

Created: 2017-01-20 09:19:26.0

Updated: 2018-05-21 04:16:29.0

Pushed: 2018-05-24 09:59:20.0

Homepage:

Size: 96

Language: JavaScript

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

egg-sequelize

Sequelize plugin for Egg.js.

NOTE: This plugin just for integrate Sequelize into Egg.js, more documentation please visit http://sequelizejs.com.

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

Install
m i --save egg-sequelize
m install --save mysql2 # For both mysql and mariadb dialects

 use other database backend.
m install --save pg pg-hstore # PostgreSQL
m install --save tedious # MSSQL
Usage & configuration
rts.sequelize = {
alect: 'mysql', // support: mysql, mariadb, postgres, mssql
tabase: 'test',
st: 'localhost',
rt: '3306',
ername: 'root',
ssword: '',

rts.sequelize = {
able: true,
ckage: 'egg-sequelize'

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 |

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 ...

pp/model/user.js

le.exports = app => {
nst { STRING, INTEGER, DATE } = app.Sequelize;

nst User = app.model.define('user', {
login: STRING,
name: STRING(30),
password: STRING(32),
age: INTEGER,
last_sign_in_at: DATE,
created_at: DATE,
updated_at: DATE,
;

er.findByLogin = function* (login) {
return yield this.findOne({
  where: {
    login: login
  }
});


er.prototype.logSignin = function* () {
yield this.update({ last_sign_in_at: new Date() });


turn User;

Now you can use it in your controller:

pp/controller/user.js
le.exports = app => {
turn class UserController extends app.Controller {
* index() {
  const users = yield this.ctx.model.User.findAll();
  this.ctx.body = users;
}

* show() {
  const user = yield this.ctx.model.User.findByLogin(this.ctx.params.login);
  yield user.logSignin();
  this.ctx.body = user;
}


Full example
pp/model/post.js

le.exports = app => {
nst { STRING, INTEGER, DATE } = app.Sequelize;

nst Post = app.model.define('Post', {
name: STRING(30),
user_id: INTEGER,
created_at: DATE,
updated_at: DATE,
;

st.associate = function() {
app.model.Post.belongsTo(app.model.User, { as: 'user' });


turn Post;

s
pp/controller/post.js
le.exports = app => {
turn class PostController extends app.Controller {
* index() {
  const posts = yield this.ctx.model.Post.findAll({
    attributes: [ 'id', 'user_id' ],
    include: { model: this.ctx.model.User, as: 'user' },
    where: { status: 'publish' },
    order: 'id desc',
  });

  this.ctx.body = posts;
}

* show() {
  const post = yield this.ctx.model.Post.findById(this.params.id);
  const user = yield post.getUser();
  post.setDataValue('user', user);
  this.ctx.body = post;
}

* destroy() {
  const post = yield this.ctx.model.Post.findById(this.params.id);
  yield post.destroy();
  this.ctx.body = { 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
dule.exports = app => {
if (app.config.env === 'local') {
  app.beforeStart(function* () {
    yield app.model.sync({force: true});
  });
}

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:

m run migrate:up

For unittest environment:

G_SERVER_ENV=unittest npm run migrate:up

or for prod environment:

G_SERVER_ENV=prod npm run migrate:up

or for others environment:

G_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:

 strict';
t co = require('co');

le.exports = {
: co.wrap(function *(db, Sequelize) {
const { STRING, INTEGER, DATE } = Sequelize;

yield db.createTable('users', {
  id: { type: INTEGER, primaryKey: true, autoIncrement: true },
  name: { type: STRING, allowNull: false },
  email: { type: STRING, allowNull: false },
  created_at: DATE,
  updated_at: DATE,
});

yield db.addIndex('users', ['email'], { indicesType: 'UNIQUE' });
,

wn: co.wrap(function *(db, Sequelize) {
yield db.dropTable('users');
,

And you may need to read Sequelize - Migrations to learn about how to write Migrations.

Recommended example
Questions & Suggestions

Please open an issue here.

License

MIT


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.