Eager and Lazy Relations

Eager relations

Eager relations are loaded automatically each time you load entities from the database.For example:

  1. import {Entity, PrimaryGeneratedColumn, Column, ManyToMany} from "typeorm";
  2. import {Question} from "./Question";
  3. @Entity()
  4. export class Category {
  5. @PrimaryGeneratedColumn()
  6. id: number;
  7. @Column()
  8. name: string;
  9. @ManyToMany(type => Question, question => question.categories)
  10. questions: Question[];
  11. }
  1. import {Entity, PrimaryGeneratedColumn, Column, ManyToMany, JoinTable} from "typeorm";
  2. import {Category} from "./Category";
  3. @Entity()
  4. export class Question {
  5. @PrimaryGeneratedColumn()
  6. id: number;
  7. @Column()
  8. title: string;
  9. @Column()
  10. text: string;
  11. @ManyToMany(type => Category, category => category.questions, {
  12. eager: true
  13. })
  14. @JoinTable()
  15. categories: Category[];
  16. }

Now when you load questions you don’t need to join or specify relations you want to load.They will be loaded automatically:

  1. const questionRepository = connection.getRepository(Question);
  2. // questions will be loaded with its categories
  3. const questions = await questionRepository.find();

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.

Lazy relations

Entities in lazy relations are loaded once you access them.Such relations must have Promise as type - you store your value in a promise,and when you load them promise is returned as well. Example:

  1. import {Entity, PrimaryGeneratedColumn, Column, ManyToMany} from "typeorm";
  2. import {Question} from "./Question";
  3. @Entity()
  4. export class Category {
  5. @PrimaryGeneratedColumn()
  6. id: number;
  7. @Column()
  8. name: string;
  9. @ManyToMany(type => Question, question => question.categories)
  10. questions: Promise<Question[]>;
  11. }
  1. import {Entity, PrimaryGeneratedColumn, Column, ManyToMany, JoinTable} from "typeorm";
  2. import {Category} from "./Category";
  3. @Entity()
  4. export class Question {
  5. @PrimaryGeneratedColumn()
  6. id: number;
  7. @Column()
  8. title: string;
  9. @Column()
  10. text: string;
  11. @ManyToMany(type => Category, category => category.questions)
  12. @JoinTable()
  13. categories: Promise<Category[]>;
  14. }

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:

  1. const category1 = new Category();
  2. category1.name = "animals";
  3. await connection.manager.save(category1);
  4. const category2 = new Category();
  5. category2.name = "zoo";
  6. await connection.manager.save(category2);
  7. const question = new Question();
  8. question.categories = Promise.resolve([category1, category2]);
  9. await connection.manager.save(question);

Example how to load objects inside lazy relations:

  1. const question = await connection.getRepository(Question).findOne(1);
  2. const categories = await question.categories;
  3. // you'll have all question's categories inside "categories" variable now

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.