简体   繁体   English

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

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

I want to set up configuration of TypeORM with .env file, but i have problem when I am trying to run migration script.我想使用.env文件设置 TypeORM 的配置,但是当我尝试运行迁移脚本时遇到问题。

What i did:我做了什么:

1.Added scripts to package.json 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.Imported TypeOrmModule in app.module* 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 file in root folder 3.根目录下的Creaded.env文件

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

now I am trying to run migration script like this:现在我正在尝试像这样运行迁移脚本:

npm run migration:generate -n AddUserTable

and I reciving error like this:我收到这样的错误:

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

According to documentation it shuld be some ormconfig.json but it should also working with .env .根据文档,它应该是一些ormconfig.json但它也应该与.env一起使用。 Please tell me, what is wrong in my case?请告诉我,我的情况有什么问题?

Regarding the error message, you should add ormconfig.json in the root project.关于错误信息,您应该在根项目中添加 ormconfig.json。 The.env file does not relate in this case.在这种情况下,.env 文件不相关。

Check import ConfigModule.forRoot() .检查导入ConfigModule.forRoot() It should be imported first应该先导入

You can not use forRootAsync for TypeOrmModule if you use these variables in the env file如果在 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 https://github.com/typeorm/typeorm/blob/master/docs/using-ormconfig.md#using-environment-variables

You can also try such scripts:你也可以试试这样的脚本:

"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"

I was having the same problem.我遇到了同样的问题。 I solved it by following the config instructions in this article, which shows you how to generate a dynamic ormconfig.json file:我按照本文中的配置说明解决了这个问题,它向您展示了如何生成动态ormconfig.json文件:

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

The extra setup is a bit annoying, but it works.额外的设置有点烦人,但它确实有效。

Just a note if you are using typescript - the article is from 2019 and per 2020 updates to nestjs, you'll want to change the src paths to allow for src or dist in the config.service.ts file, eg.如果您使用的是 typescript,请注意 - 这篇文章来自 2019 年,并且每 2020 年更新到 nestjs,您需要更改src路径以允许config.service.ts文件中的srcdist ,例如。 change to something like this (depending on your file structure):更改为这样的内容(取决于您的文件结构):

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

      migrationsTableName: 'migration',

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

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

to

      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