nestjs get relation id without join

◾️ we can not get reation id info without join

  • sample restaurant entity

@InputType('RestaurantInputType', { isAbstract: true })
@ObjectType()
@Entity()
export class Restaurant extends CoreEntity {
  @Field((type) => String)
  @Column()
  @IsString()
  @Length(5)
  name: string;

  @Field((type) => User)
  @ManyToOne((type) => User, (user) => user.restaurants, {
    onDelete: 'CASCADE',
  })
  owner: User;
}

◾️ method1 : set [loadRelationIds:true] options without join table when search

const restaurant = await this.restaurants.findOne(
        editRestaurantInput.restaurantId,
        { loadRelationIds: true },
      );
query: SELECT "Restaurant"."id" AS "Restaurant_id", "Restaurant"."creeatedAt" AS "Restaurant_creeatedAt", "Restaurant"."updatedAt" AS "Restaurant_updatedAt", "Restaurant"."name" AS "Restaurant_name", "Restaurant"."coverImage" AS "Restaurant_coverImage", "Restaurant"."address" AS "Restaurant_address", "Restaurant"."categoryId" AS "Restaurant_categoryId", "Restaurant"."ownerId" AS "Restaurant_ownerId" FROM "restaurant" "Restaurant" WHERE "Restaurant"."id" IN ($1) -- PARAMETERS: [2]
edit restaurant target: Restaurant {
  id: 2,
  creeatedAt: 2021-01-18T07:27:35.286Z,
  updatedAt: 2021-01-18T07:27:35.286Z,
  name: 'LA Pizza',
  coverImage: 'http://',
  address: 'Seoul itaewon',
  category: 1,
  owner: 8
}

◾️ method2 : declare @RelationId annotation in restaurant entity(restaurant.entity)

@RelationId((restaurant: Restaurant) => restaurant.owner)
   ownerId: number;
query: SELECT "Restaurant"."id" AS "Restaurant_id", "Restaurant"."creeatedAt" AS "Restaurant_creeatedAt", "Restaurant"."updatedAt" AS "Restaurant_updatedAt", "Restaurant"."name" AS "Restaurant_name", "Restaurant"."coverImage" AS "Restaurant_coverImage", "Restaurant"."address" AS "Restaurant_address", "Restaurant"."categoryId" AS "Restaurant_categoryId", "Restaurant"."ownerId" AS "Restaurant_ownerId" FROM "restaurant" "Restaurant" WHERE "Restaurant"."id" IN ($1) -- PARAMETERS: [2]
edit restaurant target: Restaurant {
  id: 2,
  creeatedAt: 2021-01-18T07:27:35.286Z,
  updatedAt: 2021-01-18T07:27:35.286Z,
  name: 'LA Pizza',
  coverImage: 'http://',
  address: 'Seoul itaewon',
  ownerId: 8
}

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です