Creating relationship data in TypeORM involves several steps. First, define entities (Entity) and their relationships (Relationship), then use repositories (Repository) or entity managers (EntityManager) to create and manage relationship data. Below, I'll explain the process and provide some code examples.
Defining Entities and Relationships
Here are examples of two entity definitions, a User and a Photo, defining a one-to-many relationship:
typescriptimport { Entity, PrimaryGeneratedColumn, Column, OneToMany, ManyToOne } from 'typeorm'; @Entity() export class User { @PrimaryGeneratedColumn() id: number; @Column() name: string; @OneToMany(() => Photo, photo => photo.user) photos: Photo[]; } @Entity() export class Photo { @PrimaryGeneratedColumn() id: number; @Column() url: string; @ManyToOne(() => User, user => user.photos) user: User; }
In this example, the User entity has a photos property, which is an array of Photo entities, defined using the @OneToMany decorator. Similarly, the Photo entity has a user property defined using the @ManyToOne decorator.
Creating Relationship Data
When creating relationship data, there are two common approaches: setting the relationship when creating a new entity, or establishing the relationship between existing entities.
Setting the Relationship When Creating an Entity
When creating a new Photo entity and wanting to directly associate it with a User, you can do the following:
typescriptimport { getRepository } from 'typeorm'; import { User, Photo } from './entities'; async function createPhotoForUser(userId: number, photoUrl: string) { const userRepository = getRepository(User); const photoRepository = getRepository(Photo); const user = await userRepository.findOne(userId); if (user) { const photo = new Photo(); photo.url = photoUrl; photo.user = user; // Establishing the relationship await photoRepository.save(photo); console.log('Photo saved with user relationship!'); } } // Call the function to create data with association createPhotoForUser(1, 'http://example.com/photo.jpg');
Establishing the Relationship Between Existing Entities
If you already have two independent entities and want to establish or update their relationship, you can do the following:
typescriptimport { getRepository } from 'typeorm'; import { User, Photo } from './entities'; async function addPhotoToUser(userId: number, photoId: number) { const userRepository = getRepository(User); const photoRepository = getRepository(Photo); const user = await userRepository.findOne(userId); const photo = await photoRepository.findOne(photoId); if (user && photo) { photo.user = user; // Updating the relationship await photoRepository.save(photo); console.log('Photo relationship updated with user!'); } } // Call the function to update the relationship addPhotoToUser(1, 2);
In both cases, the relationship is created and managed by modifying the entity's properties and persisting it to the database using the save method. However, in practice, you may need to handle various exception cases and data validation; the code provided is simplified.
This approach enables you to create and manage various complex relationships, including one-to-one, one-to-many, many-to-one, and many-to-many. When defining relationships, TypeORM offers rich decorators to assist in defining these relationships.