Transaction with hooks


#1

I have a query within a transaction to create a record in the database. Something like that:

const trx = await Database.beginTransaction()
// ...
await Request.create(data, trx)
// ...
await trx.commit()

But I have a afterSave hook in this same model (Request model), and apparently this causes the transaction never to end, causing a timeout error.

If I add the transaction with the afterSave hook, I get the timeout problem. But if I remove the transaction or the afterSave hook, I don’t get the error. But they don’t work together.

How can I have the transaction and the afterSave hook working together?

Many thanks,


#2

Funny, same question in quick succession. In short, you can’t. No way of passing the transaction to the hook but I think @virk has a few ideas moving forward.


#3

Hi guys, any lead about this topic?


#4

Sadly there isn’t any. However, I think we can stick the trx object on the Model instance and you use that from the transaction.

I can look onto working on same this week