如何使用massive.js执行事务

时间:2017-08-08 15:02:34

标签: node.js postgresql massivejs

我试图做一件非常简单的事情:

START TRANSACTION;
DELETE FROM table WHERE id = 1;
ROLLBACK;

postgres数据库上运行此功能非常有效。使用massive.js时,它不会:

this.db.run(
   "START TRANSACTION",
   []
); 
setTimeout(() => {
   this.db.run(
       "DELETE FROM table WHERE id = $1"
       [1]
   );
}, 2000);
setTimeout(() => {
   this.db.run(
       "ROLLBACK;"
       []
   );
}, 4000);

它不会回滚更改,只是从数据库中删除。 COMMIT也不起作用。怎么了?

有没有办法转储查询订单?

1 个答案:

答案 0 :(得分:0)

下方大量使用pg-promise,后者又支持交易:

db.instance.tx(t => {
    // BEGIN has been executed
    return t.none('DELETE FROM table WHERE id = $1', [123])
        .then(() => {
            // Records have been deleted within the transaction

            throw new Error('Random error to fail the transaction');
            // = the same as returning Promise.reject(new Error('Random...'))
        });
})
    .catch(error => {
        // ROLLBACK has been executed, no records end up deleted.
        console.log(error);
    });

另见:

  

有没有办法转储查询订单?

Monitoring Queries向您展示了如何操作,或者在初始化db.driverConfig时可以在massive对象中添加事件query处理程序。