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?
Regarding the error message, you should add ormconfig.json in the root project. The.env file does not relate in this case.
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"
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:
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. 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',
},
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.