This document covers the integration of TypeORM with NestJS applications, focusing on the @nestjs/typeorm package and its configuration patterns for both SQL and NoSQL databases. TypeORM serves as NestJS's primary Object-Relational Mapping (ORM) solution, providing support for multiple database systems including MySQL, PostgreSQL, SQLite, and MongoDB.
For other database integration approaches, see Sequelize Integration for SQL-specific ORM patterns and Mongoose Integration for MongoDB-specific ODM patterns.
The NestJS TypeORM integration follows a modular architecture where the TypeOrmModule provides database connection management and entity repository injection into the dependency injection system.
The following diagram bridges the natural language concept of "Repository Pattern" to the specific classes and functions used in the NestJS TypeORM integration.
Sources: sample/05-sql-typeorm/src/users/users.service.ts1-25 sample/05-sql-typeorm/src/users/users.service.spec.ts2-5
Sources: sample/05-sql-typeorm/src/app.module.ts1-20 sample/05-sql-typeorm/src/users/users.module.ts1-15 sample/05-sql-typeorm/package.json21-30 sample/13-mongo-typeorm/package.json21-30
NestJS TypeORM integration supports multiple database configurations through the TypeOrmModule.forRoot() and TypeOrmModule.forRootAsync() methods.
SQL integrations typically require a driver like mysql2 and configuration for host, port, and credentials.
Sources: sample/05-sql-typeorm/src/app.module.ts8-18 sample/05-sql-typeorm/package.json26-30
The MongoDB integration uses the mongodb driver. While it uses the same TypeOrmModule, the configuration properties shift to URI-based or MongoDB-specific options.
Sources: sample/13-mongo-typeorm/src/app.module.ts1-20 sample/13-mongo-typeorm/package.json21-31
Entities are defined using decorators like @Entity(), @PrimaryGeneratedColumn(), and @Column().
This diagram maps the code entities used in a typical TypeORM setup to their functional roles.
Sources: sample/05-sql-typeorm/src/users/user.entity.ts1-15
The @InjectRepository() decorator is used to inject the Repository instance for a specific entity into a service.
Sources: sample/05-sql-typeorm/src/users/users.service.ts10-14
TypeORM modules support both synchronous and asynchronous registration.
Feature modules use TypeOrmModule.forFeature() to register entities within the current scope. This makes the repositories available for injection in the module's providers.
Sources: sample/05-sql-typeorm/src/users/users.module.ts1-12
Testing TypeORM integrations involves mocking the repository or using a specialized testing module. The getRepositoryToken() function is critical for providing mock implementations in unit tests.
In unit tests, the repository is often replaced with a mock object using the token generated by getRepositoryToken(Entity).
Sources: sample/05-sql-typeorm/src/users/users.service.spec.ts28-42
The TypeORM integration requires specific package configurations depending on the database.
| Package | Version | Purpose |
|---|---|---|
@nestjs/typeorm | 11.0.3 | NestJS TypeORM integration |
typeorm | 1.1.0 | Core TypeORM library |
mysql2 | 3.23.1 | MySQL database driver |
Sources: sample/05-sql-typeorm/package.json25-30
| Package | Version | Purpose |
|---|---|---|
@nestjs/typeorm | 11.0.3 | NestJS TypeORM integration |
typeorm | 1.1.0 | Core TypeORM library |
mongodb | 7.5.0 | MongoDB database driver |
Refresh this wiki