Node.js 如何在sequelize中自定义关联方法
假设我有一个用户和项目。 然后我在它们之间创建一个1:m的关联Node.js 如何在sequelize中自定义关联方法,node.js,sequelize.js,Node.js,Sequelize.js,假设我有一个用户和项目。 然后我在它们之间创建一个1:m的关联 const User = sequlize.define("user", {...}) const Project = sequelize.define("project". {...}) User.hasMany(Project, { as: "projects" }) 然后sequelize创建 addProject 方法为我自动 在其他情况下,我可能需要一个自定义函数 例如,我可能想检查是否允许该用户拾取 这个项目 在
const User = sequlize.define("user", {...})
const Project = sequelize.define("project". {...})
User.hasMany(Project, { as: "projects" })
然后sequelize创建
addProject
方法为我自动
在其他情况下,我可能需要一个自定义函数
例如,我可能想检查是否允许该用户拾取
这个项目
在其他情况下,我可能还希望防止sequelize
将这些关联方法添加到我的对象
我的问题是:
范围
字段可以实现的所有定制。例如:
models.User.belongsToMany(
models.List,
{
through: {
model: models.UserList,
scope: {
owner: true,
},
},
as: {
singular: 'OwnedList',
plural: 'OwnedLists',
},
});
models.User.belongsToMany(
models.List,
{
through: models.UserList,
as: {
singular: 'List',
plural: 'Lists',
},
});
user.associate = function (models) {
models.User.belongsToMany(
models.List,
{
through: models.UserList,
as: {
singular: 'List',
plural: 'Lists',
},
});
models.User.belongsToMany(
models.List,
{
through: {
model: models.UserList,
scope: {
owner: true,
},
},
as: {
singular: 'OwnedList',
plural: 'OwnedLists',
},
});
/**
* Deleting method createList to prevent list creation
* without owner
* List must be created using user.createOwnedList
*/
delete user.prototype.createList;
};
因此,当我在UserList
上调用user.addOwnedList(…)
属性owner
时,默认情况下true
。与1:m关联相同,一些字段可以通过范围设置。此外,您还可以使用不同的Association作用域,并将它们除以作为字段。
例如:
models.User.belongsToMany(
models.List,
{
through: {
model: models.UserList,
scope: {
owner: true,
},
},
as: {
singular: 'OwnedList',
plural: 'OwnedLists',
},
});
models.User.belongsToMany(
models.List,
{
through: models.UserList,
as: {
singular: 'List',
plural: 'Lists',
},
});
user.associate = function (models) {
models.User.belongsToMany(
models.List,
{
through: models.UserList,
as: {
singular: 'List',
plural: 'Lists',
},
});
models.User.belongsToMany(
models.List,
{
through: {
model: models.UserList,
scope: {
owner: true,
},
},
as: {
singular: 'OwnedList',
plural: 'OwnedLists',
},
});
/**
* Deleting method createList to prevent list creation
* without owner
* List must be created using user.createOwnedList
*/
delete user.prototype.createList;
};
因此,要查找所有列表
将使用user.getlist(…)
并与其他用户共享列表,将使用“user2.addList(…)”,因此属性所有者
将为false
。要仅获取拥有的列表,将使用user.getOwnedLists(…)
result SQL将类似于其中“owner”=true
我可以阻止sequelize添加特定的关联方法吗
您不能阻止创建方法,但可以在关联后立即将其删除。例如:
models.User.belongsToMany(
models.List,
{
through: {
model: models.UserList,
scope: {
owner: true,
},
},
as: {
singular: 'OwnedList',
plural: 'OwnedLists',
},
});
models.User.belongsToMany(
models.List,
{
through: models.UserList,
as: {
singular: 'List',
plural: 'Lists',
},
});
user.associate = function (models) {
models.User.belongsToMany(
models.List,
{
through: models.UserList,
as: {
singular: 'List',
plural: 'Lists',
},
});
models.User.belongsToMany(
models.List,
{
through: {
model: models.UserList,
scope: {
owner: true,
},
},
as: {
singular: 'OwnedList',
plural: 'OwnedLists',
},
});
/**
* Deleting method createList to prevent list creation
* without owner
* List must be created using user.createOwnedList
*/
delete user.prototype.createList;
};
因此,当有人调用方法user.createList
时,将导致undefined不是一个函数
错误。这不是一个完美的解决方案,但至少它在没有复杂约束的情况下增加了更多的安全性和数据一致性