JetBrains / Exposed

Kotlin SQL Framework
http://jetbrains.github.io/Exposed/
Apache License 2.0
8.27k stars 689 forks source link

EntityHooks are not invoked when using DSL API #1706

Open Nek-12 opened 1 year ago

Nek-12 commented 1 year ago

I'm using an EntityHook to sign up for changes in the database. When an entry is deleted, I want to create an entry in another table. My problem is that when I'm using a Table.deleteWhere() clause, EntityHook is not invoked. The only place where that entity hook is invoked, judging by the source code, is when the delete() method of the entity is called (Entity.kt line 249)

I want to listen to changes on any operation on a given table. How can I do this without using database triggers?

MJavoso commented 2 days ago

I could resolve this issue by creating special functions, but I guess this is not what you want, since EntityHook and StatementInterceptors have separated mechanics.

What I did was creating special functions that only should be used when making a CRUD operation, else, a normal transaction block would be used for selecting values. These functions had a lambda parameter called onDatabaseChanged, and, with the use of flows, emit the changes from the readers. It is quite complex so if you want to see the sample code just reply.