PrismaP
Prisma11mo ago
4 replies
tort6334

Is it possible to subscribe to a creation of a model?

So I have a ticket model, and I want to execute an handler when a new model is being created, the thing is, if i subscribe to a middleware or $extend, I need to specify from where exactly i create that model, but the thing is, there are many options via nested queries to do that, e.g.:
model1: {
  create: {
     model2: {
       ...
         create: { ticket: ... } <--- I want to know when a new ticket is being created/connected/etc...
     }
  }
}


How can I do that?

And a follow up question, whenever I create a new ticket, I also want to execute something else (push a message broker event), so if one of them fail i want to fallback the query, i.e. i need a $transaction. How can I do that?

Pseudo code of what I want:

// subscribe whenever a ticket is created, no matter if its via a nested query or whatever
subscribe.create.ticket((model, operation, args, db) => {
  db.$transaction((tx) => {
    await tx.model.operation(args)
    await anotherTask()
  })
})
Was this page helpful?