Skip to content

Commit

Permalink
chore: use nest config service
Browse files Browse the repository at this point in the history
  • Loading branch information
joannechen1223 committed Mar 16, 2024
1 parent 2cd2c52 commit 3f687c1
Show file tree
Hide file tree
Showing 5 changed files with 168 additions and 50 deletions.
11 changes: 10 additions & 1 deletion apps/recnet-api/src/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
import { Module } from "@nestjs/common";
import { ConfigModule } from "@nestjs/config";

import { HealthModule } from "./modules/health/health.module";
import { UserModule } from "./modules/user/user.module";
import * as CommonConfigs from "./config/common.config";

@Module({
imports: [HealthModule, UserModule],
imports: [
ConfigModule.forRoot({
isGlobal: true,
load: [...Object.values(CommonConfigs)],
}),
HealthModule,
UserModule,
],
controllers: [],
providers: [],
})
Expand Down
14 changes: 14 additions & 0 deletions apps/recnet-api/src/config/common.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { registerAs } from "@nestjs/config";

export const DBConfig = registerAs("db", () => ({
host: process.env.RDS_HOSTNAME || "localhost",
port: parseInt(process.env.RDS_PORT, 10) || 5432,
database: process.env.RDS_DB_NAME,
username: process.env.RDS_USERNAME,
password: process.env.RDS_PASSWORD,
}));

export const PrismaConfig = registerAs("prisma", () => ({
schema: process.env.PRISMA_SCHEMA || "prisma/schema.prisma",
migrate: process.env.DB_MIGRATE === "true",
}));
34 changes: 15 additions & 19 deletions apps/recnet-api/src/database/prisma/prisma.connection.provider.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
import { execSync } from "child_process";

import { Injectable, OnModuleInit } from "@nestjs/common";
import { Inject, Injectable, OnModuleInit } from "@nestjs/common";
import { ConfigType } from "@nestjs/config";
import { PrismaClient } from "@prisma/client";

const postgresConfig = {
host: process.env.RDS_HOSTNAME,
port: parseInt(process.env.RDS_PORT, 10),
database: process.env.RDS_DB_NAME,
username: process.env.RDS_USERNAME,
password: process.env.RDS_PASSWORD,
};
import { DBConfig, PrismaConfig } from "src/config/common.config";

@Injectable()
export class PrismaConnectionProvider
Expand All @@ -18,12 +12,16 @@ export class PrismaConnectionProvider
{
private prismaUrl: string;

constructor() {
const connectionUrl = `postgresql://${postgresConfig.username}:${encodeURIComponent(
postgresConfig.password
)}@${postgresConfig.host}:${postgresConfig.port}/${
postgresConfig.database
}?schema=recnet`;
constructor(
@Inject(DBConfig.KEY)
private dbConfig: ConfigType<typeof DBConfig>,
@Inject(PrismaConfig.KEY)
private prismaConfig: ConfigType<typeof PrismaConfig>
) {
const { host, port, database, username, password } = dbConfig;
const connectionUrl = `postgresql://${username}:${encodeURIComponent(
password
)}@${host}:${port}/${database}?schema=recnet`;

super({
datasources: {
Expand All @@ -39,11 +37,9 @@ export class PrismaConnectionProvider
async onModuleInit() {
await this.$connect();

const prismaSchema = process.env.PRISMA_SCHEMA || "prisma/schema.prisma";

if (process.env.DB_MIGRATE === "true") {
if (this.prismaConfig.migrate) {
execSync(
`export PRISMA_DATABASE_URL=${this.prismaUrl} && npx prisma migrate deploy --schema=${prismaSchema}`,
`export PRISMA_DATABASE_URL=${this.prismaUrl} && npx prisma migrate deploy --schema=${this.prismaConfig.schema}`,
{ stdio: "inherit" }
);
}
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"dependencies": {
"@hookform/resolvers": "^3.3.4",
"@nestjs/common": "^10.0.2",
"@nestjs/config": "^3.2.0",
"@nestjs/core": "^10.0.2",
"@nestjs/platform-express": "^10.0.2",
"@nestjs/swagger": "^7.3.0",
Expand Down Expand Up @@ -65,8 +66,8 @@
"@nx/js": "18.0.4",
"@nx/nest": "18.0.4",
"@nx/next": "18.0.4",
"@nx/vite": "18.0.4",
"@nx/node": "18.0.4",
"@nx/vite": "18.0.4",
"@nx/web": "18.0.4",
"@nx/webpack": "18.0.4",
"@nx/workspace": "18.0.4",
Expand Down
Loading

0 comments on commit 3f687c1

Please sign in to comment.