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

[FEATURE]: Pre & Post Save Signals #755

Open
Kae7in opened this issue Jun 13, 2023 · 6 comments
Open

[FEATURE]: Pre & Post Save Signals #755

Kae7in opened this issue Jun 13, 2023 · 6 comments
Labels
enhancement New feature or request

Comments

@Kae7in
Copy link

Kae7in commented Jun 13, 2023

Hello Drizzle maintainers,

Firstly, I'd like to take a moment to appreciate all the effort that goes into maintaining and improving Drizzle. It's an incredibly valuable resource and it has made development a lot easier for me.

Summary

I would like to suggest a feature for the library: Pre & Post Save Signals, similar to the signals mechanism found in Flask or Django, or the middleware concept in Prisma.

Problem

In the current version of Drizzle, there is no in-built mechanism for triggering actions before or after a database query. Although we can manually code this logic around the Drizzle function calls in our application, it introduces additional complexity, scattered code and it’s also more error-prone. Furthermore, some tasks naturally belong to the ORM layer, such as data validation, logging, or automated reactions to data changes.

I do acknowledge that Drizzle already provides a comprehensive logging feature, which is highly versatile and configurable, allowing us to write logs anywhere desired - disk, API, etc. However, the feature I am proposing is conceptually different. While logging captures and records the state of the system at a given point of time, pre and post save signals would enable automated, direct response to these state changes, beyond just recording them.

Suggested solution

By introducing Pre & Post Save Signals, users of Drizzle would have a way to hook into the lifecycle of a model instance, allowing them to define tasks that should be performed before or after saving a record.

For example, a common use case could be sending a welcome email to a new user after a new row is created in the User table. With the suggested mechanism, one could define a 'post save' signal on the User model that automatically sends an email when a new instance of User is saved to the database.

Benefit to the Drizzle users

Such a feature would enable developers to manage side-effects more effectively by linking them directly to model lifecycle events. This would improve readability, maintainability, and reduce the likelihood of errors as changes in state can be handled at the moment they occur. It provides a clean and intuitive solution to manage a range of common tasks such as sending notifications, logging, data validation, etc.

I hope you'll consider this feature request for inclusion in a future version of Drizzle. I believe it could add a great deal of flexibility and improve the developer experience significantly.

References

@Kae7in Kae7in added the enhancement New feature or request label Jun 13, 2023
@ivanjd
Copy link

ivanjd commented Jul 19, 2023

We need this feature too. We are migrating from TypeORM so something like the TypeORM listeners/subscribers would be ideal:
https://orkhan.gitbook.io/typeorm/docs/listeners-and-subscribers

@ranjan-purbey
Copy link

Related #956 #843

@taivo
Copy link

taivo commented Oct 3, 2023

Would love to have this. Prisma's middleware applies to all requests. Not very good separation of concern and unnecessarily inefficient. It doesn't have to be fully implemented as signals in Django. Simple pre and post hooks that are definable with the model would work too.

@taivo
Copy link

taivo commented Oct 3, 2023

We need this feature too. We are migrating from TypeORM so something like the TypeORM listeners/subscribers would be ideal:
https://orkhan.gitbook.io/typeorm/docs/listeners-and-subscribers

@ivanjd I'm curious what are your reasons for leaving TypeORM?

@2coo
Copy link

2coo commented Dec 12, 2023

The functionality that developers can extend their drizzle client like prisma client extension.
It would improve the developer experience way better.

Scenarios that I have faced:

  • Hashing the password value before saving it into a database.
  • adding a new property to query results at runtime and type-level.
    e.g. to use casl ability it requires to __caslSubjectType__ property.
  • Implement soft delete

@CanRau
Copy link

CanRau commented Dec 14, 2023

From what I understand the team is looking into implementing middleware #1513 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

6 participants