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
What’s the difference between remove and delete?
提问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 remove
and delete
methods in TypeORM?
TypeORM 中的remove
和delete
方法有什么区别?
回答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
remove
if it contains an array of Entities.While you should use
delete
if you know the condition.
这意味着您应该使用
remove
它是否包含实体数组。
delete
如果您知道条件,则应该使用。
Additionally, as @Jamesstated in commentEntity Listener
such as @BeforeRemove
and @AfterRemove
listeners only triggered when the entity is removed using repository.remove
.
此外,正如@ James在评论中所述,Entity Listener
例如@BeforeRemove
和@AfterRemove
listeners 仅在使用repository.remove
.
Similarly, @BeforeInsert
, @AfterInsert
, @BeforeUpdate
, @AfterUpdate
only triggered when the entity is inserted/updated using repository.save
.
同样,@BeforeInsert
,@AfterInsert
,@BeforeUpdate
,@AfterUpdate
只有当实体插入/使用更新的触发repository.save
。
Source: Entity Listeners and Subscribers
来源:实体侦听器和订阅者