Can't figure out how to update embedded entity in typeorn

こ雲淡風輕ζ 提交于 2020-06-23 14:07:12

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!