I have a Company class with the following definition:
export class Company extends BaseEntity {
@PrimaryGeneratedColumn()
@Field((type) => ID) // needed to overwrite the number typ
id: number;
@Column()
name: string;
@Column()
domain: string;
@OneToMany(() => User, (user) => user.company, {
onDelete: 'SET NULL',
})
users: User[];
@OneToMany(() => Deal, (deal) => deal.company)
deals: Deal[];
}
and when updating a company, the update company dto would contain both a list of dealIds as well as userIds to add to the existing relations. Here is my update method:
async update(id: number, updateCompanyInput: UpdateCompanyInput) {
console.log(`This action updates a #${id} company`);
const queryRunner = this.connection.createQueryRunner();
await queryRunner.connect();
const company = await queryRunner.manager.preload(Company, {
id,
...updateCompanyInput,
});
await queryRunner.manager
.createQueryBuilder()
.relation(Company, 'users')
.of(id)
.add(updateCompanyInput.userIds);
await queryRunner.manager
.createQueryBuilder()
.relation(Company, 'deals')
.of(id)
.add(updateCompanyInput.dealIds);
const result = this.companyRepository.save(company);
await queryRunner.release();
return result;
}
I am wondering:
- Do I need to use a transaction (https://orkhan.gitbook.io/typeorm/docs/transactions)?
- Do I need to call
queryRunner.release()afterthis.companyRepository.saveor the order can be more flexible?