DEV Community

Cover image for TypeORM Tips (Part 1: Don't use save())

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

Rishit Bansal on January 22, 2022

Introduction to the series As of January 21st 2022 (source), TypeORM is the 3rd most popular Javascript ORM library and certainly the mo...
Collapse
 
aminnairi profile image
Amin

As @jorge rightly said, listeners are called by default when using the save method (this can be disabled) whereas the insert method only execute a clean and fast INSERT INTO SQL query.

Also, if you have multiple relationships to create, because a user has many articles to save, insert might not be the best fit because again it uses a clean and simple INSERT INTO SQL query, whereas the save method will actually use a transaction in order to perform a slower but safer query, leading to data not being inserted if ever one of the queries has an error. This can be useful for data integrity where the situation requires it.

Actually, the save and remove options both supports listeners like @BeforeInsert which makes it easy to implements a user entity for instance.

import { BeforeInsert, Column, PrimaryGeneratedColumn } from "typeorm";
import { hash, compare } from "bcryptjs";

@Entity("users")
export class User {
  @PrimaryGeneratedColumn("uuid")
  public id!: string;

  @Column({
    type: "varchar",
    length: 50,
    unique: true,
    nullable: false
  })
  public email!: string;

  @Column({
    type: "varchar",
    length: 60,
    nullable: false
  })
  public password!: string;

  @BeforeInsert()
  public async hashPassword() {
    this.password = await hash(this.password, 10);
  }
}
Enter fullscreen mode Exit fullscreen mode

And you can create a user as simple as that.

const user = this.userRepository.create({
  email: "user@domain.com",
  password: "plainPassword"
});

await userRepository.save(user);

const storedUser = await this.userRepository.findOneBy({
  email: "user@domain.com"
});

console.log(storedUser.password);
// $2b$10$3euPcmQFCib...
Enter fullscreen mode Exit fullscreen mode
Collapse
 
rishit profile image
Rishit Bansal

Hi, thank you for this information, always super good to learn more about type orm.

As I covered in this reply, save() does have its applications (dev.to/rishit/comment/27c1d), and I completely agree with that!

About the point on the transaction, I think the "context" of where you are executing an update mattes a lot here. For example, is the save() is just to update a single row on a table, executing

BEGIN transaction
UPDATE ...
COMMIT
Enter fullscreen mode Exit fullscreen mode

has no difference as compared to

UPDATE...
Enter fullscreen mode Exit fullscreen mode

I think the reason typeorm wraps it in a transaction is because when you use cascade, it may execute more than one UPDATE command. We actually faced an issue on production where this was causing issues as well, until we discovered this issue (github.com/typeorm/typeorm/issues/...), where you need to do some workarounds to make sure the library knows what to update in what context.

Overall, I think the objective of an ORM is maybe to compromise on a bit of performance to make it super easy to use API wise, and save() is a great example of this ideology. Of course this kind of opinionated design will have performance repercussions, which is what I was trying to point to in the article.

Collapse
 
ramirezsandin profile image
Jorge

If you don't use save, then you cannot use Entity Listeners and Subscribers.

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
 
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.

Collapse
 
ramirezsandin profile image
Jorge

Sure, include it

Collapse
 
nxquan profile image
nxquan

I think, the Entity Listener & Subscribers can be trigger by any method insert, update, or save but not for raw sql.

Collapse
 
techtitans0101 profile image
TechTitans0101

Thanks @rishit for the tips.

Open source is essential for application developers. It is unfortunate that Open Base has shut down. While searching for alternate, came across kandi from Open Weaver. It helps developers find code snippets, packages, libraries and solutions from millions of assets.
Thanks to such tools for support to the open source community. Happy coding!

Collapse
 
joaquinangelino profile image
Joaquin Angelino Corona

Nice

Collapse
 
dileepamabulage profile image
Dileepa Mabulage

In the typeorm save method, if we enter the same data that already in the database, wont it update that record, in save method

@Entity()
export class Contact {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  name: string;

  @Column({ unique: true })
  googleId: string;

  @Column()
  email: string;
}
Enter fullscreen mode Exit fullscreen mode

it will throw a unique constraint error, Is there a way to bulk update the same data records in typeorm. Thank you

Collapse
 
alexander6 profile image
Alexander6

nice post

Collapse
 
netojose profile image
José Neto

I had big problems using .save() inside transactions, because after every save, the transactions was COMMITed, and the ROLLBACK not undo commited changes.

Collapse
 
miguelci profile image
Miguel

cool stuff, thanks!

there is a typo on the TypeORM documentation link, as an undefined in there.
link is just typeorm.io/#updating-in-the-database