Node.js 如何在sequelize中自定义关联方法

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 方法为我自动 在其他情况下,我可能需要一个自定义函数 例如,我可能想检查是否允许该用户拾取 这个项目 在

假设我有一个用户和项目。 然后我在它们之间创建一个1:m的关联

const User = sequlize.define("user", {...})
const Project = sequelize.define("project". {...})

User.hasMany(Project, { as: "projects" })
然后sequelize创建

addProject 
方法为我自动

在其他情况下,我可能需要一个自定义函数

例如,我可能想检查是否允许该用户拾取 这个项目

在其他情况下,我可能还希望防止sequelize 将这些关联方法添加到我的对象

我的问题是:

  • 有没有办法创建自定义关联方法, 如果没有,我至少可以定义一个验证方法吗

  • 我可以阻止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不是一个函数
    错误。这不是一个完美的解决方案,但至少它在没有复杂约束的情况下增加了更多的安全性和数据一致性