use*_*363 3 node.js sequelize.js
我想添加一个实例方法来Sequelize User建模postgres。 User模型定义如下:
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
});
Run Code Online (Sandbox Code Playgroud)
我在模型中寻找这样的东西User:
User.instanceMethods.create(someMethod => function() {
//method code here
});
Run Code Online (Sandbox Code Playgroud)
实例方法可以这样访问:
let user = new User();
user.someMethod();
Run Code Online (Sandbox Code Playgroud)
有Instance用于模型,Sequelize但不是用于实例方法。在Sequelize模型中添加实例方法的正确方法是什么?
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
});
// This is an hook function
User.beforeSave((user, options) => {
// Do something
});
// This is a class method
User.classMethod = function (params) {
// Do something with params
}
// This is an instance method
User.prototype.instanceMethod = function (params) {
// Do something with params
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4859 次 |
| 最近记录: |