Need help with typeorm-nestjs-migration-example?
Click the “chat” button below for chat support from the developer who created it, or find similar developers for support.

About the developer

207 Stars 30 Forks 8 Commits 1 Opened issues


"Example of how to use migrations feature of TypeORM with NestJS.

Services available


Need anything else?

Contributors list

No Data

TypeORM migration


Generate and use migrations instead of syncing database. In dev and prod. This is the recommended method by TypeORM once you have data on prod, to avoid any loss.


TypeORM installed:


src/app.module.ts ```ts import { DynamicModule, Module } from '@nestjs/common'; import { AppController } from './app.controller'; import { AppService } from './app.service'; import { TypeOrmModule } from '@nestjs/typeorm'; import * as ormconfig from './ormconfig';

export function DatabaseOrmModule(): DynamicModule { // we could load the configuration from dotEnv here, // but typeORM cli would not be able to find the configuration file.

return TypeOrmModule.forRoot(ormconfig); }

@Module({ imports: [ TypeOrmModule.forRoot(ormconfig) // or // DatabaseOrmModule(), ], controllers: [AppController], providers: [AppService], }) export class AppModule {} ```

src/ormconfig.ts ```ts import {ConnectionOptions} from 'typeorm';

// You can load you .env file here synchronously using dotenv package (not installed here), // import * as dotenv from 'dotenv'; // import * as fs from 'fs'; // const environment = process.env.NODE_ENV || 'development'; // const data: any = dotenv.parse(fs.readFileSync(

)); // You can also make a singleton service that load and expose the .env file content. // ...

// Check typeORM documentation for more information. const config: ConnectionOptions = { type: 'postgres', host: 'localhost', port: 5432, username: 'postgres', password: 'pwd', database: 'migrationexample', entities: [__dirname + '/*/.entity{.ts,.js}'],

// We are using migrations, synchronize should be set to false. synchronize: false,

// Run migrations automatically, // you can disable this if you prefer running migration manually. migrationsRun: true, logging: true, logger: 'file',

// Allow both start:prod and start:dev to use migrations // dirname is either dist or src folder, meaning either // the compiled js in prod or the ts in dev. migrations: [dirname + '/migrations/*/{.ts,.js}'], cli: { // Location of migration should be inside src folder // to be compiled into dist/ folder. migrationsDir: 'src/migrations', }, };

export = config; ```


"scripts": {
    "typeorm": "ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli.js --config src/ormconfig.ts",
    "typeorm:migrate": "npm run typeorm migration:generate -- -n",
    "typeorm:run": "npm run typeorm migration:run"


  1. npm run typeorm:migrate 
  2. Check your migration queries in
  3. npm run start:dev
    npm run start:prod
    npm run typeorm:run

If everything went well, you have up to date entites and a

table listing applied migrations.

Additionnal information

  • If you set
    to false in ormconfig.ts, you will have to use
    npm run typeorm:run
    to apply the migration, otherwise all migrations are applied automatically at application start.
  • If you do not set
    parameter typeorm seek a valid configuration file at the root of the project.
  • You do not want
    at the root of the project, otherwise it change /dist structure, you would have to change
    start:prod: node dist/main.js
    start:prod: node dist/src/main.js


We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.