5
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【NestJS/TypeORM】Entity内で { eager: true } は設定するべきではない

Last updated at Posted at 2020-04-19

環境

TLDR

以下、2つの理由で、Entity内で、{ eager: true }は使わないほうが良いと判断しました。

  • 必要のないカラムを取得する場合がある(後述)。

  • Entity内で、{ eager: true }をしている時の挙動について、以下の2つの記載がありますが、現時点の挙動としては後者が正しいようです。

    1. https://github.com/typeorm/typeorm/blob/master/docs/relations.md#relation-options
      eager: boolean - If set to true, the relation will always be loaded with the main entity when using find* methods or QueryBuilder on this entity

    trueに設定した場合、find*メソッド、またはQueryBuilderを使った時は、リレーションは常にメインエンティティとともにロードされます。

    1. https://github.com/typeorm/typeorm/blob/master/docs/eager-and-lazy-relations.md#eager-relations
      Eager relations only work when you use find* methods. If you use QueryBuilder eager relations are disabled and have to use leftJoinAndSelect to load the relation.

    Eager relations は、find*メソッドを使用する場合にのみ機能します。 QueryBuilderを使用する場合、Eager relationsは無効になり、leftJoinAndSelectを使用して関係をロードする必要があります。

検証

以下のような Author has many Books という関連があるとします

@Entity()
export class Work {
  @PrimaryGeneratedColumn()
  readonly id!: number

  @Index()
  @ManyToOne(
    () => Author,
    (author) => author.books
  )
  author!: Author
@Entity()
export class Author {
  @PrimaryGeneratedColumn()
  readonly id!: number

  @OneToMany(
    () => Book,
    (book) => book.author,
    { eager: true, cascade: true }
  )
  books!: Book[]

findを使ったクエリーの結果

  • this.bookRepository.find({ relations: ['author'] })

Bookを上記の条件で検索した時に、関連するAuthorだけではなく、更にbooksもレスポンスに含まれてしまいました({ eager: true }を設定しているため)。
必要に応じて、leftJoinAndSelectを指定して、常に意図した結果を得るようにした方が良さそうです。

[
  {
    "id": 7,
    "name": "What book is this?",
    "author": {
      "id": 6,
      "name": "Who am I",
      "createdAt": "2020-04-18T01:20:26.810Z",
      "updatedAt": "2020-04-18T01:20:26.810Z",
      "books": [
        {
          "id": 7,
          "name": "What book is this?",
        }
      ]
    }
  }
]
5
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?