问题
I have a category entity CategoryEntity
and each such a category can have subcategories (one level depth), the corresponding entity is called SubcategoryEntity
, the relation between the entities is OneToMany
.
A user needs to be able to create subcategories in all the available categories. As far as I understand, I ought to learn how to update category's subcategories.
This is my try, but obviously it does not do what I'd like it to...
public async createSubcategoryByUserId(userId: number, createSubcategoryDto: CreateSubcategoryDto): Promise<Subcategory> {
const category = await this.categoryRepository.createQueryBuilder('category')
.where('category.user.id = :userId', {userId})
.andWhere('category.id = :categoryId', {categoryId: createSubcategoryDto.categoryId})
.getOne();
const subcategory = new SubCategoryEntity();
subcategory.setName(createSubcategoryDto.name)
.setTemplateId(Number(createSubcategoryDto.categoryId));
await this.subcategoryRepository.save(subcategory);
const newCategory = new CategoryEntity();
Object.assign(newCategory, category);
newCategory.subcategories = [...await this.subcategoryRepository.find(), subcategory];
await this.categoryRepository.save(newCategory);
return new Subcategory().deserialize(subcategory);
}
Namely, if I start creating subcategories for a certain category, it's fine but then when I try to create a subcategory for another category, it sort of moves all the existing subcategories into that new category and it's not what I'm trying to achieve. And I understand this behaviour appears because I rewrite the record instead of updating it.
Perhaps because of this line await this.subcategoryRepository.find()
How to update it correctly?
I'm still not too experienced using typeorm, I made a few attempts and got errors. I would greatly appreciate you help.
EDIT:
I've rewritten it the following way:
public async createSubcategoryByUserId(userId: number, createSubcategoryDto: CreateSubcategoryDto): Promise<Subcategory> {
const category = await this.categoryRepository.createQueryBuilder('category')
.where('category.user.id = :userId', {userId})
.andWhere('category.id = :categoryId', {categoryId: createSubcategoryDto.categoryId})
.getOne();
const subcategory = new SubCategoryEntity();
subcategory.setName(createSubcategoryDto.name)
.setTemplateId(Number(createSubcategoryDto.categoryId));
await this.subcategoryRepository.save(subcategory);
const newCategory = new CategoryEntity();
Object.assign(newCategory, category);
const subcategories = (await this.categoryRepository.createQueryBuilder('category')
.where('category.user.id = :userId', {userId})
.andWhere('category.id = :categoryId', {categoryId: createSubcategoryDto.categoryId})
.leftJoinAndSelect('category.subcategories', 's')
.getOne()).subcategories;
newCategory.subcategories = [...subcategories, subcategory];
await this.categoryRepository.save(newCategory);
return new Subcategory().deserialize(subcategory);
But it still looks wrong...even though it seems to work as expected.
来源:https://stackoverflow.com/questions/62282150/cant-figure-out-how-to-update-embedded-entity-in-typeorn