diff --git a/backend/src/TypeOrmConfigService.service.ts b/backend/src/TypeOrmConfigService.service.ts deleted file mode 100644 index 17ab125..0000000 --- a/backend/src/TypeOrmConfigService.service.ts +++ /dev/null @@ -1,44 +0,0 @@ -import { TypeOrmModuleOptions, TypeOrmOptionsFactory } from '@nestjs/typeorm'; -import { Injectable } from '@nestjs/common'; -import { VaultService } from './vault/vault.service'; - -const envData = process.env; - -@Injectable() -export class TypeOrmConfigService - extends VaultService - implements TypeOrmOptionsFactory { - constructor() { - super(); - } - async createTypeOrmOptions(): Promise { - - try { - const path = "cubbyhole/blog/database"; - const vaultData = await this.readSecret(path); - const result = vaultData.data; - return { - type: 'postgres', - host: 'postgres', - port: 5432, - requestTimeout: 300000, - username: 'blog', - password: result.password, - database: 'blog', - entities: [__dirname + '/entities/**/*.entity.{ts,js}'], - synchronize: false, - logging: true, - logger: 'simple-console', - migrations: [__dirname + '/migration/**/*.{ts,js}'], - migrationsRun: true, - extra: { - trustServerCertificate: true, - } - }; - - } catch (error) { - console.error('Error fetching data from Vault:', error); - } - - } -} diff --git a/backend/src/app.module.ts b/backend/src/app.module.ts index 6d9a264..41a49d6 100644 --- a/backend/src/app.module.ts +++ b/backend/src/app.module.ts @@ -2,18 +2,12 @@ import { Module } from '@nestjs/common'; import { AppController } from './app.controller'; import { AppService } from './app.service'; import { PostsModule } from './posts/posts.module'; -import { TypeOrmModule } from "@nestjs/typeorm"; -import { TypeOrmConfigService } from "./TypeOrmConfigService.service"; -// import { TypeOrmLegacyService } from "./TypeOrmLegacyService.service"; - -require("dotenv").config(); +import { DatabaseModule } from './database/database.module'; @Module({ imports: [ - TypeOrmModule.forRootAsync({ - useClass: TypeOrmConfigService, - }), - PostsModule + PostsModule, + DatabaseModule ], controllers: [AppController], providers: [AppService], diff --git a/backend/src/database/database.module.ts b/backend/src/database/database.module.ts new file mode 100644 index 0000000..eec3393 --- /dev/null +++ b/backend/src/database/database.module.ts @@ -0,0 +1,24 @@ +import { Module } from '@nestjs/common'; +import { TypeOrmModule } from '@nestjs/typeorm'; +import { ConfigModule, ConfigService } from '@nestjs/config'; + +@Module({ + imports: [ + ConfigModule.forRoot(), + TypeOrmModule.forRootAsync({ + imports: [ConfigModule], + useFactory: (configService: ConfigService) => ({ + type: 'postgres', + host: configService.get('POSTGRES_HOST'), + port: configService.get('POSTGRES_PORT'), + username: configService.get('BLOG_DB_USER'), + password: configService.get('BLOG_DB_PASSWORD'), + database: configService.get('BLOG_DB_NAME'), + entities: [__dirname + '/../**/*.entity{.ts,.js}'], + synchronize: true, // Be cautious about using synchronize in production + }), + inject: [ConfigService], + }), + ], +}) +export class DatabaseModule {} diff --git a/backend/src/vault/vault.service.ts b/backend/src/vault/vault.service.ts deleted file mode 100644 index 5e490db..0000000 --- a/backend/src/vault/vault.service.ts +++ /dev/null @@ -1,36 +0,0 @@ -import { Injectable } from '@nestjs/common'; -import * as vault from 'node-vault'; -require('dotenv').config(); -@Injectable() -export class VaultService { - private vaultClient: any; - - constructor() { - // Initialize the vault client - this.vaultClient = vault({ - apiVersion: 'v2', - endpoint: process.env.VAULT_ADDR, - token: process.env.VAULT_TOKEN, - }); - } - - async readSecret(secretPath: string): Promise { - try { - const roleId = process.env.ROLE_ID; - const secretId = process.env.SECRET_ID; - - const result = await this.vaultClient.approleLogin({ - role_id: roleId, - secret_id: secretId, - }); - this.vaultClient.token = result.auth.client_token; - const secretData = await this.vaultClient.read(secretPath); - // secretpath is the path in vault where you have stored your secrets - return secretData.data; - } catch (error) { - console.error('Error reading secret from vault:', error); - throw error; - } - } - -}