Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What’s the difference between remove and delete?

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?

like image 506
Yegor Zaremba Avatar asked Jan 18 '19 01:01

Yegor Zaremba


People also ask

What's the difference between remove app and delete app?

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.

What is the difference between remove from inventory and delete from disk?

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.

What is the difference between remove and delete in Python?

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.

What is the purpose of deleting?

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.


1 Answers

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

like image 119
Mukyuu Avatar answered Sep 23 '22 20:09

Mukyuu