Rethinkdb 在数据库中链接查询
假设我有一个“category”表,每个类别在“data”表中都有关联数据,在其他表中也有关联数据“associated”,我想删除一个类别及其所有关联数据。 我现在做的是这样的:Rethinkdb 在数据库中链接查询,rethinkdb,rethinkdb-javascript,Rethinkdb,Rethinkdb Javascript,假设我有一个“category”表,每个类别在“data”表中都有关联数据,在其他表中也有关联数据“associated”,我想删除一个类别及其所有关联数据。 我现在做的是这样的: getAllDataIdsFromCategory() .then(removeAllAssociated) .then(handleChanges) .then(removeDatas) .then(handleChanges) .th
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()
}
有没有办法在数据库端链接这些查询?
我的函数目前如下所示:
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(),因为我想根据上一次查询的结果进行查询。
我的方法的问题是,它无法处理大量的“数据”,因为我必须将所有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())
});