簡體   English   中英

如何在 Nest.JS 中設置 ORM 類型的配置 withth.env 文件

[英]How to set up configuration of Type ORM witth.env file in Nest.JS

我想使用.env文件設置 TypeORM 的配置,但是當我嘗試運行遷移腳本時遇到問題。

我做了什么:

1.package.json增加腳本

    "migration:generate": "node_modules/.bin/typeorm migration:generate -n",
    "migration:run": "node_modules/.bin/typeorm migration:run",
    "migration:revert": "node_modules/.bin/typeorm migration:revert",

2.在app.module中導入TypeOrmModule*

    TypeOrmModule.forRootAsync({
        imports: [ConfigModule],
        inject: [ConfigService],
        useFactory: (configService: ConfigService) => ({
          type: 'mysql',
          host: configService.get('HOST'),
          port: +configService.get<number>('PORT'),
          username: configService.get('DATABASE_USERNAME'),
          password: configService.get('DATABASE_PASSWORD'),
          database: configService.get('DATABASE'),
          entities: [__dirname + '/**/*.entity{.ts,.js}'],
          synchronize: true,
        })
      }
    )],

3.根目錄下的Creaded.env文件

    HOST=localhost
    PORT=5432
    DATABASE_USER=dbuser
    DATABASE_PASSWORD=dbpassword
    DATABASE=dbname

現在我正在嘗試像這樣運行遷移腳本:

npm run migration:generate -n AddUserTable

我收到這樣的錯誤:

Error during migration generation:
Error: No connection options were found in any orm configuration files.

根據文檔,它應該是一些ormconfig.json但它也應該與.env一起使用。 請告訴我,我的情況有什么問題?

關於錯誤信息,您應該在根項目中添加 ormconfig.json。 在這種情況下,.env 文件不相關。

檢查導入ConfigModule.forRoot() 應該先導入

如果在 env 文件中使用這些變量,則不能將 forRootAsync 用於 TypeOrmModule

TYPEORM_CONNECTION = postgres
TYPEORM_HOST = localhost
TYPEORM_PORT = 5432
TYPEORM_USERNAME = postgres
TYPEORM_PASSWORD = 12345
TYPEORM_DATABASE = postgres
TYPEORM_SYNCHRONIZE = false
TYPEORM_MIGRATIONS_RUN = false
TYPEORM_ENTITIES = src/modules/**/*.entity.ts
TYPEORM_MIGRATIONS = db/migrations/*.ts
TYPEORM_LOGGING = true

https://github.com/typeorm/typeorm/blob/master/docs/using-ormconfig.md#using-environment-variables

你也可以試試這樣的腳本:

"migration:run": "ts-node --project ./tsconfig.json -r tsconfig-paths/register node_modules/typeorm/cli.js migration:run",
"migration:revert": "ts-node --project ./tsconfig.json -r tsconfig-paths/register node_modules/typeorm/cli.js migration:revert",
"migration:create": "ts-node --project ./tsconfig.json -r tsconfig-paths/register node_modules/typeorm/cli.js migration:create",
"migration:generate": "ts-node --project ./tsconfig.json -r tsconfig-paths/register node_modules/typeorm/cli.js migration:generate"

我遇到了同樣的問題。 我按照本文中的配置說明解決了這個問題,它向您展示了如何生成動態ormconfig.json文件:

https://medium.com/@gausmann.simon/nestjs-typeorm-and-postgresql-full-example-development-and-project-setup-working-with-database-c1a2b1b11b8f

額外的設置有點煩人,但它確實有效。

如果您使用的是 typescript,請注意 - 這篇文章來自 2019 年,並且每 2020 年更新到 nestjs,您需要更改src路徑以允許config.service.ts文件中的srcdist ,例如。 更改為這樣的內容(取決於您的文件結構):

      entities: ['**/*.entity{.ts,.js}'],

      migrationsTableName: 'migration',

      migrations: ['src/migration/*.ts'],

      cli: {
        migrationsDir: 'src/migration',
      },

      entities: [__dirname + '/../**/*.entity{.ts,.js}'],

      migrationsTableName: 'migration',

      migrations: [__dirname + '/../migration/*{.ts,.js}'],

      cli: {
        migrationsDir: 'src/migration',
      },

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM