For example, I have a TypeORM entity Profile
:
@Entity() class Profile { @PrimaryGeneratedColumn() id: number; @Column() gender: string; @Column() photo: string; @OneToOne(type => User, { cascade: true }) @JoinColumn() user: User; }
And I’m not sure which one should I use to delete a user profile?
Profile.remove(profile) Profile.delete(profile)
What is the difference between the remove
and delete
methods in TypeORM?
Uninstall is removing a program and its associated files from a computer hard drive. The uninstall feature differs from the delete function in that it safely and efficiently removes all associated files, whereas delete only removes part of a program or selected file.
Removing from inventory is different from deleting from disk; a VM or virtual service that's out of inventory can be placed in inventory at another time, while a VM or virtual service deleted from disk is permanently removed, along with all its associated files.
Remove basically removes the first matching value. Delete deletes the item from a specific index Pop basically takes an index and returns the value at that index. Next time you print the list the value doesnt appear.
Pressing Delete (DEL) also deletes the currently highlighted text, image or group of images. The Delete key removes characters to the right of the cursor, whereas the Backspace key deletes to the left. See Backspace key.
From Repo:
remove
- Removes a given entity or array of entities. It removes all given entities in a single transaction (in the case of entity, manager is not transactional).Example:
await repository.remove(user); await repository.remove([ category1, category2, category3 ]);
delete
- Deletes entities by entity id, ids or given conditions:Example:
await repository.delete(1); await repository.delete([1, 2, 3]); await repository.delete({ firstName: "Timber" });
As stated in example here:
import {getConnection} from "typeorm"; await getConnection() .createQueryBuilder() .delete() .from(User) .where("id = :id", { id: 1 }) .execute();
Which means you should use
remove
if it contains an array of Entities.While you should use
delete
if you know the condition.
Additionally, as @James stated in comment Entity Listener
such as @BeforeRemove
and @AfterRemove
listeners only triggered when the entity is removed using repository.remove
.
Similarly, @BeforeInsert
, @AfterInsert
, @BeforeUpdate
, @AfterUpdate
only triggered when the entity is inserted/updated using repository.save
.
Source: Entity Listeners and Subscribers
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With