[英]How to extends Sequelize 4 Model in ES6 classes?
I want to make a class which extends Sequelize Model like below 我想制作一个扩展Sequelize Model的类,如下所示
const Sequelize = require('sequelize');
class MyModel extends Sequelize.Model {
constructor(){
super()
}
}
But I need to know how to define/init table name and attributes for this MyModel. 但是我需要知道如何为这个MyModel定义/初始化表名和属性。 I want every method of Sequelize.Model into MyModel
我希望将Sequelize.Model的每种方法都转换为MyModel
You can do it like this 你可以这样
To Define Models => 定义模型=>
const Sequelize = require("sequelize");
class MyModel extends Sequelize.Model {
static init(sequelize, DataTypes) {
return super.init(
{
myField: DataTypes.STRING
},
{ sequelize }
);
}
}
and to define associations 并定义关联
const Sequelize = require("sequelize");
class MyModel extends Sequelize.Model {
static associate(models) {
this.myAssociation = this.belongsTo(models.OtherModel);
// or
this.myAssociation = models.MyModel.belongsTo(models.OtherModel);
}
}
You can use it like this for more details follow from this blog. 您可以像这样使用它,有关更多详细信息,请访问此博客。 I have used it from here
我从这里用过的
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.