如何在 Sequelize 模型中添加实例方法 [英] How to add an instance method in Sequelize model
本文介绍了如何在 Sequelize 模型中添加实例方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想用 postgres
为 Sequelize
User
模型添加一个实例方法.User
模型定义如下:
I would like to add an instance method to Sequelize
User
model with postgres
. User
model is defined as below:
const Sql = require('sequelize');
const db = require("../startup/db");
const User = db.define('user', {
id: {type: Sql.INTEGER,
primaryKey:true,
min: 1},
name: {type: Sql.STRING,
allowNull: false,
min: 2,
max: 50
},
email: {type: Sql.STRING,
isEmail: true},
encrypted_password: {type: Sql.STRING,
min: 8},
createdAt: Sql.DATE,
updatedAt: Sql.DATE
});
我在 User
模型中寻找类似的东西:
I am looking for something like this in model User
:
User.instanceMethods.create(someMethod => function() {
//method code here
});
实例方法可以这样访问:
The instance method can be access like this:
let user = new User();
user.someMethod();
Sequelize
中有模型的Instance
,但实例方法没有.在 Sequelize
模型中添加实例方法的正确方法是什么?
There is Instance
for model in Sequelize
but it was not for the instance method. What is the right way to add instance method in Sequelize
model?
推荐答案
这是问题的答案.请参阅 Mariano
的回复.
Here is the answer for the question. See reply by Mariano
.
这里有更多阅读:https://codewithhugo.com/using-es6-classes-for-sequelize-4-models/
这篇关于如何在 Sequelize 模型中添加实例方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文