Database
Nest is database agnostic, allowing you to easily integrate with any SQL or NoSQL database. You have a number of options available to you, depending on your preferences. At the most general level, connecting Nest to a database is simply a matter of loading an appropriate Node.js driver for the database, just as you would with Express or Fastify.
You can also directly use any general purpose Node.js database integration library or ORM, such as MikroORM (see MikroORM recipe), Sequelize (see Sequelize integration), Knex.js (see Knex.js tutorial), TypeORM, and Prisma (see Prisma recipe), to operate at a higher level of abstraction.
For convenience, Nest provides tight integration with TypeORM and Sequelize out-of-the-box with the @nestjs/typeorm and @nestjs/sequelize packages respectively, which we'll cover in the current chapter, and Mongoose with @nestjs/mongoose, which is covered in this chapter. These integrations provide additional NestJS-specific features, such as model/repository injection, testability, and asynchronous configuration to make accessing your chosen database even easier.
TypeORM Integration
For integrating with SQL and NoSQL databases, Nest provides the @nestjs/typeorm package. TypeORM is the most mature Object Relational Mapper (ORM) available for TypeScript. Since it's written in TypeScript, it integrates well with the Nest framework.
To begin using it, we first install the required dependencies. In this chapter, we'll demonstrate using the popular MySQL Relational DBMS, but TypeORM provides support for many relational databases, such as PostgreSQL, Oracle, Microsoft SQL Server, SQLite, and even NoSQL databases like MongoDB. The procedure we walk through in this chapter will be the same for any database supported by TypeORM. You'll simply need to install the associated client API libraries for your selected database.
Once the installation process is complete, we can import the TypeOrmModule into the root AppModule.
warning Warning Setting
synchronize: trueshouldn't be used in production - otherwise you can lose production data.
The forRoot() method supports all the configuration properties exposed by the DataSource constructor from the TypeORM package. In addition, there are several extra configuration properties described below.
info Hint Learn more about the data source options here.
Once this is done, the TypeORM DataSource and EntityManager objects will be available to inject across the entire project (without needing to import any modules), for example:
Repository pattern
TypeORM supports the repository design pattern, so each entity has its own repository. These repositories can be obtained from the database data source.
To continue the example, we need at least one entity. Let's define the User entity.
info Hint Learn more about entities in the TypeORM documentation.
The User entity file sits in the users directory. This directory contains all files related to the UsersModule. You can decide where to keep your model files, however, we recommend creating them near their domain, in the corresponding module directory.
To begin using the User entity, we need to let TypeORM know about it by inserting it into the entities array in the module forRoot() method options (unless you use a static glob path):
Next, let's look at the UsersModule:
This module uses the forFeature() method to define which repositories are registered in the current scope. With that in place, we can inject the UsersRepository into the UsersService using the @InjectRepository() decorator:
warning Notice Don't forget to import the
UsersModuleinto the rootAppModule.
If you want to use the repository outside of the module which imports TypeOrmModule.forFeature, you'll need to re-export the providers generated by it.
You can do this by exporting the whole module, like this:
Now if we import UsersModule in UserHttpModule, we can use @InjectRepository(User) in the providers of the latter module.
Relations
Relations are associations established between two or more tables. Relations are based on common fields from each table, often involving primary and foreign keys.
There are three types of relations:
To define relations in entities, use the corresponding decorators. For example, to define that each User can have multiple photos, use the @OneToMany() decorator.
info Hint To learn more about relations in TypeORM, visit the TypeORM documentation.
Auto-load entities
Manually adding entities to the entities array of the data source options can be tedious. In addition, referencing entities from the root module breaks application domain boundaries and causes leaking implementation details to other parts of the application. To address this issue, an alternative solution is provided. To automatically load entities, set the autoLoadEntities property of the configuration object (passed into the forRoot() method) to true, as shown below:
With that option specified, every entity registered through the forFeature() method will be automatically added to the entities array of the configuration object.
warning Warning Note that entities that aren't registered through the
forFeature()method, but are only referenced from the entity (via a relationship), won't be included by way of theautoLoadEntitiessetting.
Separating entity definition
You can define an entity and its columns right in the model, using decorators. But some people prefer to define entities and their columns inside separate files using the "entity schemas".
warning error Warning If you provide the
targetoption, thenameoption value has to be the same as the name of the target class. If you do not provide thetargetyou can use any name.
Nest allows you to use an EntitySchema instance wherever an Entity is expected, for example:
TypeORM Transactions
A database transaction symbolizes a unit of work performed within a database management system against a database, and treated in a coherent and reliable way independent of other transactions. A transaction generally represents any change in a database (learn more).
There are many different strategies to handle TypeORM transactions. We recommend using the QueryRunner class because it gives full control over the transaction.
First, we need to inject the DataSource object into a class in the normal way:
info Hint The
DataSourceclass is imported from thetypeormpackage.
Now, we can use this object to create a transaction.
info Hint Note that the
dataSourceis used only to create theQueryRunner. However, to test this class would require mocking the entireDataSourceobject (which exposes several methods). Thus, we recommend using a helper factory class (e.g.,QueryRunnerFactory) and defining an interface with a limited set of methods required to maintain transactions. This technique makes mocking these methods pretty straightforward.
Alternatively, you can use the callback-style approach with the transaction method of the DataSource object (read more).
Subscribers
With TypeORM subscribers, you can listen to specific entity events.
error Warning Event subscribers can not be request-scoped.
Now, add the UserSubscriber class to the providers array:
Migrations
Migrations provide a way to incrementally update the database schema to keep it in sync with the application's data model while preserving existing data in the database. To generate, run, and revert migrations, TypeORM provides a dedicated CLI.
Migration classes are separate from the Nest application source code. Their lifecycle is maintained by the TypeORM CLI. Therefore, you are not able to leverage dependency injection and other Nest specific features with migrations. To learn more about migrations, follow the guide in the TypeORM documentation.
Multiple databases
Some projects require multiple database connections. This can also be achieved with this module. To work with multiple connections, first create the connections. In this case, data source naming becomes mandatory.
Suppose you have an Album entity stored in its own database.
warning Notice If you don't set the
namefor a data source, its name is set todefault. Please note that you shouldn't have multiple connections without a name, or with the same name, otherwise they will get overridden.
warning Notice If you are using
TypeOrmModule.forRootAsync, you have to also set the data source name outsideuseFactory. For example:See this issue for more details.
At this point, you have User and Album entities registered with their own data source. With this setup, you have to tell the TypeOrmModule.forFeature() method and the @InjectRepository() decorator which data source should be used. If you do not pass any data source name, the default data source is used.
You can also inject the DataSource or EntityManager for a given data source:
It's also possible to inject any DataSource to the providers:
Testing
When it comes to unit testing an application, we usually want to avoid making a database connection, keeping our test suites independent and their execution process as fast as possible. But our classes might depend on repositories that are pulled from the data source (connection) instance. How do we handle that? The solution is to create mock repositories. In order to achieve that, we set up custom providers. Each registered repository is automatically represented by an <EntityName>Repository token, where EntityName is the name of your entity class.
The @nestjs/typeorm package exposes the getRepositoryToken() function which returns a prepared token based on a given entity.
Now a substitute mockRepository will be used as the UsersRepository. Whenever any class asks for UsersRepository using an @InjectRepository() decorator, Nest will use the registered mockRepository object.
Async configuration
You may want to pass your repository module options asynchronously instead of statically. In this case, use the forRootAsync() method, which provides several ways to deal with async configuration.
One approach is to use a factory function:
Our factory behaves like any other asynchronous provider (e.g., it can be async and it's able to inject dependencies through inject).
Alternatively, you can use the useClass syntax:
The construction above will instantiate TypeOrmConfigService inside TypeOrmModule and use it to provide an options object by calling createTypeOrmOptions(). Note that this means that the TypeOrmConfigService has to implement the TypeOrmOptionsFactory interface, as shown below:
In order to prevent the creation of TypeOrmConfigService inside TypeOrmModule and use a provider imported from a different module, you can use the useExisting syntax.
This construction works the same as useClass with one critical difference - TypeOrmModule will lookup imported modules to reuse an existing ConfigService instead of instantiating a new one.
info Hint Make sure that the
nameproperty is defined at the same level as theuseFactory,useClass, oruseValueproperty. This will allow Nest to properly register the data source under the appropriate injection token.
Custom DataSource Factory
In conjunction with async configuration using useFactory, useClass, or useExisting, you can optionally specify a dataSourceFactory function which will allow you to provide your own TypeORM data source rather than allowing TypeOrmModule to create the data source.
dataSourceFactory receives the TypeORM DataSourceOptions configured during async configuration using useFactory, useClass, or useExisting and returns a Promise that resolves a TypeORM DataSource.
info Hint The
DataSourceclass is imported from thetypeormpackage.
Example
A working example is available here.
Sequelize Integration
An alternative to using TypeORM is to use the Sequelize ORM with the @nestjs/sequelize package. In addition, we leverage the sequelize-typescript package which provides a set of additional decorators to declaratively define entities.
To begin using it, we first install the required dependencies. In this chapter, we'll demonstrate using the popular MySQL Relational DBMS, but Sequelize provides support for many relational databases, such as PostgreSQL, MySQL, Microsoft SQL Server, SQLite, and MariaDB. The procedure we walk through in this chapter will be the same for any database supported by Sequelize. You'll simply need to install the associated client API libraries for your selected database.
Once the installation process is complete, we can import the SequelizeModule into the root AppModule.
The forRoot() method supports all the configuration properties exposed by the Sequelize constructor (read more). In addition, there are several extra configuration properties described below.
Once this is done, the Sequelize object will be available to inject across the entire project (without needing to import any modules), for example:
Models
Sequelize implements the Active Record pattern. With this pattern, you use model classes directly to interact with the database. To continue the example, we need at least one model. Let's define the User model.
info Hint Learn more about the available decorators here.
The User model file sits in the users directory. This directory contains all files related to the UsersModule. You can decide where to keep your model files, however, we recommend creating them near their domain, in the corresponding module directory.
To begin using the User model, we need to let Sequelize know about it by inserting it into the models array in the module forRoot() method options:
Next, let's look at the UsersModule:
This module uses the forFeature() method to define which models are registered in the current scope. With that in place, we can inject the UserModel into the UsersService using the @InjectModel() decorator:
warning Notice Don't forget to import the
UsersModuleinto the rootAppModule.
If you want to use the model outside of the module which imports SequelizeModule.forFeature, you'll need to re-export the providers generated by it.
You can do this by exporting the whole module, like this:
Now if we import UsersModule in UserHttpModule, we can use @InjectModel(User) in the providers of the latter module.
Relations
Relations are associations established between two or more tables. Relations are based on common fields from each table, often involving primary and foreign keys.
There are three types of relations:
To define relations in models, use the corresponding decorators. For example, to define that each User can have multiple photos, use the @HasMany() decorator.
info Hint To learn more about associations in Sequelize, read this chapter.
Auto-load models
Manually adding models to the models array of the connection options can be tedious. In addition, referencing models from the root module breaks application domain boundaries and causes leaking implementation details to other parts of the application. To solve this issue, automatically load models by setting both autoLoadModels and synchronize properties of the configuration object (passed into the forRoot() method) to true, as shown below:
With that option specified, every model registered through the forFeature() method will be automatically added to the models array of the configuration object.
warning Warning Note that models that aren't registered through the
forFeature()method, but are only referenced from the model (via an association), won't be included.
Sequelize Transactions
A database transaction symbolizes a unit of work performed within a database management system against a database, and treated in a coherent and reliable way independent of other transactions. A transaction generally represents any change in a database (learn more).
There are many different strategies to handle Sequelize transactions. Below is a sample implementation of a managed transaction (auto-callback).
First, we need to inject the Sequelize object into a class in the normal way:
info Hint The
Sequelizeclass is imported from thesequelize-typescriptpackage.
Now, we can use this object to create a transaction.
info Hint Note that the
Sequelizeinstance is used only to start the transaction. However, to test this class would require mocking the entireSequelizeobject (which exposes several methods). Thus, we recommend using a helper factory class (e.g.,TransactionRunner) and defining an interface with a limited set of methods required to maintain transactions. This technique makes mocking these methods pretty straightforward.
Migrations
Migrations provide a way to incrementally update the database schema to keep it in sync with the application's data model while preserving existing data in the database. To generate, run, and revert migrations, Sequelize provides a dedicated CLI.
Migration classes are separate from the Nest application source code. Their lifecycle is maintained by the Sequelize CLI. Therefore, you are not able to leverage dependency injection and other Nest specific features with migrations. To learn more about migrations, follow the guide in the Sequelize documentation.
Multiple databases
Some projects require multiple database connections. This can also be achieved with this module. To work with multiple connections, first create the connections. In this case, connection naming becomes mandatory.
Suppose you have an Album entity stored in its own database.
warning Notice If you don't set the
namefor a connection, its name is set todefault. Please note that you shouldn't have multiple connections without a name, or with the same name, otherwise they will get overridden.
At this point, you have User and Album models registered with their own connection. With this setup, you have to tell the SequelizeModule.forFeature() method and the @InjectModel() decorator which connection should be used. If you do not pass any connection name, the default connection is used.
You can also inject the Sequelize instance for a given connection:
It's also possible to inject any Sequelize instance to the providers:
Testing
When it comes to unit testing an application, we usually want to avoid making a database connection, keeping our test suites independent and their execution process as fast as possible. But our classes might depend on models that are pulled from the connection instance. How do we handle that? The solution is to create mock models. In order to achieve that, we set up custom providers. Each registered model is automatically represented by a <ModelName>Model token, where ModelName is the name of your model class.
The @nestjs/sequelize package exposes the getModelToken() function which returns a prepared token based on a given model.
Now a substitute mockModel will be used as the UserModel. Whenever any class asks for UserModel using an @InjectModel() decorator, Nest will use the registered mockModel object.
Async configuration
You may want to pass your SequelizeModule options asynchronously instead of statically. In this case, use the forRootAsync() method, which provides several ways to deal with async configuration.
One approach is to use a factory function:
Our factory behaves like any other asynchronous provider (e.g., it can be async and it's able to inject dependencies through inject).
Alternatively, you can use the useClass syntax:
The construction above will instantiate SequelizeConfigService inside SequelizeModule and use it to provide an options object by calling createSequelizeOptions(). Note that this means that the SequelizeConfigService has to implement the SequelizeOptionsFactory interface, as shown below:
In order to prevent the creation of SequelizeConfigService inside SequelizeModule and use a provider imported from a different module, you can use the useExisting syntax.
This construction works the same as useClass with one critical difference - SequelizeModule will lookup imported modules to reuse an existing ConfigService instead of instantiating a new one.
Example
A working example is available here.

