Reputation: 330
I am relatively new to using typeorm. I am trying to separate the ormconfig.json for production and development environment but I keep getting this error:
Error during migration generation:
Error: Cannot find connection default because its not defined in any orm configuration files.
Folder structure:
database.module.ts
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { Connection, getConnectionOptions } from 'typeorm';
@Module({
imports: [
TypeOrmModule.forRootAsync({
useFactory: async () =>
Object.assign(
await getConnectionOptions(
process.env.NODE_ENV === 'production' ? 'prod' : 'dev',
),
),
}),
],
exports: [TypeOrmModule],
})
export class DatabaseModule {
constructor(connection: Connection) {
if (connection.isConnected) console.log('DB Connected Successfully!');
}
}
App.module.ts
...
@Module({
imports: [
ConfigModule.forRoot({ isGlobal: true }),
DatabaseModule,
GraphQLModule.forRoot({
playground: true,
debug: true,
autoSchemaFile: true,
}),
ComponentsModule,
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
ormconfig.json
[
{
"name": "dev",
"type": "postgres",
"host": "localhost",
"port": 5432,
"username": "...",
"password": "...",
"database": "yourcar",
"entities": ["dist/**/entities/*{.ts,.js}"],
"synchronize": true,
"logging":true,
"migrationsRun":true,
"migrations":["dist/database/migrations/*{.ts,.js}"],
"cli": {
"migrationsDir":"src/database/migrations"
}
},
{
"name": "prod",
"type": "postgres",
"host": "localhost",
"port": 5432,
"username": "...",
"password": "...",
"database": "yourcar",
"entities": ["dist/**/entities/*{.ts,.js}"],
"synchronize": false,
"migrationsRun":true,
"migrations":["dist/database/migrations/*{.ts,.js}"],
"cli": {
"migrationsDir":"src/database/migrations"
}
}
]
I thought the ormconfig.json gets automatically detected at the root directory so I tried using just one environment and changed database.module.ts to:
@Module({
imports: [TypeOrmModule.forRoot(typeOrmConfig)],
})
export class DatabaseModule {
constructor(connection: Connection) {
if (connection.isConnected) {
console.log('CONNECTED SUCCESS');
}
}
}
but this also doesn't work so I guess ormconfig is not getting detected only. Any help is greatly appreciated.
Upvotes: 0
Views: 3598
Reputation: 879
Unfortunately, the support for ormconfig.json
was dropped in [email protected]
. See breaking-changes.
NestJS shows an alternative approach via ConfigService
and .env
file here:
TypeOrmModule.forRootAsync({
imports: [ConfigModule],
useFactory: (configService: ConfigService) => ({
type: 'mysql',
host: configService.get('HOST'),
port: +configService.get('PORT'),
username: configService.get('USERNAME'),
password: configService.get('PASSWORD'),
database: configService.get('DATABASE'),
entities: [],
synchronize: true,
}),
inject: [ConfigService],
});
Upvotes: 0
Reputation: 855
You can use NestJS/config with .env
file for declare your BDD config. And in your database.module.ts
you can set this config.
Example :
// app.module.ts
@Module({
imports: [
ConfigModule.forRoot({
isGlobal: true,
cache: true,
load: config,
envFilePath: getEnvFiles(),
}),
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
// src/config/database.config.ts
export default registerAs('database', () => ({
host: process.env.DB_HOST || 'localhost',
port: process.env.DB_PORT || 3306,
user: process.env.DB_USER || 'root',
password: process.env.DB_PASSWORD || 'root',
database: process.env.DB_DATABASE || 'test',
...
}));
// database.module.ts
@Module({
imports: [
TypeOrmModule.forRootAsync({
inject: [ConfigService],
useFactory: async (configService: ConfigService) => ({
type: 'postgres',
host: configService.get<string>('database.host'),
port: configService.get<number>('database.port'),
username: configService.get<string>('database.user'),
password: configService.get<string>('database.password'),
database: configService.get<string>('database.database'),
logger: new CustomORMLogger(new AdvancedConsoleLogger('all'), configService),
logging: configService.get<boolean>('database.logging'),
entities: configService.get<string[]>('database.entities'),
migrations: configService.get<string[]>('database.migrations'),
synchronize: true,
}),
}),
],
exports: [TypeOrmModule],
})
export class DatabaseModule {
constructor(connection: Connection) {
if (connection.isConnected) console.log('DB Connected Successfully!');
}
}
Upvotes: 0