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...
}
}
}
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()
})
})
// 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()
})
})
4 Replies
Prisma AI Help
You chose to debug with a human. They'll tinker with your query soon. If you get curious meanwhile, hop into #ask-ai for a quick spin!
Nurul
Nurul3w ago
Hey @tort6334 👋 Did you had a look at this extension? https://github.com/olivierwilkinson/prisma-extension-nested-operations
GitHub
GitHub - olivierwilkinson/prisma-extension-nested-operations: Utils...
Utils for creating Prisma client extensions with nested operations - olivierwilkinson/prisma-extension-nested-operations
tort6334
tort6334OP3w ago
Thanks @Nurul (Prisma) ! ❤️ yeah that one will do the trick. But a thing to probably notice is that it's CPU and not IO to look through all nested objects. But I guess it has minor effect because most queries not that deep?
Nurul
Nurul3w ago
Yes, I don't expect it to have any major effect. Please feel free to submit a bug report in case you observe any major issues.

Did you find this page helpful?