How to extend the Sequelize model?

Is there a way that I could use a base class that has all the common attributes and methods for my models, but is not related to the database table, and then I could extend this base class when defining new models.

Here I created a basic human model in node express. I need the human class to expand from the base class.

const person = sequelizeClient.define('person', { name: { type: DataTypes.STRING, allowNull: false } }, { hooks: { beforeCount(options) { options.raw = true; } } }); const base = sequelizeClient.define('base', { id: { type: Sequelize.INTEGER, autoIncrement: true, primaryKey: true }, createdBy: { type: Sequelize.INTEGER, }, updatedBy: { type: Sequelize.INTEGER, }, deletedBy: { type: Sequelize.INTEGER, } }, { hooks: { beforeCount(options) { options.raw = true; } } }); 

The documentation states that

Sequelize models are ES6 classes. You can easily add your own class or class methods.

How to do this using ES6 extends the class template?

There is a question similar to this question, but not updated recently. How to extend the Sequelize model

+10
javascript ecmascript-6 express feathersjs
source share
2 answers

How can I do this using ES6 extends pattern pattern?

The statement in Sequelize docs is a bit confusing to developers. This does not mean that you can extend a specific model using the syntax of the ES6 class as follows.

 const User = db.define('User', { name: sequelize.STRING, age: sequelize.INTEGER }); // This is impossible. class Staff extends User { ... } 

But you can define instance methods by referring to the prototype, as shown below.

 const User = db.define('User', { name: sequelize.STRING, age: sequelize.INTEGER }); User.Instance.prototype.test = function() { return 'Name: ${this.name}, Age: ${this.age}' } User.create({ name: "John", age: 10 }); User.findOne().then((user) => { console.log(user.test()) // "Name: John, Age: 10" }); 

The statement you mentioned in the Sequelize doc actually says that you can improve the behavior of the model using a prototype extension , so you cannot do something like inherit the model you are trying to do in the question.

There is a lot of discussion about the proposal to implement the syntax of the ES6 class in Sequelize, like this , but it is still under discussion.

+4
source share

Since it seems that with Sequelize, extending the class model is not possible, what about extending the config object of the sequelize object? This is a bit more work, but provides the closest experience and remains relatively clean. Of course, using the attributes and options parameters requires the use of 2 separate classes or the proper destructuring of the merged class.

 class Base { constructor() { this.id = { type: Sequelize.INTEGER, autoIncrement: true, primaryKey: true }; ... } } class Person extends Base { constructor() { super(); this.name = { type: DataTypes.STRING, allowNull: false }; ... } } // using separate class for attributes const person = sequelize.define('person', new Person()); // possible destructuring of attributes/options const someModel= sequelize.define('someModel', (new SomeModel()).attributes, (new SomeModel()).options); 
0
source share

All Articles