Vahid Najafi
Vahid Najafi

Reputation: 5243

Nodejs extending Sequelize model

I'm using Sequelize in a project. There are some models there, such as the following:

class Agents extends CommonModel {

  static init(sequelize) {
    return super.init({
      ...super.baseFields,
      userId: {
        type: DataTypes.BIGINT,
        references: User
      },
      ...
    }, {
        sequelize,
        timestamps: true,
        paranoid: true
      })
  }

Also in CommonModel:

class CommonModel extends Model {

  static baseFields = {
    createdBy: {
      type: DataTypes.INTEGER,
      references: {
        model: 'Users',
        key: 'id'
      }
    },
    ...
  }
  
}

Now, here is the question, what is the sequelize parameter in init function? (I don't call it directly). When is init going to being called? (there is no constructor though) Is this a special design pattern?

Upvotes: 1

Views: 377

Answers (1)

meBe
meBe

Reputation: 164

  1. I think keyword super refers to parent class and constructs it with super.init() arguments.
  2. you did call sequelize in Agent model so parameter is needed right? and maybe class model has sequelize variable

Upvotes: 1

Related Questions