npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

egg-isequelize

v2.0.0-beta.3

Published

egg Sequelize(v5) plugin

Downloads

505

Readme

egg-isequelize

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
exports.sequelize = {
  client: {
    dialect: 'mysql', // support: mysql, mariadb, postgres, mssql
      database: 'test',
      host: 'localhost',
      port: '3306',
      username: 'root',
      password: '',
  },
  // or multiple source
  clients: {
    mysql: {
      dialect: 'mysql', // support: mysql, mariadb, postgres, mssql
      database: 'test',
      host: 'localhost',
      port: '3306',
      username: 'root',
      password: '',
    },
    pg: {
      ...
    }
  },
};
  • config/plugin.js
exports.sequelize = {
  enable: true,
  package: 'egg-isequelize'
}
  • package.json
{
  "scripts": {
    "migrate:new": "egg-sequelize migration:create",
    "migrate:up": "egg-sequelize db:migrate",
    "migrate:down": "egg-sequelize db:migrate:undo"
  }
}

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

// multiple source
const mysql = app.model.get('mysql');
const pg = app.model.get('pg');
...
// app/model/user.js

module.exports = app => {
  const { STRING, INTEGER, DATE } = app.Sequelize;

  const 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,
  });

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

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

  return User;
};

Now you can use it in your controller:

// app/controller/user.js
module.exports = app => {
  return 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

// app/model/post.js

module.exports = app => {
  const { STRING, INTEGER, DATE } = app.Sequelize;

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

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

  return Post;
};
// app/controller/post.js
module.exports = app => {
  return 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
  module.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:

$ 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 = require('co');

module.exports = {
  up: 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' });
  }),

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

  • https://github.com/eggjs/examples/tree/master/sequelize-example/

Questions & Suggestions

Please open an issue here.

License

MIT