Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lifecycle hooks for crud operations. #9684

Open
PrinceShaji opened this issue Oct 9, 2021 · 5 comments
Open

Lifecycle hooks for crud operations. #9684

PrinceShaji opened this issue Oct 9, 2021 · 5 comments
Labels
kind/feature A request for a new feature. team/client Issue for team Client. topic: extend-client Extending the Prisma Client

Comments

@PrinceShaji
Copy link

Problem

Sometimes there's a need to run certain function before a record is added or maybe create another record or execute certain workflow after a record is created/deleted (eg: trigger websocket events), and it is useful to have away to tap into those lifecycle events.

Alternatives

Strapi has a great way of handling lifecycle events. Linking doc here.

@pantharshit00 pantharshit00 added kind/feature A request for a new feature. team/client Issue for team Client. topic: extend-client Extending the Prisma Client labels Nov 1, 2021
@millsp
Copy link
Member

millsp commented Aug 31, 2022

Hey there. I think you could build this with middlewares already. However, we will be bringing a way to write custom extensions for Prisma Client. I see this one could be built directly via $use, which is a better alternative to middlewares. I can also imagine this being a top-level (client) extension. To learn more about this, you can read our proposal.

@dzcpy
Copy link

dzcpy commented Sep 18, 2022

This is really useful. Probably is also the only reason I can't use Prisma

@thebiglabasky
Copy link

Hi @dzcpy, would you be able to elaborate why middlewares aren't meeting your expectations specifically? That would help to understand the differences which may exist between them and what you imagine this feature request to be.
Thank you!

@dzcpy
Copy link

dzcpy commented Sep 20, 2022

Hi @thebiglabasky, thanks for your reply.

I'm building an ecommerce platform using nestjs. There are a lot of enttities like orders, products, users etc., they are written in different modules. For a lot of them, I need to create lifecycle hooks, for example before updating some records, I'll do basic data validation, change the data user has input and then update some other entities.

So basically my needs can be concluded as the following:

  1. Entity level lifecycle hooks (for a specific entity instead of a global middleware).
  2. Data validation
  3. Ability to modify the data before update / insert
  4. Change some other records before and after insert / update / delete

For 2 and 4 it's better to put them in one place because in some cases, I'll need to change other records even when validation has failed

Also, I would like it to be modular because there are too many entities, using middleware to do it seems to be a bit inconvinient in term of maintainability. And if I use a midduleware for each single entities it's kind of cumbersome that if I update an entity then all other middlewares which don't have anything to do with this entity will still be run.

I'm new to Prisma so I'm not sure if there's already some ways to do it (elegantly). If so, it would be awesome to have some examples for Prisma beginners like me to learn. Thanks!

@thebiglabasky
Copy link

Thanks for the additional details 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/feature A request for a new feature. team/client Issue for team Client. topic: extend-client Extending the Prisma Client
Projects
None yet
Development

No branches or pull requests

5 participants