Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set up configuration of Type ORM with .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.

What i did:

1.Added scripts to 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*

    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

    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. Please tell me, what is wrong in my case?

like image 575
obaram Avatar asked Oct 15 '22 03:10

obaram


2 Answers

Regarding the error message, you should add ormconfig.json in the root project. The .env file does not relate in this case.

like image 74
Truoc Pham Avatar answered Nov 15 '22 10:11

Truoc Pham


Check import ConfigModule.forRoot(). It should be imported first

You can not use forRootAsync for TypeOrmModule if you use these variables in the env file

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

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"
like image 22
IERomanov Avatar answered Nov 15 '22 08:11

IERomanov