admin管理员组文章数量:1386925
Let's suppose I have an entity:
@OneToMany(
(): typeof UserCarData => UserCarData,
(userCar: UserCarData): User => userCar.user,
{
cascade: [Cascade.ALL],
},
)
public userCars = new Collection<UserCarData>(this);
@OneToMany((): typeof Auth => Auth, (auth: Auth): User => auth.user, {
cascade: [Cascade.ALL],
})
public auth = new Collection<Auth>(this);
@Unique()
@Property({ columnType: 'text' })
public email: string;
When I try to create a new record like this:
const entityToCreate = this.em.create(User, {
...plainToInstance(User, dto),
password: hashedPassword,
accessTokenVersion: 0,
});
I get such object:
{
userCars: Collection<UserCarData> { initialized: true, dirty: false },
auth: Collection<Auth> { initialized: true, dirty: false },
email: '[email protected]',
role: 'car_owner',
password: '$argon2id$v=19$m=65536,t=3,p=4$Voe9E9r7vWQzAM/qY0NUIw$uhiYSCO/V2j3gX3VjA7WXEQ+L3Z7PMfX6eh6AtA9VCY',
accessTokenVersion: 0
}
The problem is that during creational transaction MicroORM makes two additional queries:
update "auth" set "user_id" = '27' where "id" is null
update "user_car_data" set "user_id" = '27' where "id" is null
The question is how to get rid of those queries? Manually set to null collections before creation?
Let's suppose I have an entity:
@OneToMany(
(): typeof UserCarData => UserCarData,
(userCar: UserCarData): User => userCar.user,
{
cascade: [Cascade.ALL],
},
)
public userCars = new Collection<UserCarData>(this);
@OneToMany((): typeof Auth => Auth, (auth: Auth): User => auth.user, {
cascade: [Cascade.ALL],
})
public auth = new Collection<Auth>(this);
@Unique()
@Property({ columnType: 'text' })
public email: string;
When I try to create a new record like this:
const entityToCreate = this.em.create(User, {
...plainToInstance(User, dto),
password: hashedPassword,
accessTokenVersion: 0,
});
I get such object:
{
userCars: Collection<UserCarData> { initialized: true, dirty: false },
auth: Collection<Auth> { initialized: true, dirty: false },
email: '[email protected]',
role: 'car_owner',
password: '$argon2id$v=19$m=65536,t=3,p=4$Voe9E9r7vWQzAM/qY0NUIw$uhiYSCO/V2j3gX3VjA7WXEQ+L3Z7PMfX6eh6AtA9VCY',
accessTokenVersion: 0
}
The problem is that during creational transaction MicroORM makes two additional queries:
update "auth" set "user_id" = '27' where "id" is null
update "user_car_data" set "user_id" = '27' where "id" is null
The question is how to get rid of those queries? Manually set to null collections before creation?
Share Improve this question edited Mar 18 at 21:26 xDD asked Mar 18 at 20:45 xDDxDD 275 bronze badges1 Answer
Reset to default 0Using plainToInstance(User, dto)
causes the problem
本文标签: nestjsMikroORM behaviour of Collection during creatingStack Overflow
版权声明:本文标题:nestjs - MikroORM: behaviour of Collection during creating - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744491955a2608787.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论