小编Jay*_*Jay的帖子

如何实现 NestJs 配置文件、.env (DOTENV)、TypeOrm 数据库配置和迁移

我一直在尝试让我的 TypeOrm Config 与 Migrations 和 .env 很好地配合工作,而不需要在两个地方指定它。

斗争是这样的 - 我的迁移脚本需要读取立即返回的对象 -

{
  type: 'postgres',
  host: '127.0.0.1',
  port: 5432,
  username: 'postgres',
  password: 'myPassword',
  database: 'postgres',
  entities: [ 'dist/**/*.entity.js' ],
  logging: [ 'query', 'error', 'schema' ],
  synchronize: false,
  migrations: [ 'dist/app/database/migrations/*.js' ],
  cli: { migrationsDir: 'src/app/database/migrations' },
  namingStrategy: SnakeNamingStrategy {
    nestedSetColumnNames: { left: 'nsleft', right: 'nsright' },
    materializedPathColumnName: 'mpath'
  },
  subscribers: [],
  migrationsRun: false,
  dropSchema: false
}
Run Code Online (Sandbox Code Playgroud)

但是,当我使用 NestJs 建议的配置,同时还利用 .env (DOTENV) 文件时,解决方案如下所示:

import {TypeOrmModuleOptions, TypeOrmOptionsFactory} from "@nestjs/typeorm";
import {SnakeNamingStrategy} from …
Run Code Online (Sandbox Code Playgroud)

migration typeorm nestjs dotenv nestjs-config

1
推荐指数
1
解决办法
3981
查看次数

标签 统计

dotenv ×1

migration ×1

nestjs ×1

nestjs-config ×1

typeorm ×1