使用现有连接表的一对多关联

Not*_*ent 4 sails.js

我正在转换使用MariaDB的现有应用程序的后端来使用Sails(v0.10.0-rc7),并且我试图弄清楚如何在给定底层架构结构的情况下获取填充到Role模型中的角色的所有权限我必须与之合作.

有三个表用于当前获取角色及其相关权限,工作查询如下所示:

SELECT pm.permission, pm.permkey
FROM role_master rm
INNER JOIN role_perm rp ON ( rm.roleid = rp.roleid )
INNER JOIN perm_master pm ON ( rp.permid = pm.permid )
WHERE rm.roleid = 1
GROUP By pm.permission
Run Code Online (Sandbox Code Playgroud)

如您所见,role_master每个角色的个别权限中都有角色role_perm定义,最后是权限定义perm_master.

我已经阅读了关于协会的这个很棒的维基,但我没有看到任何可以帮助我的东西.

理想情况下,我最终想要的是一个输出的角色模型:

{
  "id"          : 1,
  "name"        : "My Role Name",
  "description" : "My Role Description",
  "permissions" : [ 'canread', 'canwrite', 'canjump', 'canplay' ]
}
Run Code Online (Sandbox Code Playgroud)

在不修改底层数据库的情况下实现此目的的最佳方法是什么?

par*_*ana 12

关于Waterline的一个好处是能够将模型映射到自定义表和列名称.但是目前我们没有很好的方法为自动生成的连接表执行此操作.我提出的早期作品之一是创建through关联的能力.这些本质上允许您构建一个充当连接表的模型.我们后来决定这基本上只是一个嵌套的填充,但我把through逻辑留在那里用于这样的用例.

您无法向直通表添加其他属性,但如果您为连接表绘制了两个值,则查询和蓝图路径将正常运行.现在有一个关于直通表所需的主键值的小注释,但这只是架构构建器中的一个错误,应尽快解决.

目前没有记录以下逻辑,但可以帮助您获得所需的结果.

请注意,这仍处于测试版状态,所以它还不会很稳固.我们没有在mysql适配器上进行正确的外连接调用,因此您将看到三个查询,然后结果将在应用程序层的内存中连接.这将被清理以执行单个SQL查询,就像您更新条件解析器时所期望的那样.

此外,无论何时使用现有数据,请确保您具有migrate: safe下面指定的标志,因此不会对当前数据库应用任何更改.

// Role.js
module.exports = {
  identity      : 'Role',
  tableName     : 'role_master',
  migrate       : 'safe',
  schema        : true,
  autoPK        : false,
  autoCreatedAt : false,
  autoUpdatedAt : false,

  attributes: {

    id : {
      columnName : 'rolefk',
      type       : 'string',
      required   : true,
      primaryKey : true,
      unique     : true,
      uuidv4     : true
    },

    // A Role can have many permissions through the roleperm model
    permissions : {
      collection : 'permission',
      through: 'roleperm'
    }

  }
};
Run Code Online (Sandbox Code Playgroud)
// Permission.js
module.exports = {
  identity      : 'Permission',
  tableName     : 'perm_master',
  migrate       : 'safe',
  schema        : true,
  autoPK        : false,
  autoCreatedAt : false,
  autoUpdatedAt : false,

  attributes : {

    id : {
      columnName : 'permfk',
      type       : 'string',
      required   : true,
      primaryKey : true,
      unique     : true,
      uuidv4     : true
    },

    // A Permission can belong to many roles using the roleperm model
    roles : {
      collection: 'role',
      through: 'roleperm'
    }

  }

};
Run Code Online (Sandbox Code Playgroud)
// RolePerm.js
module.exports = {
  identity      : 'RolePerm',
  tableName     : 'role_perm',
  migrate       : 'safe',
  schema        : true,
  autoPK        : false,
  autoCreatedAt : false,
  autoUpdatedAt : false,

  attributes : {

    // Due to a bug in the schema generator this seems to be needed at the
    // model level but not on the actual database.
    id: {
      type: 'integer',
      primaryKey: true
    },

    roleid : {
      model: 'role',
      columnName: 'role_id'
    },

    permid : {
      model: 'permission',
      columnName: 'perm_id'
    }

  }

};
Run Code Online (Sandbox Code Playgroud)