How to share the transaction in multiple services?

Hi,

This is more like a design question.

I'm using Drizzle ORM in Express.js application. I have userService and auditService to update the database records. Before the database transaction, I basically call the services like this,
router.post('/', async (req, res) => {
  userService.create('john')
  auditService.log('Create user john')
})


and then with the database transaction,
router.post('/', async (req, res) => {
  await db.transaction(async (tx) => {
    userService.create(tx, 'john')
    auditService.log(tx, 'Create user john')
  })
})


Note that in the above, I have to pass the tx transaction to every service function. Is there a more elegant way than passing the tx to every function?
Was this page helpful?