Possible to change the operation inside Client Extension? #21530
Replies: 5 comments 3 replies
|
I guess you can pass down the Prisma client instance and then return the operation you want to make: Hope this helps! |
|
Hi @haleksandre 👋 Yes, you can modify the operation of a query using Prisma Client extensions. Prisma Client extensions allow you to hook into the query life-cycle and modify an incoming query or its result. You can use the query extension component to create custom behavior for specific operations on models. const prisma = new PrismaClient().$extends({
query: {
user: {
async delete({ args, query }) {
// Custom logic to implement soft delete
// For instance, setting a 'deleted' flag instead of actual deletion
args.data = { ...args.data, deleted: true };
args.where = { ...args.where }; // Ensure the 'where' clause is preserved
return query.update(args); // Use the update method with the modified args
},
},
},
});In this example, the delete operation is modified to perform an update instead, setting a deleted flag on the user record. If this answers your question, it would be great if you could mark this Discussion as answered to indicate that it has been resolved. Otherwise please let us know how else we can help you further or close the Discussion if it was resolved in some other way 🙏 |
|
Same error on my side in prisma 5.22 `import { Prisma } from '@prisma/client'; //extension for filtering soft deleted rows from queries }); I have the error : this.update is not a function |
|
I also had to figure this out. Here's what I did: |
|
Hi there, To keep our discussions organized and focused on the most relevant topics, we’re reviewing and tidying up our backlog. As part of this process, we’re closing discussions that haven’t had any recent activity and appear to be outdated. If this discussion is still important to you or unresolved, we’d love to hear from you! Feel free to reopen it or start a new one with updated details. For more details about our priorities and vision for the future of Prisma ORM, check out our latest blog post: https://www.prisma.io/blog/prisma-orm-manifesto. Thank you for your understanding and being part of the community! |


Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Question
Is it possible to modify the operation of a query?
The use case is to implement a
Soft Deletebut instead of adding asoftDeletemethod, I'd prefer to change the behaviour of thedeleteoperation on models & add a customforceDeletethat actually deletes.Is it possible to implement some kind of behaviour like in the
'delete' + custom 'forceDelete' APIexample?All reactions