Reputation: 1236
I set up a ConfigService as described in docs https://docs.nestjs.com/techniques/configuration
How can I use this service with the the TypeOrmModule?
TypeOrmModule.forRoot({
type: 'mysql',
host: 'localhost',
port: 3306,
username: 'root',
password: 'root',
database: 'test',
entities: [__dirname + '/**/*.entity{.ts,.js}'],
synchronize: true,
}),
Upvotes: 27
Views: 45863
Reputation: 21
i created a type-orm-config.service.ts file and in this file i get db credentials from env
import { Injectable } from '@nestjs/common';
import { TypeOrmModuleOptions, TypeOrmOptionsFactory } from '@nestjs/typeorm';
@Injectable()
export class TypeOrmConfigService implements TypeOrmOptionsFactory {
constructor(private configService: ConfigService) {}
createTypeOrmOptions(): TypeOrmModuleOptions {
return {
type: 'postgres',
host: this.configService.get<string>('DATABASE_HOST'),
port: +this.configService.get<string>('DATABASE_PORT'),
username: this.configService.get<string>('DATABASE_USER'),
password: this.configService.get<string>('DATABASE_PASSWORD'),
database: this.configService.get<string>('DATABASE_NAME'),
entities: [],
migrations: ['dist/migrations/*.js'],
synchronize: false,
};
}
}
Then for the app.module file i import TypeOrmModule as async
@Module({
imports: [
ConfigModule.forRoot(),
TypeOrmModule.forRootAsync({
imports: [ConfigModule],
useClass: TypeOrmConfigService,
}),
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
Upvotes: 0
Reputation: 1
I prefered created seperated file for typeorm config "typeorm.config.ts".
Inside this file export config:typeormmoduleoptions
Then in app.module.ts
@Module({imports:[typeormmodule.forroot(config)]]})
Upvotes: 0
Reputation: 5756
Following on @Monfa.red's answer, here's an example of what TypeORMConfigService looks like:
import {Injectable} from '@nestjs/common'
import {ConfigService} from '@nestjs/config'
import {TypeOrmModuleOptions, TypeOrmOptionsFactory} from '@nestjs/typeorm'
@Injectable()
export class TypeOrmConfigService implements TypeOrmOptionsFactory {
constructor(protected readonly configService: ConfigService) {}
createTypeOrmOptions(): TypeOrmModuleOptions {
const {configService} = this
return {
type: 'postgres',
host: configService.getOrThrow('POSTGRES_HOST'),
port: configService.getOrThrow('POSTGRES_PORT'),
username: configService.getOrThrow('POSTGRES_USER'),
password: configService.getOrThrow('POSTGRES_PASSWORD'),
database: configService.getOrThrow('POSTGRES_DB'),
autoLoadEntities: true,
synchronize: !configService.get('PRODUCTION')
}
}
}
Upvotes: 2
Reputation: 4506
In NestJS 10.0.0 a superclean way of doing this is as follows:
.env
file.# DATABASE
DB_HOST=localhost
DB_PORT=5432
DB_NAME=demo-db
DB_USERNAME=postgres
DB_PASSWORD=example
registerAs
.// config/database.config.ts
import { registerAs } from '@nestjs/config';
import { TypeOrmModuleOptions } from '@nestjs/typeorm';
export default registerAs(
'database',
(): TypeOrmModuleOptions => ({
type: 'postgres',
host: process.env.DB_HOST,
port: +process.env.DB_PORT,
username: process.env.DB_USERNAME,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME,
autoLoadEntities: true,
synchronize: true,
}),
);
databaseConfig
into the main app.module.ts
as follows:// app.module.ts
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { TypeOrmModule } from '@nestjs/typeorm';
import { ConfigModule } from '@nestjs/config';
import databaseConfig from './config/database.config';
@Module({
imports: [
ConfigModule.forRoot(),
TypeOrmModule.forRoot(databaseConfig()),
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
Additionally, If you wish to access this database
configuration in one of your module services, you can do it as below:
// app.module.ts
@Module({
imports: [
ConfigModule.forRoot({ load: [databaseConfig] }),
TypeOrmModule.forRoot(databaseConfig()),
...
],
...
})
export class AppModule {}
// [feature].module.ts
@Module({
imports: [ConfigModule],
controllers: [FeatureController],
providers: [FeatureService],
})
export class FeatureModule {}
// feature.service.ts
@Injectable()
export class FeatureService {
constructor(private configService: ConfigService) {}
printConfiguration() {
console.log(this.configService.get('database'));
}
}
Upvotes: 7
Reputation: 9178
See https://docs.nestjs.com/techniques/database#async-configuration Async Configuration chapter
import { ConfigService } from './config.service'
import { Module } from '@nestjs/common'
import { TypeOrmModule } from '@nestjs/typeorm'
@Module({
imports: [
TypeOrmModule.forRootAsync({
imports: [ConfigModule],
useFactory: (config: ConfigService) => config.get('database'),
inject: [ConfigService],
}),
],
})
export class AppModule {}
Upvotes: 72