【问题标题】:Can you 'filter' the columns from a second leftJoinAndMapOne?您可以从第二个 leftJoinAndMapOne 中“过滤”列吗?
【发布时间】:2022-01-19 05:12:36
【问题描述】:

第二个leftJoinAndMapOne() 的列是否可以被“过滤”,以便它只返回我在对象内需要的列或值?

查询生成器:

    const favorites = await this.favoritesRepo.createQueryBuilder('favorite')
      .where('favorite.user_id = :token', { token })
      .leftJoinAndMapOne('favorite.item', 'favorite.item_id', 'items')
      .select(['favorite.id'])
      .addSelect([
        'items.title',
        'items.description',
        'items.price',
        'items.stock',
      ])
      .leftJoinAndMapOne(
        'items.photos',
        PhotosEntity,
        'photos',
        'favorite.item_id = photos.subject_id and photos.subject_type = :item',
        {item: 'item'}
      )


    return paginate<Favorite>(favorites, options)

输出(GET):

    //...
    {
      "id": 32,
      "item": {
        "title": "Foo",
        "description": "Buzz",
        "price": 250.99,
        "stock": 10,
        "photos": {
          "id": 2,
          "createdAt": "2021-12-04T04:55:02.408Z",
          "url": "https://images.unsplash.com/photo-1633114128729-0a8dc13406b9?ixlib=rb-1.2.1&ixid=MnwxMjA3fDF8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=870&q=80",
          "width": 324,
          "height": 281,
          "size": 42,
          "entityId": 18,
          "entityType": "item",
          "redirectUrl": "https://facebook.com/"
        }
      }
    },
    //...

预期输出:

//...
        {
      "id": 32,
      "item": {
        "title": "Foo",
        "description": "Buzz",
        "price": 250.99,
        "stock": 10,
        "photos": {
          "url": "https://images.unsplash.com/photo-1633114128729-0a8dc13406b9?ixlib=rb-1.2.1&ixid=MnwxMjA3fDF8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=870&q=80"
        }
      }
    },
//...

为了使输出符合预期,我应该提出任何建议或更改? ????

      .leftJoinAndMapOne(
        'items.image',
        PhotosEntity,
        'photos',
        'favorite.item_id = photos.subject_id and photos.subject_type = :item',
        {item: 'item'}
      )
      .addSelect('photos.url')

我尝试使用.select ().addSelect (),但它不会过滤来自photos 的列。

【问题讨论】:

    标签: nestjs typeorm query-builder


    【解决方案1】:

    我已经解决了

    查询生成器:

        const favorites = await this.favoritesRepo.createQueryBuilder('favorite')
          .where('favorite.user_id = :token', { token })
          .leftJoinAndMapOne('favorite.item', 'favorite.item_id', 'items')
          .leftJoinAndMapOne(
            'items.photos',
            PhotosEntity,
            'photos',
            'favorite.item_id = photos.subject_id and photos.subject_type = :item',
            {item: 'item'}
          )
          .select(['favorite.id'])
          .addSelect([
            'items.title',
            'items.description',
            'items.price',
            'items.stock',
            'photos.url',
          ])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-14
      • 2011-01-22
      • 2012-08-07
      • 2013-07-13
      • 1970-01-01
      相关资源
      最近更新 更多