区分三向关系

时间:2018-07-25 12:08:12

标签: node.js sequelize.js feathersjs

我有3种关系,需要通过续集进行建模。 OrganisationUsers:[id,userId,roleId,OrganisationId];

我试图如下创建它:

组织用户:

module.exports = (app) => {
  const sequelizeClient = app.get('sequelizeClient');
  const organisationUsers = sequelizeClient.define('organisationUsers', {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
  }, {
      hooks: {
        beforeCount(options) {
          options.raw = true;
        }
      }
    });

  // eslint-disable-next-line no-unused-vars
  organisationUsers.associate = (models) => {
    // Define associations here
    // See http://docs.sequelizejs.com/en/latest/docs/associations/
    organisationUsers.belongsTo(models.roles);
  };

  return organisationUsers;
};

用户:

module.exports = (app) => {
  const sequelizeClient = app.get('sequelizeClient');
  const users = sequelizeClient.define('users', {
    email: {
      type: DataTypes.STRING,
      allowNull: false,
      unique: true,
      validate: {
        isEmail: true
      }
    },
    password: {
      type: DataTypes.STRING,
      allowNull: false
    }
  }, {
      hooks: {
        beforeCount(options) {
          options.raw = true;
        }
      }
    });

  // eslint-disable-next-line no-unused-vars
  users.associate = (models) => {
    // Define associations here
    // See http://docs.sequelizejs.com/en/latest/docs/associations/
    users.belongsToMany(models.organisations, { through: organisationUsers(app) });
  };

  return users;
};

组织:

module.exports = (app) => {
  const sequelizeClient = app.get('sequelizeClient');
  const organisations = sequelizeClient.define('organisations', {
    name: {
      type: DataTypes.STRING,
      allowNull: false,
    },
  }, {
      hooks: {
        beforeCount(options) {
          options.raw = true;
        }
      }
    });

  // eslint-disable-next-line no-unused-vars
  organisations.associate = (models) => {
    // Define associations here
    // See http://docs.sequelizejs.com/en/latest/docs/associations/
    organisations.belongsToMany(models.users, { through: organisationUsers(app) });
  };

  return organisations;
};

我想为OrganisationUsers模型的实例设置角色。 当我跑步时:

const orgUser = await organisationUsers(app).find({
   where: {
      userId: 1,
     organisationId: 1
   }
});
await orgUser.setRole(superRoleId);

我得到的orgUser.setRole不是函数。

是否有更好的方法来建立这种关系的模型? 我不确定为什么setRole不是函数?

任何帮助/建议/建议将不胜感激。

感谢与问候,

埃米尔

0 个答案:

没有答案