Как реализовать транзакцию в Loopback 4

Я делаю два вызова репозитория для обновления данных в двух разных таблицах в db. Я хочу осуществить транзакцию. Нужна помощь в том, как это сделать. https://loopback.io/doc/en/lb4/Using-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();

Кто-нибудь знает, как это сделать.


person Shashi Prakash Gautam    schedule 14.06.2020    source источник


Ответы (1)


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

Да, это возможно, если ваши репо1 и репо2 принадлежат одному источнику данных.

дополнительная информация здесь

person Madaky    schedule 14.06.2020