Stop 08 · ORMs & Query BuildersTopics · 4/10

TypeORM

Part of the Backend Developer Roadmap

In short

If you like classes and decorators, TypeORM will feel natural. You define tables as TypeScript classes, and it has an official NestJS integration.

A TypeScript-first ORM using decorators and classes to define entities — commonly paired with NestJS, appealing to developers who like an object-oriented, class-based data layer.

The blob sticks coloured tags onto a row of spice jars on a pantry shelf.

Think of it like this

Labels on every jar

In a well-run kitchen, every jar has a label: 'Haldi, 200 g, use by March'. Anyone can read the shelf and know what's what. TypeORM decorators are those labels: @Entity, @Column and @ManyToOne on your classes tell the ORM how each field maps to the database.

Watch it happen

From class to query

Entity
DataSource
Migration
Repository

1/4Write a User class and decorate it with @Entity and @Column.

The big ideas

What you need to know

Decorators describe columns

@Entity marks a class as a table. @Column, @PrimaryGeneratedColumn and friends describe each field.

Relations as properties

@OneToMany and @ManyToOne turn foreign keys into object properties you can load.

Repository and QueryBuilder

Use repository methods for simple queries, and createQueryBuilder when you need more control.

Many databases

Works with Postgres, MySQL, SQLite, SQL Server and more, and even has a MongoDB mode.

See it in code

Walk through the code

ts
1import { Entity, PrimaryGeneratedColumn, Column, OneToMany, ManyToOne } from "typeorm";2 3@Entity()4export class User {5  @PrimaryGeneratedColumn() id!: number;6  @Column({ unique: true }) email!: string;7  @OneToMany(() => Post, (post) => post.author) posts!: Post[];8}9 10@Entity()11export class Post {12  @PrimaryGeneratedColumn() id!: number;13  @Column() title!: string;14  @ManyToOne(() => User, (user) => user.posts) author!: User;15}16const users = await dataSource.getRepository(User).find({ relations: { posts: true } });

1/4@Entity makes User a table. Each decorated property becomes a column.

Decorators need experimentalDecorators and emitDecoratorMetadata in tsconfig, plus the reflect-metadata package.

Save yourself the pain

Common mistakes

Oops

Leaving synchronize: true on in production.

Do this instead

It alters tables to match your entities on every start and can drop data. Use generated migrations instead.

Oops

Forgetting relations in find and wondering why user.posts is undefined.

Do this instead

Relations aren't loaded unless you ask, with relations: { posts: true } or a join in QueryBuilder.

Your mission

TypeORM in half an hour

~30 min

Quick check

In the code above, which table gets the authorId column?

Go deeper

  1. 1Define an entity using TypeORM's decorator syntax.
  2. 2Compare the decorator-based approach to Prisma's schema-file approach.
  3. 3Notice how TypeORM integrates naturally if you're already using NestJS.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge