How to use TypeOrmModuleOptions Within a NestJS App

Posted December 1, 2023
How to use TypeOrmModuleOptions Within a NestJS App

TypeOrmModuleOptions extends the TypeORM configurations you want to add within a NestJS app. In this tutorial, I will show you how to use TypeOrmModuleOptions perfectly with NestJS.

Related: How to use TypeOrmModule forFeature and forRoot Methods

Related: How to use TypeOrmModule forRootAsync and configService

What is TypeOrmModuleOptions

TypeOrmModuleOptions is an interface of the @nestjs/typeorm module. It represents the configuration options passed when setting up the TypeORM module and related database connection parameters. Instead of using these settings directly, use TypeOrmModule as follows:

@Module({
  imports: [
    TypeOrmModule.forRoot({
      type: 'mysql', // or any other supported database type
      host: 'localhost',
      port: 3306,
      username: 'username',
      password: 'password',
      database: 'database_name',
      entities: [__dirname + '/**/*.entity{.ts,.js}'], // your entities
      synchronize: true, // Auto-create database tables (not suitable for production)
    }),
  ],
  controllers: [],
  providers: [],
})
export class AppModule {}

TypeOrmModuleOptions changes the game. You don’t need to use Config modules. Instead, you can still create a separate TypeORM configuration file and use it in your Modules.

How to use TypeOrmModuleOptions

Still, on your main module app.module.ts file, you will use TypeOrmModuleOptions as follows:

import { TypeOrmModule, TypeOrmModuleOptions } from '@nestjs/typeorm';

const typeOrmConfig: TypeOrmModuleOptions = {
  type: 'mysql', // or any other supported database type
  host: 'localhost',
  port: 3306,
  username: 'username',
  password: 'password',
  database: 'database_name',
  entities: [__dirname + '/**/*.entity{.ts,.js}'], // path to your entity files
  synchronize: true, 
};

@Module({
  imports: [TypeOrmModule.forRoot(typeOrmConfig)],
})
export class AppModule {}

Note that TypeORM settings are not passed directly to TypeOrmModule.forRoot. Instead, you create a variable typeOrmConfig to hold them and use it in [TypeOrmModule.forRoot(typeOrmConfig)]

Alternatively, you can create a file with these settings. For example, create a typeorm.congif.ts file and use TypeOrmModuleOptions as follows:

import {TypeOrmModuleOptions } from '@nestjs/typeorm';

export const typeOrmConfig: TypeOrmModuleOptions = {
  type: 'mysql', // or any other supported database type
  host: 'localhost',
  port: 3306,
  username: 'username',
  password: 'password',
  database: 'database_name',
  entities: [__dirname + '/**/*.entity{.ts,.js}'], // path to your entity files
  synchronize: true, 
};

Now, you will import typeOrmConfig and use it as such:

import { TypeOrmModule } from '@nestjs/typeorm';
import {typeOrmConfig} from './typeorm.congif'

@Module({
  imports: [TypeOrmModule.forRoot(typeOrmConfig)],
})
export class AppModule {}

Advanced TypeOrmModuleOptions Configuration

TypeOrmModuleOptions can extend other information, such as logging to see SQL queries, adding migrations, and SSL configurations as follows:

const typeOrmConfig: TypeOrmModuleOptions = {
  type: 'mysql', // or any other supported database type
  host: 'localhost',
  port: 3306,
  username: 'username',
  password: 'password',
  database: 'database_name',
  entities: [__dirname + '/**/*.entity{.ts,.js}'], // path to your entity files
  ssl: true,
  logging: true,
    migrations: [__dirname + '/migrations/*{.ts,.js}'],
  cli: {
    migrationsDir: 'src/migrations',
  },
  synchronize: false, 
  retryAttempts: 5,
  retryDelay: 3000,
};

Retry Options add:

  • retryAttempts for the number of retry attempts.
  • retryDelay for delay between retries (in milliseconds).

But when you have migrations, synchronize will change to false.

For migrations, I have this Perfect Create, Generate, and Run TypeORM Migrations in NestJS with PostgreSQL guide that you will enjoy reading.

Conclusion

This guide showed you the perfect way to use TypeOrmModuleOptions in a NestJS. Customize these configurations based on your database setup and refer to the official docs for the most up-to-date TypeORM configuration options. I hope you found this helpful!

How to use TypeOrmModuleOptions Within a NestJS App

Written By:

Joseph Chege