Skip to content

Commit

Permalink
chore: change access of env variables from process.env to env.config …
Browse files Browse the repository at this point in the history
…file
  • Loading branch information
pratham-outside committed Dec 24, 2024
1 parent a0e9c52 commit 351952d
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 11 deletions.
2 changes: 0 additions & 2 deletions src/config/env.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { config } from 'dotenv';
import { z } from 'zod';
config();
export const envVariables = {
POSTGRES_DB: process.env.POSTGRES_DB,
POSTGRES_USER: process.env.POSTGRES_USER,
Expand Down
3 changes: 2 additions & 1 deletion src/database/db.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ import { Module } from '@nestjs/common';
import { TypeOrmModule, TypeOrmModuleOptions } from '@nestjs/typeorm';
import { dataBaseConfigurations } from 'src/scripts/orm.config';
import { DataSource, DataSourceOptions, TypeORMError } from 'typeorm';
import { envVariables } from '@/config/env.config';

@Module({
imports: [
TypeOrmModule.forRootAsync({
useFactory: () => {
return {
host: process.env.DB_HOST,
host: envVariables.DB_HOST,
...dataBaseConfigurations,
} as TypeOrmModuleOptions;
},
Expand Down
8 changes: 4 additions & 4 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { envVariables } from './config/env.config';

async function bootstrap(): Promise<void> {
const app = await NestFactory.create(AppModule);

await app.listen(process.env.APP_PORT ?? 3000, () => {
console.info('Listening to server....');
console.info(`Server listening at port http://localhost:${process.env.APP_PORT}`);
const port = envVariables.APP_PORT ?? 3000;
await app.listen(port, () => {
console.info(`Server listening at port ${port}`);
});
}

Expand Down
9 changes: 5 additions & 4 deletions src/scripts/orm.config.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { writeFile } from 'fs/promises';
import { join } from 'path';
import { DataSource, DataSourceOptions } from 'typeorm';
import { envVariables } from '@/config/env.config';

const dataBaseConfigurations = {
type: 'postgres',
port: +(process.env.POSTGRES_PORT ?? '5432'),
username: process.env.POSTGRES_USER,
password: process.env.POSTGRES_PASSWORD,
database: process.env.POSTGRES_DB,
port: +(envVariables.POSTGRES_PORT ?? '5432'),
username: envVariables.POSTGRES_USER,
password: envVariables.POSTGRES_PASSWORD,
database: envVariables.POSTGRES_DB,
synchronize: false, // Should be false in production to use migrations
logging: true,
entities: [join(__dirname, '/../entities', '*.entity.{ts,js}')],
Expand Down

0 comments on commit 351952d

Please sign in to comment.