在 rethinkdb 中链接查询

Chaining queries in rethinkdb

假设我有一个 "category" table,每个类别在 "data" table 中都有关联数据,在其他 table 中也有关联数据s "associated" 并且我想删除一个类别及其所有关联数据。 我目前正在做的是这样的:

getAllDataIdsFromCategory()
        .then(removeAllAssociated)
        .then(handleChanges)
        .then(removeDatas)
        .then(handleChanges)
        .then(removeCategory)
        .then(handleChanges);

有没有办法在数据库端链接这些查询? 我的功能目前看起来像这样:

var getAllDataIdsFromCategory = () => {
        return r
            .table('data')
            .getAll(categoryId, { index: 'categoryId' })
            .pluck('id').map(r.row('id')).run();
    }

var removeAllAssociated = (_dataIds: string[]) => {
        dataIds = _dataIds;
        return r
            .table('associated')
            .getAll(dataIds, { index: 'dataId' })
            .delete()
            .run()
    }

var removeDatas = () => {
        return r
            .table('data')
            .getAll(dataIds)
            .delete()
            .run()
    }

请注意,我不能使用 r.expr() 或 r.do(),因为我想根据上一个查询的结果进行查询。 我的方法的问题是它不适用于大量 "data" 因为我必须将所有 id 带到客户端,并且在客户端循环中为其进行分页似乎是解决方法。

您可以为此使用 forEach

r.table('data').getAll(categoryID, {index: 'categoryId'})('id').forEach(function(id) {
  return r.table('associated').getAll(id, {index: 'dataId'}).delete().merge(
    r.table('data').get(id).delete())
});