nestjs / typeorm

TypeORM module for Nest framework (node.js) 🍇

Home Page:https://nestjs.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

TYPEORM_ENTITIES not taken into account when calling `TypeOrmModule.forRoot`

hgezim opened this issue · comments

I'm submitting a...


[ ] Regression 
[x] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

TYPEORM_ENTITIES=src/**/*.entity.ts set as an env variable doesn't seem to be taken into consideration when TypeOrmModule.forRoot is called with no args.

Expected behavior

I expect value of TYPEORM_ENTITIES to be taken into account when calling TypeOrmModule.forRoot().

It essentially should result in the same result as this:

TypeOrmModule.forRoot(
    {
      entities: ['src/**/*.entity.ts']
    }
  )

If TYPEORM_ENTITIES=src/**/*.entity.ts env is provided.

Minimal reproduction of the problem with instructions

  1. Create a boilerplate nest project
  2. Set TYPEORM_ENTITIES as the env variable
  3. Create a database service like so:
@Module({
  imports: [TypeOrmModule.forRoot(
    {
      entities: ['src/**/*.entity.ts']
    }
  )],
  providers: [DatabaseService],
})
export class DatabaseModule {
  constructor() { }
}
  1. Import it into your main.
  2. Start the project and note how long this step takes TypeOrmCoreModule dependencies initialized
  3. Change database service and pass { entities: ['src/**/*.entity.ts'] } to forRoot.
  4. Start the project again and note how long TypeOrmCoreModule dependencies initialized takes.

What is the motivation / use case for changing the behavior?

I was really perplexed as to why this step in app loading would take forever:

TypeOrmCoreModule dependencies initialized +530848ms. My env variables were set using the TYPEORM_* convention and namely:

TYPEORM_ENTITIES=src/**/*.entity.ts

Explicitly passing entities to forRoot fixed this:

TypeOrmCoreModule dependencies initialized +457ms

Environment


Nest version: @nestjs/core@6.0.2, @nestjs/typeorm@6.0.0

 
For Tooling issues:
- Node version: v11.15.0  
- Platform: Linux  

Others:

You must explicitly set the env variable:

TypeOrmModule.forRoot({
  ...
  entities: process.env. TYPEORM_ENTITIES,
})

This package doesn't check your env variables and we don't plan to add this in the future.