使用Knex.js还原已提交的事务

时间:2019-02-07 16:25:05

标签: javascript transactions knex.js

是否可以像这样还原已提交的事务:

const trx = await transaction.start(Model.knex());

try {
  await doStuff(trx);
  await doOtherStuff(trx); // If this fails rollback is working well
  await trx.commit();
  await externalAPICall(); // External API call failed! Error!
} catch (err) {
  await trx.rollback(err); // Can I rollback if externalAPICall throwed and error? (Commit is already done)
}

1 个答案:

答案 0 :(得分:0)

我会这样:

await knex.transaction(async (trx) => {
  await doStuff(trx);
  await doOtherStuff(trx);
  await externalAPICall();
}

在此语法中,commit()和rollback()是隐式的:)