diff --git a/api/src/app.module.ts b/api/src/app.module.ts index 973aa81d..3ffdb425 100644 --- a/api/src/app.module.ts +++ b/api/src/app.module.ts @@ -1,41 +1,10 @@ import { Module } from '@nestjs/common'; import { AppController } from './app.controller'; import { AppService } from './app.service'; -import { ConfigModule, ConfigService } from '@nestjs/config'; -import { join } from 'path'; -import { TypeOrmModule } from '@nestjs/typeorm'; -import * as process from 'node:process'; +import { ApiConfigModule } from '@api/modules/config/app-config.module'; @Module({ - imports: [ - ConfigModule.forRoot({ - isGlobal: true, - cache: true, - envFilePath: [ - join(__dirname, `../../shared/config/.env.${process.env.NODE_ENV}`), - join(__dirname, '../../shared/config/.env'), - ], - }), - TypeOrmModule.forRootAsync({ - imports: [ConfigModule], - // TODO: Move this to config service method - useFactory: (configService: ConfigService) => ({ - type: 'postgres', - host: configService.get('DB_HOST'), - port: configService.get('DB_PORT'), - username: configService.get('DB_USERNAME'), - password: configService.get('DB_PASSWORD'), - database: configService.get('DB_NAME'), - entities: [join(__dirname, '**', '*.entity.{ts,js}')], - synchronize: true, - ssl: - process.env.NODE_ENV === 'production' - ? { require: true, rejectUnauthorized: false } - : false, - }), - inject: [ConfigService], - }), - ], + imports: [ApiConfigModule], controllers: [AppController], providers: [AppService], }) diff --git a/api/src/modules/auth/authentication/authentication.controller.ts b/api/src/modules/auth/authentication/authentication.controller.ts new file mode 100644 index 00000000..b81a9489 --- /dev/null +++ b/api/src/modules/auth/authentication/authentication.controller.ts @@ -0,0 +1,4 @@ +import { Controller } from '@nestjs/common'; + +@Controller('authentication') +export class AuthenticationController {} diff --git a/api/src/modules/auth/authentication/authentication.module.ts b/api/src/modules/auth/authentication/authentication.module.ts new file mode 100644 index 00000000..0c26a9b2 --- /dev/null +++ b/api/src/modules/auth/authentication/authentication.module.ts @@ -0,0 +1,9 @@ +import { Module } from '@nestjs/common'; +import { AuthenticationService } from './authentication.service'; +import { AuthenticationController } from './authentication.controller'; + +@Module({ + providers: [AuthenticationService], + controllers: [AuthenticationController], +}) +export class AuthenticationModule {} diff --git a/api/src/modules/auth/authentication/authentication.service.ts b/api/src/modules/auth/authentication/authentication.service.ts new file mode 100644 index 00000000..8e234fb8 --- /dev/null +++ b/api/src/modules/auth/authentication/authentication.service.ts @@ -0,0 +1,4 @@ +import { Injectable } from '@nestjs/common'; + +@Injectable() +export class AuthenticationService {} diff --git a/api/src/modules/auth/authorisation/authorisation.module.ts b/api/src/modules/auth/authorisation/authorisation.module.ts new file mode 100644 index 00000000..411e7d4b --- /dev/null +++ b/api/src/modules/auth/authorisation/authorisation.module.ts @@ -0,0 +1,4 @@ +import { Module } from '@nestjs/common'; + +@Module({}) +export class AuthorisationModule {} diff --git a/api/src/modules/config/app-config.module.ts b/api/src/modules/config/app-config.module.ts new file mode 100644 index 00000000..b1cf89c5 --- /dev/null +++ b/api/src/modules/config/app-config.module.ts @@ -0,0 +1,29 @@ +import { Global, Module } from '@nestjs/common'; +import { ConfigModule, ConfigService } from '@nestjs/config'; +import { join } from 'path'; +import { ApiConfigService } from '@api/modules/config/app-config.service'; +import { DatabaseModule } from '@api/modules/config/database/database.module'; + +@Global() +@Module({ + imports: [ + DatabaseModule, + /** + * @note: Check if we can abstract the conf to ApiConfigService + */ + ConfigModule.forRoot({ + isGlobal: true, + cache: true, + envFilePath: [ + join( + __dirname, + `../../../../shared/config/.env.${process.env.NODE_ENV}`, + ), + join(__dirname, '../../../../shared/config/.env'), + ], + }), + ], + providers: [ConfigService, ApiConfigService], + exports: [ApiConfigService, DatabaseModule], +}) +export class ApiConfigModule {} diff --git a/api/src/modules/config/app-config.service.ts b/api/src/modules/config/app-config.service.ts new file mode 100644 index 00000000..2e560a0b --- /dev/null +++ b/api/src/modules/config/app-config.service.ts @@ -0,0 +1,31 @@ +import { Injectable } from '@nestjs/common'; +import { ConfigService } from '@nestjs/config'; +import { join } from 'path'; + +@Injectable() +export class ApiConfigService { + constructor(private configService: ConfigService) {} + + /** + * @note We could abstract this to a data layer access config specific class within database module, as well for other configs when the thing gets more complex. + * we could also abstract the underlying engine type, which is now set in the main app module + */ + getDatabaseConfig() { + return { + host: this.configService.get('DB_HOST'), + port: this.configService.get('DB_PORT'), + username: this.configService.get('DB_USERNAME'), + password: this.configService.get('DB_PASSWORD'), + database: this.configService.get('DB_NAME'), + entities: [join(__dirname, '**', '*.entity.{ts,js}')], + synchronize: true, + ssl: this.isProduction() + ? { require: true, rejectUnauthorized: false } + : false, + }; + } + + private isProduction(): boolean { + return this.configService.get('NODE_ENV') === 'production'; + } +} diff --git a/api/src/modules/config/database/database.module.ts b/api/src/modules/config/database/database.module.ts new file mode 100644 index 00000000..5aa628db --- /dev/null +++ b/api/src/modules/config/database/database.module.ts @@ -0,0 +1,20 @@ +import { forwardRef, Module } from '@nestjs/common'; +import { ApiConfigService } from '@api/modules/config/app-config.service'; +import { TypeOrmModule } from '@nestjs/typeorm'; +import { ApiConfigModule } from '@api/modules/config/app-config.module'; + +@Module({ + imports: [ + TypeOrmModule.forRootAsync({ + imports: [forwardRef(() => ApiConfigModule)], + + useFactory: (config: ApiConfigService) => ({ + ...config.getDatabaseConfig(), + type: 'postgres', + }), + inject: [ApiConfigService], + }), + ], + exports: [TypeOrmModule], +}) +export class DatabaseModule {}