typescript 删除和删除有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/54246615/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 05:46:04  来源:igfitidea点击:

What’s the difference between remove and delete?

typescripttypeorm

提问by Yegor Zaremba

For example, I have a TypeORM entity Profile:

例如,我有一个 TypeORM 实体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 removeand deletemethods in TypeORM?

TypeORM 中的removedelete方法有什么区别?

回答by Mukyuu

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).
  • remove- 删除给定的实体或实体数组。它在单个事务中删除所有给定的实体(在实体的情况下,管理器不是事务性的)。

Example:

例子:

await repository.remove(user);
await repository.remove([
    category1,
    category2,
    category3
]);
  • delete- Deletes entities by entity id, ids or given conditions:
  • delete- 按实体 id、id 或给定条件删除实体:

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 removeif it contains an array of Entities.

While you should use deleteif you know the condition.

这意味着您应该使用remove它是否包含实体数组。

delete如果您知道条件,则应该使用。



Additionally, as @Jamesstated in commentEntity Listenersuch as @BeforeRemoveand @AfterRemovelisteners only triggered when the entity is removed using repository.remove.

此外,正如@ James评论中所述,Entity Listener例如@BeforeRemove@AfterRemovelisteners 仅在使用repository.remove.

Similarly, @BeforeInsert, @AfterInsert, @BeforeUpdate, @AfterUpdateonly triggered when the entity is inserted/updated using repository.save.

同样,@BeforeInsert@AfterInsert@BeforeUpdate@AfterUpdate只有当实体插入/使用更新的触发repository.save

Source: Entity Listeners and Subscribers

来源:实体侦听器和订阅者