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 Not reading migrationsDir when not using ormconfig.json

shaunstone0 opened this issue · comments

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

Let me start off that I have researched this problem for about 2 days. I have seen many people do the configuration different ways, and have tried many different solutions. However, this still seems to be to a bug... or perahps a feature?

--

1.) I have created a service that implements the TypeOrmOptionsFactory (typeorm-config.service.ts). This Service provides the connection options in a method createTypeOrmOptions().

return { type: this.type, host: this.host, port: this.port, username: this.username, password: this.password, database: this.database, entities: [this.entities], migrations: ['/src/migration'], cli: { migrationsDir: './src/migration', }, synchronize: false, };

the Cli migrationsDir is set.

2.) In the app.module.ts file I have added the following block:
TypeOrmModule.forRootAsync({ name: 'default', useClass: TypeormConfigService, inject: [NestConfigService, NestLogger], }),

3.) when using my npm run typeorm migration:create -n test the migration is created in the./ dir of my project.

Minimum reproduction code

https://github.com/shaunstone0/history

Steps to reproduce

1.) Npm install
2.) create, or change the variables in the src/config/typeorm-config/typorm-config.service.ts for your connection.
3.) run 'npm run typeorm:migrate -- -n test'
4.) watch were file is created

Expected behavior

When the migrationsDir is set inside config file, the CLI should read it.

Package version

8.0.2

NestJS version

8.2.4

Node.js version

16.3.1

In which operating systems have you tested?

  • macOS
  • Windows
  • Linux

Other

No response

This is not a bug. The typeorm CLI is what is running when you use npm run typeorm and as such it reads the config from a defined location, either the command line itself, or an ormconfig file. The chain Typeorm config service class that you make is for nest to know how to create a class, inject the config service (if there is one), call the config method, and retrieve the confusion for the module. For typeorm (the CLI) to do this, it would need to create a nest application context, pull out the Typeorm config service, and retrieve the configuration.

In short, this is working as intended as they're two separate systems