Get a Quote Right Now

Edit Template

Relations in SQL & Typeorm

Relations in SQL:

In the context of databases, a relation refers to the logical association between tables based on common columns. Relations are used to establish connections between tables, enabling you to retrieve and manipulate related data efficiently.

There are several types of relationships in SQL:

  1. One-to-One (1:1) Relationship: In a one-to-one relationship, each record in one table is associated with exactly one record in another table, and vice versa. This relationship is less common in databases due to the potential for data redundancy.Example: Consider a Person table and an Address table. Each person has one corresponding address, and each address belongs to a single person.
  2. One-to-Many (1:N) Relationship: In a one-to-many relationship, each record in one table can be associated with multiple records in another table, but each record in the second table is associated with only one record in the first table.Example: An Author table and a Book table. An author can have multiple books, but each book is written by a single author.
  3. Many-to-Many (N:N) Relationship: In a many-to-many relationship, multiple records in one table can be associated with multiple records in another table. This type of relationship requires a junction table to store the associations.Example: A Student table and a Course table. A student can enroll in multiple courses, and a course can have multiple students.

TypeORM:

TypeORM is an Object-Relational Mapping (ORM) library for TypeScript and JavaScript that simplifies database interactions by allowing you to work with database tables as classes and objects. It provides a high-level abstraction over SQL databases, enabling developers to focus on their application’s logic rather than dealing with raw SQL queries.

In TypeORM, relationships are defined using decorators and configuration within your entity classes. Let’s go through examples for each type of relationship using TypeORM:

One-to-One Relationship:

import { Entity, PrimaryGeneratedColumn, Column, OneToOne, JoinColumn } from 'typeorm';
import { Address } from './Address';

@Entity()
export class Person {
@PrimaryGeneratedColumn()
id: number;

@Column()
name: string;

@OneToOne(() => Address)
@JoinColumn()
address: Address;
}

One-to-Many Relationship:

import { Entity, PrimaryGeneratedColumn, Column, OneToMany } from 'typeorm';
import { Book } from './Book';

@Entity()
export class Author {
@PrimaryGeneratedColumn()
id: number
;

@Column()
name: string;

@OneToMany(() => Book, book => book.author)
books: Book[];
}

Many-to-Many Relationship:

import { Entity, PrimaryGeneratedColumn, Column, ManyToMany, JoinTable } from 'typeorm';
import { Course } from './Course';

@Entity()
export class Student {
@PrimaryGeneratedColumn()
id: number;

@Column()
name: string;

@ManyToMany(() => Course)
@JoinTable()
courses: Course[];
}

In these examples, we’ve used TypeORM decorators like @OneToOne, @OneToMany, and @ManyToMany to define relationships between entities. The @JoinColumn and @JoinTable decorators are used to specify how the join should occur in the database.

Remember that TypeORM also supports additional options for relationship configuration, such as eager loading, cascading, and more.

These are just basic examples to illustrate the concepts. In a real-world scenario, you would also need to configure your database connection and set up entities properly before using them.

One-to-One Relationship:

Person Table:

idnameaddress_id
1John1
2Jane2

Address Table:

idstreetcity
1123 Main StAnytown
2456 Elm StBig City

One-to-Many Relationship:

Author Table:

idname
1Mark
2Emily

Book Table:

idtitleauthor_id
1Book A1
2Book B1
3Book C2

Many-to-Many Relationship:

Student Table:

idname
1Alice
2Bob

Course Table:

idname
1Math
2Science
3History

Student_Course Table (Junction Table):

student_idcourse_id
11
12
22
23

This table representation showcases how the data would be stored in the database tables for each type of relationship. Keep in mind that these examples are simplified for illustration purposes, and real-world scenarios might involve more complex relationships and additional columns.

Share

Leave a Reply

Your email address will not be published. Required fields are marked *