我正在编写一些 PostgreSQL 事务,一旦执行了 forEach 中的每个函数,我就需要执行回调。这是一些代码:
var sql = "BEGIN;UPDATE object SET name = "+data.name+", object_subtype_id = "+data.object_subtype_id+" WHERE id = "+data.id+";";
db.driver.execQuery(sql, function(err, result) {
data.object_subtype.object_property_type.forEach(function(item) {
db.driver.execQuery("WITH upsert AS (UPDATE object_property SET value = '"+item.value+"' WHERE object_property_type_id = "+item.id+" AND object_id = "+data.id+" RETURNING *) INSERT INTO object_property (object_property_type_id, object_id, value) SELECT "+item.id+", "+data.id+", '"+item.value+"' WHERE NOT EXISTS (SELECT * FROM upsert);", function(err, nb) {
// I need to send the COMMIT; here once all the functions in the forEach have been executed
});
});
});
我看过async但我不确定如何或者如果可以的话,将它应用于我的情况。
有什么想法吗?
谢谢!