如何扩展Sequelize模型? [英] How to extend a Sequelize model?

查看:403
本文介绍了如何扩展Sequelize模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法可以使用具有模型所有通用属性和方法但不与数据库表链接的基类,然后可以在定义新模型时扩展该基类.

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

在这里,我在node express中创建了基本的人模型.我需要从基类扩展人员类.

Here I have created the base, person model in node express. I need the person class to be extended 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;
      }
    }
  });

在文档中指出

序列化模型是ES6类.您可以非常轻松地添加自定义实例或类级别的方法.

Sequelize Models are ES6 classes.You can very easily add custom instance or class level methods.

如何使用ES6扩展类模式来做到这一点?

How can I do this using ES6 extends class pattern?

有一个与此类似的问题,但最近未更新. 如何扩展Sequelize模型

There's a question similar to this but has not been updated recently. How to extend Sequelize model

推荐答案

如何使用ES6扩展类模式来做到这一点?

How can I do this using ES6 extends class pattern?

Sequelize docs中的语句对于开发人员来说有点令人困惑.这并不意味着您可以使用ES6类语法扩展定义的模型,如下所示.

The statement in Sequelize docs is a little bit confusing for developers. That does not mean that you can extend the defined model with ES6 class syntax as follow.

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 accessing protoype like 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"
});

您在Sequelize文档中提到的声明实际上说的是,您可以使用基于 prototype 的扩展来增强模型行为,因此您不能执行在问题中尝试进行的模型继承之类的事情.

The statement you mentioned in Sequelize doc actually says is that you can enhance model behaviour using prototype based extension, so you cannot do something like model inheritance you try to do in the question.

关于Sequelize中ES6类语法的实现建议,有很多讨论,例如 ,但看起来仍在讨论中.

There are lots of discussion about implementation proposal of ES6 class syntax in Sequelize like this, but it is still under discussion it looks.

这篇关于如何扩展Sequelize模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆