如何根据联接 table 的列对 Bookshelf.js 查询的结果进行排序?

How can I sort the results of a Bookshelf.js query based on a column of the join table?

我正在尝试从 Bookshelf.js 查询中获取结果并按连接 table 中的列对它们进行排序。但是,在添加 'withRelated' 时,我只能获得目标 table 而不是连接 table 本身。下面是我到目前为止的代码。

MyTable.forge().query({where: {id: req.params.id}})
  .fetch({
    withRelated: [
      {'children.children': (qb) => { qb.orderBy('position', 'asc'); }}
    ]
  }).then(result => {
    res.send(JSON.stringify({theResult: result}));
  });

'orderBy' 应该按仅在联接 table 中的列 'position' 排序。似乎 'qb' 只有 returns 来自目标 table 而不是连接 table。我已经尝试 'withPivot' 但没有得到正确的结果。

因此,在查看返回的对象并阅读更多文档后,我发现 orderBy 的属性由 Bookshelf.js 更改为“_pivot_position”。 'pivot' 已添加到列名中,这就是您要使用的内容。但是,这似乎只适用于以下内容。

withRelated: [
      {'children': (qb) => { qb.orderBy('_pivot_position', 'asc'); }}
    ]

如上所示,我不得不排除 'children.children' 并只使用 'children'。我现在想知道 Bookshelf.js 是否允许您从子元素中指定多个 'orderBy' 列。