Skip to content

How to understand a TypeORM codebase

TypeORM maps classes to tables with decorators. Entities describe columns and relations, a `DataSource` holds the connection, and code works through repositories or the query builder. It's common in NestJS projects.

How to tell it's TypeORM

  • typeorm in package.json
  • Classes decorated with @Entity, @Column, @ManyToOne and @OneToMany
  • new DataSource({...}) or TypeOrmModule.forRoot(...)

Where things usually live

src/entities/ or **/*.entity.ts
Entity classes.
src/data-source.ts
Connection settings, entity and migration paths.
src/migrations/
Migration classes with up and down.
src/subscribers/
Entity event subscribers such as afterInsert.
src/repositories/
Custom repositories, if used.

Conventions, not rules. Real projects rename and move things, so confirm against the repository.

Where to start reading

  1. The DataSource config, especially synchronize and entity paths
  2. Entity classes
  3. Repository and query builder usage

How a request moves through the code

  1. 1The DataSource initialises at startup and loads entities.
  2. 2Code gets a repository and calls find, save or a query builder.
  3. 3Relations load eagerly or lazily depending on entity options.
  4. 4Subscribers and entity listeners run on inserts, updates and removes.

Questions to ask about the repository

  • Describe the entities and their relations.
  • Which relations are eager, and could that load too much data?
  • Is synchronize enabled anywhere outside development?

What trips people up

  • synchronize: true changes the database schema at startup and can drop data; production should use migrations.
  • Eager relations load on every find, which can hide performance problems.