github.com/typeorm/typeorm/blob/master/docs/eager-and-lazy-relations.md
- eager
별다로 relationship을 설정하지 않아도 eager를 설정하면 자동으로 relationship을 불러옵니다.
@ManyToMany(type => Category, category => category.questions, {
eager: true
})
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 can only be used on one side of the relationship, using eager: true on both sides of relationship is disallowed.
find* (즉, find, findAll, findOne...)에서 자동으로 relationship을 불러온다고 알아두면 됩니다.
- lazy
Promise로 반환하면, 자동으로 lazy relationship이 됩니다.
lazy를 불러올 때는 Promise.resolve를 하던가, await로 불러오면 됩니다.
@ManyToMany(type => Question, question => question.categories)
questions: Promise<Question[]>;
categories is a Promise. It means it is lazy and it can store only a promise with a value inside. Example how to save such relation:
const category1 = new Category();
category1.name = "animals";
await connection.manager.save(category1);
const category2 = new Category();
category2.name = "zoo";
await connection.manager.save(category2);
const question = new Question();
question.categories = Promise.resolve([category1, category2]); // resolve
const question = await connection.getRepository(Question).findOne(1);
const categories = await question.categories;
Note: if you came from other languages (Java, PHP, etc.) and are used to use lazy relations everywhere - be careful. Those languages aren't asynchronous and lazy loading is achieved different way, that's why you don't work with promises there. In JavaScript and Node.JS you have to use promises if you want to have lazy-loaded relations. This is non-standard technique and considered experimental in TypeORM.
'DB, ORM > 🧊 typeORM' 카테고리의 다른 글
typeorm과 raw query를 조합하는 방법. (0) | 2021.03.25 |
---|---|
typeORM 배포 끝났으면 Migration 하셔야죠? (0) | 2021.03.16 |
Active Record 패턴 vs Data Mapper 패턴 (0) | 2020.11.10 |
조인 없이 관계 테이블의 fk 조회하기 + @RelationId (0) | 2020.11.10 |
graphql (to) typeORM Entity (to) resolvers.ts (0) | 2020.10.27 |