## Option 1: As an `injectable service`
You can create a service `SomeTypeOrmConfigService` as an `@Injectable()`, and then inject it like so.
```typescript
import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { TypeOrmModuleOptions, TypeOrmOptionsFactory } from '@nestjs/typeorm';
// create your injectable here
@Injectable()
export class SomeTypeOrmConfigService implements TypeOrmOptionsFactory {
constructor(private configService: ConfigService) {}
createTypeOrmOptions(): TypeOrmModuleOptions {
return {
type: 'mysql',
host: this.configService.get('DATABASE_HOST'),
username: this.configService.get('DATABASE_USERNAME'),
password: this.configService.get('DATABASE_PASSWORD'),
};
}
}
```
__________________
## Option 2: Save the information in a common `.env`, or b) another approach `database.provider.ts` file and export that copied ans./credit to [from here][1]
import { ConfigModule, ConfigService } from '@nestjs/config';
import { TypeOrmModule } from '@nestjs/typeorm';
export const databaseProviders = [
TypeOrmModule.forRootAsync({
imports: [ConfigModule],
inject: [ConfigService],
useFactory: (configService: ConfigService) => ({
type: 'postgres',
host: configService.get('PGHOST'),
port: +configService.get<number>('PGPORT'),
username: configService.get('PGUSER'),
password: configService.get('PGPASSWORD'),
database: configService.get('PGDATABASE'),
entities: ['dist/**/*.entity{.ts,.js}'],
synchronize: false,
logging: true,
}),
}),
];
___________
Then import it in your database.module.ts
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
import { databaseProviders } from './database.provider';
import { DatabaseService } from './database.service';
import { Module } from '@nestjs/common';
@Module({
imports: [...databaseProviders],
providers: [DatabaseService],
exports: [...databaseProviders],
})
export class DatabaseModule {}
<!-- end snippet -->
And that's all, you can add multiple connections in your database.provider.ts if you want it, also, don´t forget to create the .env and import the database module in your root module.
[1]: https://stackoverflow.com/a/64127695/6085193
Create a file (in this case the file is database.provider.ts) the file will export an array of connections.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
import { ConfigModule, ConfigService } from '@nestjs/config';
import { TypeOrmModule } from '@nestjs/typeorm';
export const databaseProviders = [
TypeOrmModule.forRootAsync({
imports: [ConfigModule],
inject: [ConfigService],
useFactory: (configService: ConfigService) => ({
type: 'postgres',
host: configService.get('PGHOST'),
port: +configService.get<number>('PGPORT'),
username: configService.get('PGUSER'),
password: configService.get('PGPASSWORD'),
database: configService.get('PGDATABASE'),
entities: ['dist/**/*.entity{.ts,.js}'],
synchronize: false,
logging: true,
}),
}),
];
<!-- end snippet -->
Then import it in your database.module.ts
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
import { databaseProviders } from './database.provider';
import { DatabaseService } from './database.service';
import { Module } from '@nestjs/common';
@Module({
imports: [...databaseProviders],
providers: [DatabaseService],
exports: [...databaseProviders],
})
export class DatabaseModule {}
<!-- end snippet -->
And that's all, you can add multiple connections in your database.provider.ts if you want it, also, don´t forget to create the .env and import the database module in your root module.