在 pg-promise 中使用带有参数列表的 .batch
Using .batch with list of parameters in pg-promise
我是 运行 nodejs 和 pg-promise,我想使用批处理功能创建一个事务,其中包含多个更新的 BEGIN 和 COMMIT。
这是我的代码:
db.tx(function (t) {
return this.batch(function() {
for (var i = 0; i < cars.length; i++) {
return db.any('UPDATE ... ', [car_id, cars[i].votes]);
}
});
})
但是,它似乎没有工作,因为没有任何反应。不能为这样的输入创建我的批处理列表吗?
方法batch不将函数作为参数,它需要一个promise数组来解析。
并且有很多关于如何使用它的示例(也在 Whosebug 上),从官方文档开始:Transactions.
对于一组更新,您只需创建一组更新查询,然后使用 batch:
执行它们
db.tx(t => {
const queries = cars.map(c => {
return t.none('UPDATE ... ', [c.car_id, c.votes]);
});
return t.batch(queries);
})
.then(data => {
// success
})
.catch(error => {
// error
});
额外
同一类型的多个更新可以作为单个查询执行,以获得更好的性能。参见 Performance Boost and method helpers.update。
我是 运行 nodejs 和 pg-promise,我想使用批处理功能创建一个事务,其中包含多个更新的 BEGIN 和 COMMIT。
这是我的代码:
db.tx(function (t) {
return this.batch(function() {
for (var i = 0; i < cars.length; i++) {
return db.any('UPDATE ... ', [car_id, cars[i].votes]);
}
});
})
但是,它似乎没有工作,因为没有任何反应。不能为这样的输入创建我的批处理列表吗?
方法batch不将函数作为参数,它需要一个promise数组来解析。
并且有很多关于如何使用它的示例(也在 Whosebug 上),从官方文档开始:Transactions.
对于一组更新,您只需创建一组更新查询,然后使用 batch:
执行它们db.tx(t => {
const queries = cars.map(c => {
return t.none('UPDATE ... ', [c.car_id, c.votes]);
});
return t.batch(queries);
})
.then(data => {
// success
})
.catch(error => {
// error
});
额外
同一类型的多个更新可以作为单个查询执行,以获得更好的性能。参见 Performance Boost and method helpers.update。