我正在用Bookshelf.js编辑一些记录。记录的数量可能相当高。为了速度,我希望批量更新这些记录,或者至少在事务上下文中运行update循环。我如何在Bookshelf.js中做到这一点?如果这是不可能的,我如何使用Knex?
下面是我当前的更新功能:
new Endpoint()
.where({
'organization_id': orgId,
'id': endpointId
})
.save(updatedEndpoint, {patch: true})
.then((endpoint) => {
Endpoint
.where({
'organization_id': orgId,
'id': endpointId
})
.fetch({withRelated: ['settings', 'organization']})
.then((newEndpoint) => {
ReS(res, {
endpoint: newEndpoint
}, 200);
})
.catch(e => TE(e));
})
.catch(e => TE(e));发布于 2018-06-13 18:26:09
您将不得不求助于查询生成器(knex.js)来进行批量更新:
Endpoint
.query()
.whereIn('id', [1, 2, 3, /* ... */])
.andWhere({'organization_id': orgId})
.update({whatever: 'a value'})
.then(function(results) {
// ...
})有关更多信息,请参见第402期。
https://stackoverflow.com/questions/50838192
复制相似问题