Loopback4中的事务支持?

时间:2019-02-09 09:26:26

标签: transactions loopbackjs

如何在Loopback4中支持交易? 是否有相同的装饰器,例如@Transaction?

我在https://loopback.io/doc/en/lb4/Repositories.html处查看了文档,但未提及该交易。

2 个答案:

答案 0 :(得分:1)

LoopBack 4现在支持相当长一段时间的数据库事务:

import {
  Transaction,
  DefaultTransactionalRepository,
  IsolationLevel,
} from '@loopback/repository';

// assuming there is a Note model extending Entity class, and
// ds datasource which is backed by a transaction enabled
// connector
const repo = new DefaultTransactionalRepository(Note, ds);
// Now we have a transaction (tx)
const tx = await repo.beginTransaction(IsolationLevel.READ_COMMITTED);

const created = await repo.create({title: 'Groceries'}, {transaction: tx});
const updated = await repo.update(
  {title: 'Errands', id: created.id},
  {transaction: tx},
);

// commit the transaction to persist the changes
await tx.commit();

进一步阅读:

答案 1 :(得分:0)

LoopBack 4不支持开箱即用的事务。请打开一个新的GitHub问题以讨论如何实现此功能: