Define instance methods and static methods in sequence

Time:2020-4-5

Maybe it’s just like memongooseTo switch tosequelizeOf them.
Methods recommended in the official documents first:

const {Sequelize, Model} = require('sequelize');
const sequelize = require('../../base/db');

class User extends Model {

  //Static methods are equivalent to those defined on mongoose schema.statics
  static method() {
     return 'statics function';
  };

  //The instance method is equivalent to the method defined on mongoose schema.methods
  method() {
    return 'methods function';
  }
}

User.init({
  id: {
    type: Sequelize.INTEGER(11),
    primaryKey: true,
    autoIncrement: true,
  },
  name: {
    type: Sequelize.STRING,
  },
}, {sequelize, tableName: 'users'});

There is a project to be upgraded recently, and the database will be upgraded frommongodbMigrate tomysql, so you need to learn to use sequelize to replace the original mongose.
Prior tonodeAll are used.mongose, used to useschema.methodsDefine instance methods usingschema.staticsTo define static methods.
Because there are many project models, in order to be easier to maintain, different methods are also maintained separately by files
Define instance methods and static methods in sequence
mongooseProject modelindex.jsWriting method

const mongoose = require('mongoose');

const schema = require('./schema');
const methods = require('./methods');
const statics = require('./statics');

const User = new mongoose.Schema(schema);

for (const attr in methods) {
  User.methods[attr] = methods[attr];
}

for (const attr in statics) {
  User.statics[attr] = statics[attr];
}

module.exports = mongoose.model('users', User);

sequelizeDefine the method inclassAbove, what if I want to maintain a single file as before?
Think about it. There are two options.
1. Write for each fileclassGradual succession

// statics.js
class UserStatics extends Model {
  //Static method
  static method() {
  };
}

// methods.js
class UserMethods extends UserStatics {
  //Instance method
  method() {
  };
}

// index.js
class User extends UserMethods {

}

If I adopt this scheme, it will be very difficult for my original project to upgrade, which means that every file must be rewritten.
2. Add method directly to class (function)
Think about it again, actuallyES6 classIt’s just the syntax sugar of the constructor

class User {
  static method() {
    return 'statics function';
  }
  method() {
    return 'methods function';
  }
}

Be equal to

function User() {
}

User.method = function () {
  return 'statics function';
};

User.prototype.method = function () {
  return 'methods function';
};

It’s easy to think about it. I’ll soonindex.jsWrite it out
sequelizeProject modelindex.jsWriting method

const {Model} = require('sequelize');
const sequelize = require('../../base/db');

const schema = require('./schema');
const methods = require('./methods');
const statics = require('./statics');

class User extends Model {
}

for (const attr in methods) {
  User.prototype[attr] = methods[attr];
}

for (const attr in statics) {
  User[attr] = statics[attr];
}

User.init(schema, {sequelize, tableName: 'users'});

module.exports = User;

In this way, it is directly compatible with my original file structure, and the rest is the switching adjustment of syntax and logic