链接查询在rethinkdb



假设我有一个"category"表,每个类别在"data"表中有关联数据,在其他表"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.r expr()或r.r do(),因为我想基于前一个查询的结果执行查询。我的方法的问题是,它不会为大量的"数据"工作,因为我必须把所有的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())
});

最新更新