In development environement, i’ve used Typeorm in my Nestjs application. I use the forRootAsync in the app.module.ts to import it with injecting ConfigService to use environement variables:
app.module.ts
imports: [
ConfigModule.forRoot({
envFilePath: ['.env.local', '.env'],
isGlobal: true,
}),
TypeOrmModule.forRootAsync({
inject: [ConfigService],
useFactory: getTypeOrmConfig,
}),
// ...
]
My Typeorm config is as follows:
typeorm.config.ts
import { ConfigService } from '@nestjs/config';
import { TypeOrmModuleOptions } from '@nestjs/typeorm';
const getTypeOrmConfig = (
configService: ConfigService,
): TypeOrmModuleOptions => ({
type: 'mysql',
host: configService.get('MYSQL_IP'),
port: configService.get('MYSQL_HOST'),
username: 'root',
password: configService.get('MYSQL_ROOT_PASSWORD'),
database: configService.get('MYSQL_DATABASE'),
entities: [__dirname + '/**/*.entity{.ts,.js}'],
synchronize: true, // Set to false in production
});
export default getTypeOrmConfig;
I used this method because my environement variables were not accessible from process.env despite me following all the guidelines to do so.
My problem is that i want to export the DataSource object so that i can use it to run my migrations in the production environment. Since my DataSourceOptions depend on the ConfigService, i don’t know how to do that. Thank you for all your insights.