Skip to content

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
Development

Successfully merging this pull request may close these issues.

2 participants