Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support to define mode by class style #35

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

leaves4j
Copy link

Checklist
  • npm test passes
  • tests and/or benchmarks are included
  • documentation is changed or added
  • 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.

@codecov-io
Copy link

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%> (ø) ⬆️

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants