Using TypeORM Migration in NestJS with Postgres Database

If you are building a NestJS application and want to use a PostgreSQL database with TypeORM migration, then this article will guide you on creating and running migrations efficiently.

First, create a NestJS application with Nest CLI:

nest new my-ap…


This content originally appeared on DEV Community and was authored by amir fakoor

If you are building a NestJS application and want to use a PostgreSQL database with TypeORM migration, then this article will guide you on creating and running migrations efficiently.

First, create a NestJS application with Nest CLI:

nest new my-app

Next, install the required packages for TypeORM, Postgres, and Configuration:

npm install --save @nestjs/typeorm typeorm pg @nestjs/config

Create a file to store the TypeORM configuration in the src/config/typeorm.ts directory. This file should export a TypeORM configuration object and a connection source object.

import { registerAs } from "@nestjs/config";
import { config as dotenvConfig } from 'dotenv';
import { DataSource, DataSourceOptions } from "typeorm";

dotenvConfig({ path: '.env' });

const config = {
    type: 'postgres',
    host: `${process.env.DATABASE_HOST}`,
    port: `${process.env.DATABASE_PORT}`,
    username: `${process.env.DATABASE_USERNAME}`,
    password: `${process.env.DATABASE_PASSWORD}`,
    database: `${process.env.DATABASE_NAME}`,
    entities: ["dist/**/*.entity{.ts,.js}"],
    migrations: ["dist/migrations/*{.ts,.js}"],
    autoLoadEntities: true,
    synchronize: false,
}

export default registerAs('typeorm', () => config)
export const connectionSource = new DataSource(config as DataSourceOptions);

Load TypeORM into app.module.ts to use it in your NestJS application:

import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { TypeOrmModule } from '@nestjs/typeorm';
import typeorm from './config/typeorm';

@Module({
  imports: [
    ConfigModule.forRoot({
      isGlobal: true,
      load: [typeorm]
    }),
    TypeOrmModule.forRootAsync({
      inject: [ConfigService],
      useFactory: async (configService: ConfigService) => (configService.get('typeorm'))
    }),
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule { }

Create a .env file in the src/ directory to define the variables in your typeorm.ts file:

DATABASE_HOST=postgres
DATABASE_PORT=5432
DATABASE_USERNAME=postgres
DATABASE_PASSWORD=dev-secret
DATABASE_NAME=postgres

Add the TypeORM migration commands into your package.json file:

"scripts": {
    // ...
    "typeorm": "ts-node ./node_modules/typeorm/cli",
    "migration:run": "npm run typeorm migration:run -- -d ./src/config/typeorm.ts",
    "migration:generate": "npm run typeorm -- -d ./src/config/typeorm.ts migration:generate ./src/migrations/$npm_config_name",
    "migration:create": "npm run typeorm -- migration:create ./src/migrations/$npm_config_name",
    "migration:revert": "npm run typeorm -- -d ./src/config/typeorm.ts migration:revert"
  },

To execute migration, use the following command:

npm run migration:run

To create a new migration, use the following command:

npm run migration:create --name=your_migration_name

That's it! With this guide, you should now be able to use TypeORM migration with Postgres in your NestJS application efficiently.


This content originally appeared on DEV Community and was authored by amir fakoor


Print Share Comment Cite Upload Translate Updates
APA

amir fakoor | Sciencx (2023-05-03T16:45:42+00:00) Using TypeORM Migration in NestJS with Postgres Database. Retrieved from https://www.scien.cx/2023/05/03/using-typeorm-migration-in-nestjs-with-postgres-database/

MLA
" » Using TypeORM Migration in NestJS with Postgres Database." amir fakoor | Sciencx - Wednesday May 3, 2023, https://www.scien.cx/2023/05/03/using-typeorm-migration-in-nestjs-with-postgres-database/
HARVARD
amir fakoor | Sciencx Wednesday May 3, 2023 » Using TypeORM Migration in NestJS with Postgres Database., viewed ,<https://www.scien.cx/2023/05/03/using-typeorm-migration-in-nestjs-with-postgres-database/>
VANCOUVER
amir fakoor | Sciencx - » Using TypeORM Migration in NestJS with Postgres Database. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2023/05/03/using-typeorm-migration-in-nestjs-with-postgres-database/
CHICAGO
" » Using TypeORM Migration in NestJS with Postgres Database." amir fakoor | Sciencx - Accessed . https://www.scien.cx/2023/05/03/using-typeorm-migration-in-nestjs-with-postgres-database/
IEEE
" » Using TypeORM Migration in NestJS with Postgres Database." amir fakoor | Sciencx [Online]. Available: https://www.scien.cx/2023/05/03/using-typeorm-migration-in-nestjs-with-postgres-database/. [Accessed: ]
rf:citation
» Using TypeORM Migration in NestJS with Postgres Database | amir fakoor | Sciencx | https://www.scien.cx/2023/05/03/using-typeorm-migration-in-nestjs-with-postgres-database/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.