DEV Community

Discussion on: TypeORM Tips (Part 1: Don't use save())

Collapse
 
rishit profile image
Rishit Bansal

Hi, thank you for pointing this out!
I definitely agree save() has its use cases, and Entity Listeners and Subscribers are great! However, I also feel that for users that do not use these features, and for performance critical applications where tables have millions of rows, it is sometimes more efficient to execute an insert() or update() directly.

If you don't mind, I would like to include this information (about entity listeners and subscribers) as a note in the article, with credit to you. Let me know if this is okay!

Collapse
 
ramirezsandin profile image
Jorge

Sure, include it

Collapse
 
scr4bble profile image
Peter Hostačný

Hi guys, @rishit thanks for the article.
@ramirezsandin & @rishit: what would you suggest if we want to retain the performance of executing the simple query (INSERT/UPDATE) by executing insert()/update() methods but also want to use Entity Listeners and Subscribers? Do you by any chance know about a workaround?

We have milions of rows and are syncing the database with external systems so updates/inserts are happening pretty often. We are doing the evaluation "whether to update the entity or not" inside our code so we have access to the old record in the time of evaluation but then if we call update() method, the event listener does not (obviously) have the access to it. I would like to solve it by providing the old record to the listener somehow but avoid using the save() function to refetch the same record we already have.

Thread Thread
 
ramirezsandin profile image
Jorge

If you don't want to use save method of typeorm, then I would suggest to write listeners/subscribers at database level, creating triggers, stored procedures, etc.