How to manage relationships in relational databases with TypeORM?
In the world of relational databases, it is crucial to understand how to manage the different relationships that can occur between tables, such as one-to-one, one-to-many, and many-to-many. In this class we will explore how TypeORM can be used to manage these relationships, focusing on the one-to-one relationship, specifically between a user and a customer.
How is the one-to-one relationship established?
We are going to create the necessary entities, following good practices in structuring them:
-
Customer
entity:
- Primary key
(ID
).
- Attributes such as
name
, lastName
, and phone
, all of Varchar
type.
- Timestamps for
created_at
and updated_at
.
-
Entity User
:
- Primary key
(ID
).
- Attributes such as
email
, password
(encrypted in future implementations), and role
.
- Timestamps for
created_at
and updated_at
.
Both entities must be declared in the User Module
, where they will be configured as entities managed by TypeORM.
How to create the one-to-one relationship?
To implement the one-to-one relationship, two TypeORM decorators must be imported and used: @OneToOne
and @JoinColumn
. Our relationship, in this scenario, will be optional, allowing some users to have no associated clients (administrative users, for example).
@OneToOne(() => CustomerEntity, { nullable: true })@JoinColumn()customer: CustomerEntity;
We use @OneToOne
to declare the relationship and @JoinColumn
exclusively on one of the entities, designating the entity "owner" of the relationship.
How to handle bidirectional relationships?
TypeORM supports bidirectional relationships, which means that any entity can know its associated counterpart:
@OneToOne(() => UserEntity, user => user.customer, { nullable: true })user: UserEntity;
Here we specify the @OneToOne
decorator with a row function
, resolving the relationship from both entities. However, @JoinColumn
is only specified on one side, in this case on User
, to establish who has the relationship in the database.
How to create and execute migrations?
To establish our entities in the database, it is essential to generate and execute migrations:
-
Create migration:
npm run typeorm migration:generate -- create-user-and-customer
-
Execute migration:
npm run typeorm migration:run
These actions translate into the physical creation of the tables and relationships in the database. You can verify the execution by accessing pgAdmin
and reviewing the generated schemas.
How to check table structures from the terminal?
You can inspect the structures and relationships created by TypeORM by connecting directly to the PostgreSQL container:
docker-compose exec postgres-container bash
psql -h localhost -d mydb -U root
\d+ users // Query table details users
This methodology allows you to verify the structure of the tables and their foreign keys in detail.
Dare to implement this configuration in your projects, experimenting with the powerful features TypeORM offers you to manage complex relationships in your databases. Keep exploring and learning new tools to take your development skills to the next level!
Want to see more contributions, questions and answers from the community?