In TypeORM, to retrieve nested entities (i.e., entities related to another entity), you typically use relationship options such as relations or leftJoinAndSelect, depending on your specific query method. The following examples illustrate how to retrieve nested entities:
1. Including Relationships When Using the find Method
When using find or findOne methods, you can specify the relationships to load using the relations property:
javascriptimport { getRepository } from 'typeorm'; import { User } from './entity/User'; import { Profile } from './entity/Profile'; // Assuming the User entity has a related property 'profile' to the Profile entity const users = await getRepository(User).find({ relations: ['profile'], });
In this example, each User entity will have its Profile entity loaded.
2. Using QueryBuilder for More Complex Queries
When you need finer control over the query, you can use createQueryBuilder. This allows you to specify left joins, inner joins, etc., and selectively load fields:
javascriptimport { createQueryBuilder } from 'typeorm'; import { User } from './entity/User'; import { Profile } from './entity/Profile'; const users = await createQueryBuilder(User, 'user') .leftJoinAndSelect('user.profile', 'profile') .getMany();
In this example, we specify a left join to associate the user's profile with each user and select these entities. The leftJoinAndSelect method automatically selects the related entities, so Profile will be returned as part of the result.
3. Deeply Nested Relationships
If you have deeply nested relationships, such as User -> Profile -> Address, you can do the following:
javascriptimport { getRepository } from 'typeorm'; import { User } from './entity/User'; const users = await getRepository(User).find({ relations: ['profile', 'profile.address'], // Note the representation for nested relationships });
Or using QueryBuilder:
javascriptimport { createQueryBuilder } from 'typeorm'; import { User } from './entity/User'; const users = await createQueryBuilder(User, 'user') .leftJoinAndSelect('user.profile', 'profile') .leftJoinAndSelect('profile.address', 'address') .getMany();
This will load users and their profiles, along with the addresses associated with each profile.
4. Using find* Methods with eager Relationships
TypeORM allows you to automatically load related entities by setting eager: true in the entity definition:
javascript@Entity() export class User { // ... @OneToOne(() => Profile, profile => profile.user, { eager: true }) profile: Profile; }
After this configuration, whenever you load a User entity, the Profile entity will be automatically loaded, even if you don't explicitly specify the relations option.
Note
Note: Eagerly retrieving nested entities may have adverse effects on performance, especially when dealing with numerous relationships or deep nesting. You should choose the most appropriate loading strategy based on your specific application context.
Overall, TypeORM provides powerful tools for handling entity relationships and allows you to flexibly load them as needed. By the examples above, you can adjust your queries based on your specific business requirements to retrieve nested entities.