From ed04c97e4ff3ad9d4d484a625118cc3e2cb1bb6f Mon Sep 17 00:00:00 2001 From: pratham-outside Date: Mon, 23 Dec 2024 16:58:31 +0545 Subject: [PATCH 01/15] feat(fixes): sonar fixes --- sonar-project.properties | 1 + 1 file changed, 1 insertion(+) create mode 100644 sonar-project.properties diff --git a/sonar-project.properties b/sonar-project.properties new file mode 100644 index 0000000..78cf2b0 --- /dev/null +++ b/sonar-project.properties @@ -0,0 +1 @@ +sonar.exclusions= **/* \ No newline at end of file From 8d28b906003a9ab62bf4ef509d961c1519663739 Mon Sep 17 00:00:00 2001 From: pratham-outside Date: Mon, 23 Dec 2024 18:25:29 +0545 Subject: [PATCH 02/15] chore: added environment variables validation --- .env.example | 11 ++++++----- package.json | 3 ++- src/app.module.ts | 5 +++-- src/config/env.config.ts | 36 ++++++++++++++++++++++++++++++++++++ yarn.lock | 5 +++++ 5 files changed, 52 insertions(+), 8 deletions(-) create mode 100644 src/config/env.config.ts diff --git a/.env.example b/.env.example index e803eb5..063e29c 100644 --- a/.env.example +++ b/.env.example @@ -1,13 +1,14 @@ # Application APP_PORT=3000 -APP_ENV=development +NODE_ENV=development # development || production # Database DB_HOST=postgres -DB_PORT=5432 -DB_USER=your_db_user -DB_PASSWORD=your_db_password -DB_NAME=url_shortener +POSTGRES_PORT=5432 +POSTGRES_USER=your_db_user +POSTGRES_PASSWORD=your_db_password +POSTGRES_DB=url_shortener + # JWT JWT_SECRET=your_jwt_secret diff --git a/package.json b/package.json index 879834c..150c9b0 100644 --- a/package.json +++ b/package.json @@ -34,7 +34,8 @@ "pg": "^8.13.1", "reflect-metadata": "^0.2.0", "rxjs": "^7.8.1", - "typeorm": "^0.3.20" + "typeorm": "^0.3.20", + "zod": "^3.24.1" }, "devDependencies": { "@commitlint/cli": "^19.6.0", diff --git a/src/app.module.ts b/src/app.module.ts index 546aab3..c4e2f53 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -1,9 +1,10 @@ import { Module } from '@nestjs/common'; import { AppController } from './app.controller'; import { AppService } from './app.service'; - +import { validate } from './config/env.config'; +import { ConfigModule } from '@nestjs/config'; @Module({ - imports: [], + imports: [ConfigModule.forRoot({ isGlobal: true, validate })], controllers: [AppController], providers: [AppService], }) diff --git a/src/config/env.config.ts b/src/config/env.config.ts new file mode 100644 index 0000000..47cc071 --- /dev/null +++ b/src/config/env.config.ts @@ -0,0 +1,36 @@ +import { config } from 'dotenv'; +import { z } from 'zod'; +config(); +export const envVariables = { + POSTGRES_DB: process.env.POSTGRES_DB, + POSTGRES_USER: process.env.POSTGRES_USER, + POSTGRES_PASSWORD: process.env.POSTGRES_PASSWORD, + POSTGRES_PORT: +(process.env.POSTGRES_PORT || 5432), + DB_HOST: process.env.DB_HOST, + APP_PORT: +(process.env.APP_PORT || 3000), + NODE_ENV: process.env.NODE_ENV, +} as const; + +enum APP_ENVIRONVENT { + DEVELOPMENT = 'development', + PRODUCTION = 'production', +} +const validationSchema = z + .object({ + POSTGRES_DB: z.string().min(2, { message: 'Must be atleast 2 characters long' }), + POSTGRES_USER: z.string().min(2, { message: 'Must be atleast 2 characters long' }), + POSTGRES_PASSWORD: z.string().min(2, { message: 'Must be atleast 2 characters long' }), + POSTGRES_PORT: z.number().gt(0, { message: 'Port cannot be empty' }), + DB_HOST: z.string().min(2, { message: 'Must be atleast 2 characters long' }), + APP_PORT: z.number().gt(0, { message: 'Port cannot be empty' }), + NODE_ENV: z.nativeEnum(APP_ENVIRONVENT), + }) + .required(); + +export const validate = (): object => { + const value = validationSchema.safeParse(envVariables); + if (!value.success) { + throw new Error(`---${value.error.issues[0].path} :: ${value.error.issues[0].message.toUpperCase()}---`); + } + return value.data; +}; diff --git a/yarn.lock b/yarn.lock index f467074..b80c78f 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4375,3 +4375,8 @@ yocto-queue@^1.0.0: version "1.1.1" resolved "https://registry.yarnpkg.com/yocto-queue/-/yocto-queue-1.1.1.tgz#fef65ce3ac9f8a32ceac5a634f74e17e5b232110" integrity sha512-b4JR1PFR10y1mKjhHY9LaGo6tmrgjit7hxVIeAmyMw3jegXR4dhYqLaQF5zMXZxY7tLpMyJeLjr1C4rLmkVe8g== + +zod@^3.24.1: + version "3.24.1" + resolved "https://registry.yarnpkg.com/zod/-/zod-3.24.1.tgz#27445c912738c8ad1e9de1bea0359fa44d9d35ee" + integrity sha512-muH7gBL9sI1nciMZV67X5fTKKBLtwpZ5VBp1vsOQzj1MhrBZ4wlVCm3gedKZWLp0Oyel8sIGfeiz54Su+OVT+A== From a0e9c52ae981dfd0f2201e3e94a5e0c8899868ee Mon Sep 17 00:00:00 2001 From: pratham-outside Date: Tue, 24 Dec 2024 13:17:29 +0545 Subject: [PATCH 03/15] chore: add node env in dockercompose file --- docker-compose.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/docker-compose.yml b/docker-compose.yml index ce7baa6..86ca86d 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -30,6 +30,7 @@ services: POSTGRES_PASSWORD: ${POSTGRES_PASSWORD} POSTGRES_DB: ${POSTGRES_DB} APP_PORT: ${APP_PORT} + NODE_ENV: ${NODE_ENV} ports: - ${APP_PORT}:${APP_PORT} networks: From 351952d5fcf7ef70e9a5f9eff6097aea8f9ed415 Mon Sep 17 00:00:00 2001 From: pratham-outside Date: Tue, 24 Dec 2024 13:22:56 +0545 Subject: [PATCH 04/15] chore: change access of env variables from process.env to env.config file --- src/config/env.config.ts | 2 -- src/database/db.module.ts | 3 ++- src/main.ts | 8 ++++---- src/scripts/orm.config.ts | 9 +++++---- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/config/env.config.ts b/src/config/env.config.ts index 47cc071..3218fbe 100644 --- a/src/config/env.config.ts +++ b/src/config/env.config.ts @@ -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, diff --git a/src/database/db.module.ts b/src/database/db.module.ts index d3cc800..6fa540b 100644 --- a/src/database/db.module.ts +++ b/src/database/db.module.ts @@ -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; }, diff --git a/src/main.ts b/src/main.ts index daa3eeb..2374235 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,12 +1,12 @@ import { NestFactory } from '@nestjs/core'; import { AppModule } from './app.module'; +import { envVariables } from './config/env.config'; async function bootstrap(): Promise { 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}`); }); } diff --git a/src/scripts/orm.config.ts b/src/scripts/orm.config.ts index 0273d17..09c3c46 100644 --- a/src/scripts/orm.config.ts +++ b/src/scripts/orm.config.ts @@ -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}')], From 03d8a329d0e4653b9d89408fb20991eb9d39431e Mon Sep 17 00:00:00 2001 From: pratham-outside Date: Tue, 24 Dec 2024 13:27:52 +0545 Subject: [PATCH 05/15] chore: edit .env.example file --- .env.example | 8 -------- 1 file changed, 8 deletions(-) diff --git a/.env.example b/.env.example index 1489778..063e29c 100644 --- a/.env.example +++ b/.env.example @@ -3,20 +3,12 @@ APP_PORT=3000 NODE_ENV=development # development || production # Database -<<<<<<< HEAD DB_HOST=postgres POSTGRES_PORT=5432 POSTGRES_USER=your_db_user POSTGRES_PASSWORD=your_db_password POSTGRES_DB=url_shortener -======= -POSTGRES_USER=trainee -POSTGRES_PASSWORD=root9996 -POSTGRES_DB=url_shortener -POSTGRES_PORT= 5432 -DB_HOST= postgres ->>>>>>> main # JWT JWT_SECRET=your_jwt_secret From 58b33436fcd5bcd2333f2cafdcfd300115dab96d Mon Sep 17 00:00:00 2001 From: pratham-outside Date: Fri, 27 Dec 2024 12:38:49 +0545 Subject: [PATCH 06/15] chore: added environment variables and removed default value in main --- src/config/env.config.ts | 14 ++++++++++++++ src/main.ts | 2 +- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/config/env.config.ts b/src/config/env.config.ts index 3218fbe..85ff764 100644 --- a/src/config/env.config.ts +++ b/src/config/env.config.ts @@ -7,12 +7,22 @@ export const envVariables = { DB_HOST: process.env.DB_HOST, APP_PORT: +(process.env.APP_PORT || 3000), NODE_ENV: process.env.NODE_ENV, + EMAIL_HOST: process.env.EMAIL_HOST, + EMAIL_PASS: process.env.EMAIL_PASS, + EMAIL_USER: process.env.EMAIL_USER, + EMAIL_PORT: +(process.env.EMAIL_PORT || 587), } as const; enum APP_ENVIRONVENT { DEVELOPMENT = 'development', PRODUCTION = 'production', } + +enum SMTP_PORTS { + STANDARD = 25, + DEFAULT = 587, + TLS = 465, +} const validationSchema = z .object({ POSTGRES_DB: z.string().min(2, { message: 'Must be atleast 2 characters long' }), @@ -22,6 +32,10 @@ const validationSchema = z DB_HOST: z.string().min(2, { message: 'Must be atleast 2 characters long' }), APP_PORT: z.number().gt(0, { message: 'Port cannot be empty' }), NODE_ENV: z.nativeEnum(APP_ENVIRONVENT), + EMAIL_HOST: z.string().min(2, { message: 'Must be atleast 2 characters long' }), + EMAIL_PORT: z.nativeEnum(SMTP_PORTS), + EMAIL_USER: z.string().min(2, { message: 'Must be atleast 2 characters long' }), + EMAIL_PASS: z.string().min(2, { message: 'Must be atleast 2 characters long' }), }) .required(); diff --git a/src/main.ts b/src/main.ts index 2374235..90060af 100644 --- a/src/main.ts +++ b/src/main.ts @@ -4,7 +4,7 @@ import { envVariables } from './config/env.config'; async function bootstrap(): Promise { const app = await NestFactory.create(AppModule); - const port = envVariables.APP_PORT ?? 3000; + const port = envVariables.APP_PORT; await app.listen(port, () => { console.info(`Server listening at port ${port}`); }); From f1a5e7437c1268c9a75e517ca63aa246b650f4b5 Mon Sep 17 00:00:00 2001 From: pratham-outside Date: Sun, 29 Dec 2024 21:50:22 +0545 Subject: [PATCH 07/15] chore: added comment on env example --- .env.example | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.env.example b/.env.example index 063e29c..b119585 100644 --- a/.env.example +++ b/.env.example @@ -3,7 +3,7 @@ APP_PORT=3000 NODE_ENV=development # development || production # Database -DB_HOST=postgres +DB_HOST=postgres #should be localhost if db is running in docker and app is running in container POSTGRES_PORT=5432 POSTGRES_USER=your_db_user POSTGRES_PASSWORD=your_db_password From 52330536502ccd5e535dec24123eced76e3d52d8 Mon Sep 17 00:00:00 2001 From: pratham-outside Date: Sun, 29 Dec 2024 21:52:40 +0545 Subject: [PATCH 08/15] chore: updates on docker compose,package.json and yarn.lock file --- .gitignore | 2 ++ docker-compose.yml | 6 ++++++ package.json | 1 + yarn.lock | 2 +- 4 files changed, 10 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 4b56acf..2f80a18 100644 --- a/.gitignore +++ b/.gitignore @@ -54,3 +54,5 @@ pids # Diagnostic reports (https://nodejs.org/api/report.html) report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json + +ormconfig.json \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index 86ca86d..0f9c316 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -31,6 +31,12 @@ services: POSTGRES_DB: ${POSTGRES_DB} APP_PORT: ${APP_PORT} NODE_ENV: ${NODE_ENV} + EMAIL_SERVICE: ${EMAIL_SERVICE} + EMAIL_HOST: ${EMAIL_HOST} + EMAIL_PORT: ${EMAIL_PORT} + EMAIL_USER: ${EMAIL_USER} + EMAIL_PASS: ${EMAIL_PASS} + ports: - ${APP_PORT}:${APP_PORT} networks: diff --git a/package.json b/package.json index 74462c7..e6ddb68 100644 --- a/package.json +++ b/package.json @@ -36,6 +36,7 @@ "bcrypt": "^5.1.1", "class-transformer": "^0.5.1", "class-validator": "^0.14.1", + "dotenv": "^16.4.7", "pg": "^8.13.1", "reflect-metadata": "^0.2.0", "rxjs": "^7.8.1", diff --git a/yarn.lock b/yarn.lock index b80c78f..97d295c 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1623,7 +1623,7 @@ dotenv@16.4.5: resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-16.4.5.tgz#cdd3b3b604cb327e286b4762e13502f717cb099f" integrity sha512-ZmdL2rui+eB2YwhsWzjInR8LldtZHGDoQ1ugH85ppHKwpUHL7j7rN0Ti9NCnGiQbhaZ11FpR+7ao1dNsmduNUg== -dotenv@^16.0.3: +dotenv@^16.0.3, dotenv@^16.4.7: version "16.4.7" resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-16.4.7.tgz#0e20c5b82950140aa99be360a8a5f52335f53c26" integrity sha512-47qPchRCykZC03FhkYAhrvwU4xDBFIj1QPqaarj6mdM/hgUzfPHcpkHJOn3mJAufFeeAxAzeGsr5X0M4k6fLZQ== From 81a71f01061a7cf72935b32b3f1e1a83a5e705f9 Mon Sep 17 00:00:00 2001 From: pratham-outside Date: Sun, 29 Dec 2024 21:54:12 +0545 Subject: [PATCH 09/15] chore: added absolute aliases and renamed variable env --- src/app.controller.ts | 2 +- src/app.module.ts | 10 +++++----- src/database/db.module.ts | 6 +++--- src/main.ts | 8 ++++---- src/scripts/orm.config.ts | 12 ++++++------ 5 files changed, 19 insertions(+), 19 deletions(-) diff --git a/src/app.controller.ts b/src/app.controller.ts index 6509517..d97ed8c 100644 --- a/src/app.controller.ts +++ b/src/app.controller.ts @@ -1,5 +1,5 @@ import { Controller, Get } from '@nestjs/common'; -import { AppService } from './app.service'; +import { AppService } from '@/app.service'; @Controller() export class AppController { diff --git a/src/app.module.ts b/src/app.module.ts index 0d2a377..4c21cb5 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -1,9 +1,9 @@ -import { Module } from '@nestjs/common'; -import { AppController } from './app.controller'; -import { AppService } from './app.service'; -import { validate } from './config/env.config'; import { ConfigModule } from '@nestjs/config'; -import { DatabaseModule } from './database/db.module'; +import { Module } from '@nestjs/common'; +import { AppController } from '@/app.controller'; +import { AppService } from '@/app.service'; +import { validate } from '@/config/env.config'; +import { DatabaseModule } from '@/database/db.module'; @Module({ imports: [ConfigModule.forRoot({ isGlobal: true, validate }), DatabaseModule], diff --git a/src/database/db.module.ts b/src/database/db.module.ts index 6fa540b..0476a89 100644 --- a/src/database/db.module.ts +++ b/src/database/db.module.ts @@ -1,15 +1,15 @@ import { Module } from '@nestjs/common'; import { TypeOrmModule, TypeOrmModuleOptions } from '@nestjs/typeorm'; -import { dataBaseConfigurations } from 'src/scripts/orm.config'; +import { dataBaseConfigurations } from '@/scripts/orm.config'; import { DataSource, DataSourceOptions, TypeORMError } from 'typeorm'; -import { envVariables } from '@/config/env.config'; +import { env } from '@/config/env.config'; @Module({ imports: [ TypeOrmModule.forRootAsync({ useFactory: () => { return { - host: envVariables.DB_HOST, + host: env.DB_HOST, ...dataBaseConfigurations, } as TypeOrmModuleOptions; }, diff --git a/src/main.ts b/src/main.ts index 90060af..90b0f9a 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,12 +1,12 @@ import { NestFactory } from '@nestjs/core'; -import { AppModule } from './app.module'; -import { envVariables } from './config/env.config'; +import { AppModule } from '@/app.module'; +import { env } from '@/config/env.config'; async function bootstrap(): Promise { const app = await NestFactory.create(AppModule); - const port = envVariables.APP_PORT; + const port = env.APP_PORT; await app.listen(port, () => { - console.info(`Server listening at port ${port}`); + console.info(`${env.NODE_ENV?.toLocaleUpperCase()} Server listening at port ${port}`); }); } diff --git a/src/scripts/orm.config.ts b/src/scripts/orm.config.ts index 09c3c46..4a9b655 100644 --- a/src/scripts/orm.config.ts +++ b/src/scripts/orm.config.ts @@ -1,15 +1,15 @@ import { writeFile } from 'fs/promises'; import { join } from 'path'; import { DataSource, DataSourceOptions } from 'typeorm'; -import { envVariables } from '@/config/env.config'; +import { env, APP_ENVIRONVENT } from '@/config/env.config'; const dataBaseConfigurations = { type: 'postgres', - 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 + port: +env.POSTGRES_PORT, + username: env.POSTGRES_USER, + password: env.POSTGRES_PASSWORD, + database: env.POSTGRES_DB, + synchronize: env.NODE_ENV === APP_ENVIRONVENT.PRODUCTION ? false : true, // Should be false in production to use migrations logging: true, entities: [join(__dirname, '/../entities', '*.entity.{ts,js}')], migrations: [join(__dirname, '/../migrations', '*.{ts,js}')], From b3e172f3316ab5624222bcdf8f5f3202d4867980 Mon Sep 17 00:00:00 2001 From: pratham-outside Date: Sun, 29 Dec 2024 21:56:14 +0545 Subject: [PATCH 10/15] chore: added validation schema and imported dotenv --- src/config/env.config.ts | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/config/env.config.ts b/src/config/env.config.ts index 85ff764..2b38f5c 100644 --- a/src/config/env.config.ts +++ b/src/config/env.config.ts @@ -1,5 +1,7 @@ import { z } from 'zod'; -export const envVariables = { +import { config } from 'dotenv'; +config(); +export const env = { POSTGRES_DB: process.env.POSTGRES_DB, POSTGRES_USER: process.env.POSTGRES_USER, POSTGRES_PASSWORD: process.env.POSTGRES_PASSWORD, @@ -13,7 +15,7 @@ export const envVariables = { EMAIL_PORT: +(process.env.EMAIL_PORT || 587), } as const; -enum APP_ENVIRONVENT { +export enum APP_ENVIRONVENT { DEVELOPMENT = 'development', PRODUCTION = 'production', } @@ -39,8 +41,10 @@ const validationSchema = z }) .required(); -export const validate = (): object => { - const value = validationSchema.safeParse(envVariables); +type ValidationSchema = z.infer; + +export const validate = (): ValidationSchema => { + const value = validationSchema.safeParse(env); if (!value.success) { throw new Error(`---${value.error.issues[0].path} :: ${value.error.issues[0].message.toUpperCase()}---`); } From 1976ee8f5802bf39e0e0aac6802efa9f8d87a9d6 Mon Sep 17 00:00:00 2001 From: pratham-outside Date: Mon, 30 Dec 2024 10:34:43 +0545 Subject: [PATCH 11/15] chore: removed dotenv import --- src/config/env.config.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/config/env.config.ts b/src/config/env.config.ts index 2b38f5c..4596b64 100644 --- a/src/config/env.config.ts +++ b/src/config/env.config.ts @@ -1,6 +1,4 @@ import { z } from 'zod'; -import { config } from 'dotenv'; -config(); export const env = { POSTGRES_DB: process.env.POSTGRES_DB, POSTGRES_USER: process.env.POSTGRES_USER, From 687240524e94748bbb365debd24e207af931e25f Mon Sep 17 00:00:00 2001 From: pratham-outside Date: Tue, 31 Dec 2024 10:56:52 +0545 Subject: [PATCH 12/15] fix: typo --- src/scripts/orm.config.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/scripts/orm.config.ts b/src/scripts/orm.config.ts index 4a9b655..88c75cd 100644 --- a/src/scripts/orm.config.ts +++ b/src/scripts/orm.config.ts @@ -9,7 +9,7 @@ const dataBaseConfigurations = { username: env.POSTGRES_USER, password: env.POSTGRES_PASSWORD, database: env.POSTGRES_DB, - synchronize: env.NODE_ENV === APP_ENVIRONVENT.PRODUCTION ? false : true, // Should be false in production to use migrations + synchronize: env.NODE_ENV === APP_ENVIRONVENT.DEVELOPMENT, // Should be false in production to use migrations logging: true, entities: [join(__dirname, '/../entities', '*.entity.{ts,js}')], migrations: [join(__dirname, '/../migrations', '*.{ts,js}')], From 02a5139bf13f24eea4d17e548d93c5013b4f061c Mon Sep 17 00:00:00 2001 From: pratham-outside Date: Tue, 31 Dec 2024 14:16:54 +0545 Subject: [PATCH 13/15] chore: seperated orm configurations --- .gitignore | 2 -- ormconfig.json | 15 +++++++++++++++ src/config/env.config.ts | 2 ++ src/database/db.config.ts | 16 ++++++++++++++++ src/database/db.module.ts | 2 +- src/scripts/orm.config.ts | 18 +++--------------- 6 files changed, 37 insertions(+), 18 deletions(-) create mode 100644 ormconfig.json create mode 100644 src/database/db.config.ts diff --git a/.gitignore b/.gitignore index 2f80a18..4b56acf 100644 --- a/.gitignore +++ b/.gitignore @@ -54,5 +54,3 @@ pids # Diagnostic reports (https://nodejs.org/api/report.html) report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json - -ormconfig.json \ No newline at end of file diff --git a/ormconfig.json b/ormconfig.json new file mode 100644 index 0000000..30d9d22 --- /dev/null +++ b/ormconfig.json @@ -0,0 +1,15 @@ +{ + "type": "postgres", + "port": 5432, + "username": "trainee", + "password": "root9996", + "database": "url_shortener", + "synchronize": true, + "logging": true, + "entities": [ + "/home/sanzay/Outside/product-traineeship-2024-shortner/src/entities/*.entity.{ts,js}" + ], + "migrations": [ + "/home/sanzay/Outside/product-traineeship-2024-shortner/src/migrations/*.{ts,js}" + ] +} \ No newline at end of file diff --git a/src/config/env.config.ts b/src/config/env.config.ts index 4596b64..2b38f5c 100644 --- a/src/config/env.config.ts +++ b/src/config/env.config.ts @@ -1,4 +1,6 @@ import { z } from 'zod'; +import { config } from 'dotenv'; +config(); export const env = { POSTGRES_DB: process.env.POSTGRES_DB, POSTGRES_USER: process.env.POSTGRES_USER, diff --git a/src/database/db.config.ts b/src/database/db.config.ts new file mode 100644 index 0000000..8ac05cc --- /dev/null +++ b/src/database/db.config.ts @@ -0,0 +1,16 @@ +import { env, APP_ENVIRONVENT } from '@/config/env.config'; +import { join } from 'path'; +import { config } from 'dotenv'; +config(); + +export const dataBaseConfigurations = { + type: 'postgres', + port: +env.POSTGRES_PORT, + username: env.POSTGRES_USER, + password: env.POSTGRES_PASSWORD, + database: env.POSTGRES_DB, + synchronize: env.NODE_ENV === APP_ENVIRONVENT.DEVELOPMENT, // Should be false in production to use migrations + logging: true, + entities: [join(__dirname, '/../entities', '*.entity.{ts,js}')], + migrations: [join(__dirname, '/../migrations', '*.{ts,js}')], +}; diff --git a/src/database/db.module.ts b/src/database/db.module.ts index 0476a89..8de75bf 100644 --- a/src/database/db.module.ts +++ b/src/database/db.module.ts @@ -1,6 +1,6 @@ import { Module } from '@nestjs/common'; import { TypeOrmModule, TypeOrmModuleOptions } from '@nestjs/typeorm'; -import { dataBaseConfigurations } from '@/scripts/orm.config'; +import { dataBaseConfigurations } from '@/database/db.config'; import { DataSource, DataSourceOptions, TypeORMError } from 'typeorm'; import { env } from '@/config/env.config'; diff --git a/src/scripts/orm.config.ts b/src/scripts/orm.config.ts index 88c75cd..d7e615b 100644 --- a/src/scripts/orm.config.ts +++ b/src/scripts/orm.config.ts @@ -1,20 +1,8 @@ import { writeFile } from 'fs/promises'; -import { join } from 'path'; import { DataSource, DataSourceOptions } from 'typeorm'; -import { env, APP_ENVIRONVENT } from '@/config/env.config'; +import { dataBaseConfigurations } from '@/database/db.config'; -const dataBaseConfigurations = { - type: 'postgres', - port: +env.POSTGRES_PORT, - username: env.POSTGRES_USER, - password: env.POSTGRES_PASSWORD, - database: env.POSTGRES_DB, - synchronize: env.NODE_ENV === APP_ENVIRONVENT.DEVELOPMENT, // Should be false in production to use migrations - logging: true, - entities: [join(__dirname, '/../entities', '*.entity.{ts,js}')], - migrations: [join(__dirname, '/../migrations', '*.{ts,js}')], -}; const dataSource = new DataSource(dataBaseConfigurations as DataSourceOptions); - +console.log(dataBaseConfigurations); writeFile('ormconfig.json', JSON.stringify(dataSource.options, null, 2)); -export { dataSource, dataBaseConfigurations }; +export { dataSource }; From 4d17f877c79302c5c81e186ce669ac803d3a1433 Mon Sep 17 00:00:00 2001 From: pratham-outside Date: Tue, 31 Dec 2024 14:20:35 +0545 Subject: [PATCH 14/15] fix: changed names from validation schema to env schema --- src/config/env.config.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/config/env.config.ts b/src/config/env.config.ts index 2b38f5c..5f76f8b 100644 --- a/src/config/env.config.ts +++ b/src/config/env.config.ts @@ -25,7 +25,7 @@ enum SMTP_PORTS { DEFAULT = 587, TLS = 465, } -const validationSchema = z +const envSchema = z .object({ POSTGRES_DB: z.string().min(2, { message: 'Must be atleast 2 characters long' }), POSTGRES_USER: z.string().min(2, { message: 'Must be atleast 2 characters long' }), @@ -41,10 +41,10 @@ const validationSchema = z }) .required(); -type ValidationSchema = z.infer; +type EnvSchema = z.infer; -export const validate = (): ValidationSchema => { - const value = validationSchema.safeParse(env); +export const validate = (): EnvSchema => { + const value = envSchema.safeParse(env); if (!value.success) { throw new Error(`---${value.error.issues[0].path} :: ${value.error.issues[0].message.toUpperCase()}---`); } From 5a59750c1c4aaaaf59ded7cb210f07be48800614 Mon Sep 17 00:00:00 2001 From: pratham-outside Date: Thu, 2 Jan 2025 10:31:14 +0545 Subject: [PATCH 15/15] chore: fixed type annotations --- src/database/db.config.ts | 3 ++- src/database/db.module.ts | 4 ++-- src/scripts/orm.config.ts | 5 ++--- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/database/db.config.ts b/src/database/db.config.ts index 8ac05cc..8c7447a 100644 --- a/src/database/db.config.ts +++ b/src/database/db.config.ts @@ -1,9 +1,10 @@ import { env, APP_ENVIRONVENT } from '@/config/env.config'; import { join } from 'path'; import { config } from 'dotenv'; +import { DataSourceOptions } from 'typeorm'; config(); -export const dataBaseConfigurations = { +export const dataBaseConfigurations: DataSourceOptions = { type: 'postgres', port: +env.POSTGRES_PORT, username: env.POSTGRES_USER, diff --git a/src/database/db.module.ts b/src/database/db.module.ts index 8de75bf..0c5db8c 100644 --- a/src/database/db.module.ts +++ b/src/database/db.module.ts @@ -1,5 +1,5 @@ import { Module } from '@nestjs/common'; -import { TypeOrmModule, TypeOrmModuleOptions } from '@nestjs/typeorm'; +import { TypeOrmModule } from '@nestjs/typeorm'; import { dataBaseConfigurations } from '@/database/db.config'; import { DataSource, DataSourceOptions, TypeORMError } from 'typeorm'; import { env } from '@/config/env.config'; @@ -11,7 +11,7 @@ import { env } from '@/config/env.config'; return { host: env.DB_HOST, ...dataBaseConfigurations, - } as TypeOrmModuleOptions; + }; }, dataSourceFactory: async (options: DataSourceOptions) => { try { diff --git a/src/scripts/orm.config.ts b/src/scripts/orm.config.ts index d7e615b..f7255d0 100644 --- a/src/scripts/orm.config.ts +++ b/src/scripts/orm.config.ts @@ -1,8 +1,7 @@ import { writeFile } from 'fs/promises'; -import { DataSource, DataSourceOptions } from 'typeorm'; +import { DataSource } from 'typeorm'; import { dataBaseConfigurations } from '@/database/db.config'; -const dataSource = new DataSource(dataBaseConfigurations as DataSourceOptions); -console.log(dataBaseConfigurations); +const dataSource = new DataSource(dataBaseConfigurations); writeFile('ormconfig.json', JSON.stringify(dataSource.options, null, 2)); export { dataSource };