add TypeORM postgres config and migration runner.
This commit is contained in:
@@ -1,13 +1,19 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { ConfigModule } from '@nestjs/config';
|
||||
import appConfig from './config';
|
||||
import { ConfigModule, ConfigService, registerAs } from '@nestjs/config';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { getAppConfig, getPostgresConfig } from './config';
|
||||
import { HealthCheckModule } from './modules/healthCheck/HealthCheckModule';
|
||||
import { Config } from './types/Config';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
ConfigModule.forRoot({
|
||||
isGlobal: true,
|
||||
load: [appConfig]
|
||||
load: [registerAs('postgres', getPostgresConfig), registerAs('app', getAppConfig)]
|
||||
}),
|
||||
TypeOrmModule.forRootAsync({
|
||||
useFactory: (configService: ConfigService) => configService.get('postgres') as Config['postgres'],
|
||||
inject: [ConfigService]
|
||||
}),
|
||||
HealthCheckModule
|
||||
]
|
||||
|
||||
@@ -1,11 +1,28 @@
|
||||
import { registerAs } from '@nestjs/config';
|
||||
import { AppConfig } from '../types/AppConfig';
|
||||
import { PostgresConfig } from '../types/Config';
|
||||
import { NodeEnv } from '../types/NodeEnv';
|
||||
|
||||
const env = (key: string): string => process.env[key] || '';
|
||||
|
||||
const envInt = (key: string): number => parseInt(env(key), 10);
|
||||
|
||||
const isEnabled = (key: string): boolean => env(key) === 'true';
|
||||
|
||||
export const getPostgresConfig = (): PostgresConfig => {
|
||||
return {
|
||||
type: 'postgres',
|
||||
url: `postgres://${env('POSTGRES_USER')}:${env('POSTGRES_PASSWORD')}@${env('POSTGRES_HOST')}:${env('POSTGRES_PORT')}/${env('POSTGRES_DB')}`,
|
||||
host: env('POSTGRES_HOST'),
|
||||
username: env('POSTGRES_USER'),
|
||||
password: env('POSTGRES_PASSWORD'),
|
||||
port: envInt('POSTGRES_PORT'),
|
||||
database: env('POSTGRES_DB'),
|
||||
entities: [__dirname + '/../modules/**/entities/*{.ts,.js}'],
|
||||
migrations: [__dirname + '/../database/migrations/*{.ts,.js}'],
|
||||
migrationsRun: isEnabled('POSTGRES_MIGRATIONS_RUN')
|
||||
};
|
||||
};
|
||||
|
||||
export const getAppConfig = (): AppConfig => {
|
||||
const rawOrigins = env('CORS_ORIGINS');
|
||||
|
||||
@@ -18,5 +35,3 @@ export const getAppConfig = (): AppConfig => {
|
||||
.filter(Boolean)
|
||||
};
|
||||
};
|
||||
|
||||
export default registerAs('app', getAppConfig);
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
import { DataSource } from 'typeorm';
|
||||
|
||||
import { getPostgresConfig } from '../config';
|
||||
|
||||
export default new DataSource(getPostgresConfig());
|
||||
+2
-2
@@ -2,13 +2,13 @@ import { ValidationPipe } from '@nestjs/common';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import { NestFactory } from '@nestjs/core';
|
||||
import { AppModule } from './AppModule';
|
||||
import { AppConfig } from './types/AppConfig';
|
||||
import { Config } from './types/Config';
|
||||
|
||||
async function bootstrap() {
|
||||
const app = await NestFactory.create(AppModule);
|
||||
|
||||
const configService = app.get(ConfigService);
|
||||
const { port, corsOrigins } = configService.get('app') as AppConfig;
|
||||
const { port, corsOrigins } = configService.get('app') as Config['app'];
|
||||
|
||||
app.setGlobalPrefix('api');
|
||||
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
import { AppConfig } from './AppConfig';
|
||||
|
||||
export interface PostgresConfig {
|
||||
type: 'postgres';
|
||||
url: string;
|
||||
host: string;
|
||||
username: string;
|
||||
password: string;
|
||||
port: number;
|
||||
database: string;
|
||||
entities: string[];
|
||||
migrations: string[];
|
||||
migrationsRun: boolean;
|
||||
}
|
||||
|
||||
export interface Config {
|
||||
app: AppConfig;
|
||||
postgres: PostgresConfig;
|
||||
}
|
||||
Reference in New Issue
Block a user