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
typeorminpackage.json- Classes decorated with
@Entity,@Column,@ManyToOneand@OneToMany new DataSource({...})orTypeOrmModule.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
upanddown. - 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
- The
DataSourceconfig, especiallysynchronizeand entity paths - Entity classes
- Repository and query builder usage
How a request moves through the code
- The
DataSourceinitialises at startup and loads entities. - Code gets a repository and calls
find,saveor a query builder. - Relations load eagerly or lazily depending on entity options.
- Subscribers 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
synchronizeenabled anywhere outside development?
What trips people up
synchronize: truechanges the database schema at startup and can drop data; production should use migrations.- Eager relations load on every
find, which can hide performance problems.