egg-sequelize icon indicating copy to clipboard operation
egg-sequelize copied to clipboard

Support to define mode by class style

Open leaves4j opened this issue 7 years ago • 1 comments

Checklist
  • [x] npm test passes
  • [x] tests and/or benchmarks are included
  • [x] documentation is changed or added
  • [x] commit message follows commit guidelines
Affected core subsystem(s)
Description of change

Support to define mode by class style

// app/model/user.js
const { Sequelize, Model } = require('egg-sequelize');
const { STRING, INTEGER, DATE } = Sequelize;

class User extends Model {
  static get modelOptions() {
    const modelName = 'user';
    const attributes = {
      login: STRING,
      name: STRING(30),
      password: STRING(32),
      age: INTEGER,
      last_sign_in_at: DATE,
      created_at: DATE,
      updated_at: DATE,
    };
    const options = {};
    return { modelName, attributes, options };
  }

  async logSignin() {
    await this.update({ last_sign_in_at: new Date() });
  }

  static findByLogin(login) {
    return this.findOne({ login });
  }
}

module.exports = User;

Loader will check whether the model class has the modelOptions property, and then automatically initialize the model.

leaves4j avatar Dec 22 '17 08:12 leaves4j

Codecov Report

Merging #35 into master will not change coverage. The diff coverage is 100%.

Impacted file tree graph

@@          Coverage Diff          @@
##           master    #35   +/-   ##
=====================================
  Coverage     100%   100%           
=====================================
  Files           5      6    +1     
  Lines          37     48   +11     
=====================================
+ Hits           37     48   +11
Impacted Files Coverage Δ
index.js 100% <100%> (ø)
lib/loader.js 100% <100%> (ø) :arrow_up:

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 7b57739...f61358d. Read the comment docs.

codecov-io avatar Dec 22 '17 09:12 codecov-io