如何在Loopback 4中实现事务处理

问题描述 投票:0回答:1

我正在进行两个存储库调用来更新db中两个不同表的数据。我想实现一个事务。需要帮助如何执行这个。https:/loopback.iodocenlb4Using-database-transactions.html。从文档上看,交易只能在一个仓库中执行,而不能在两个仓库之间执行。

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();

但我想

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

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

有谁知道如何做到这一点。

loopback loopback4 v4l2loopback
1个回答
0
投票
const created = await repo1.create({title: 'Groceries'}, {transaction: tx});
const updated = await repo2.update( {title: 'Errands', id: created.id},{transaction: tx}, );

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

是的,如果你的repo1和repo2属于同一个数据源,这是可能的。

更多信息 此处

© www.soinside.com 2019 - 2024. All rights reserved.