发布于 2026-01-05 1 阅读
0

在 NestJS 中使用 TypeORM 进行 Postgres 数据库迁移

在 NestJS 中使用 TypeORM 进行 Postgres 数据库迁移

如果您正在构建 NestJS 应用程序并希望使用带有 TypeORM 迁移的 PostgreSQL 数据库,那么本文将指导您如何高效地创建和运行迁移。

首先,使用 Nest CLI 创建一个 NestJS 应用程序:

nest new my-app
Enter fullscreen mode Exit fullscreen mode

接下来,安装 TypeORM、Postgres 和 Configuration 所需的软件包:

npm install --save @nestjs/typeorm typeorm pg @nestjs/config
Enter fullscreen mode Exit fullscreen mode

在该目录中创建一个文件,用于存储 TypeORM 配置src/config/typeorm.ts。该文件应导出 TypeORM 配置对象和连接源对象。

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);
Enter fullscreen mode Exit fullscreen mode

将 TypeORM 加载到app.module.tsNestJS 应用程序中以使用它:

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 { }
Enter fullscreen mode Exit fullscreen mode

在目录中创建一个 .env 文件src/,用于定义文件中的变量typeorm.ts

DATABASE_HOST=postgres
DATABASE_PORT=5432
DATABASE_USERNAME=postgres
DATABASE_PASSWORD=dev-secret
DATABASE_NAME=postgres
Enter fullscreen mode Exit fullscreen mode

将 TypeORM 迁移命令添加到您的package.json文件中:

"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"
  },
Enter fullscreen mode Exit fullscreen mode

要执行迁移,请使用以下命令:

npm run migration:run
Enter fullscreen mode Exit fullscreen mode

要创建新的迁移,请使用以下命令:

npm run migration:create --name=your_migration_name
Enter fullscreen mode Exit fullscreen mode

好了!有了本指南,您现在应该能够在 NestJS 应用程序中高效地使用 TypeORM 进行 Postgres 数据库迁移了。

文章来源:https://dev.to/amirfakour/using-typeorm-migration-in-nestjs-with-postgres-database-3c75