From ababfc7a333250613d4d2b420b1fd039772c3735 Mon Sep 17 00:00:00 2001 From: Remi REGNAULT Date: Wed, 29 Nov 2023 10:26:15 +0100 Subject: [PATCH] feat: class connection will use from now the env variable --- API-Project/package-lock.json | 12 +++++++++++ API-Project/package.json | 7 +++---- .../src/controllers/ingredients.controller.ts | 5 +---- .../src/controllers/steps.controller.ts | 1 - API-Project/src/database/connection.ts | 20 +++++-------------- API-Project/src/gateways/steps.gateway.ts | 1 - API-Project/src/server.ts | 3 +++ 7 files changed, 24 insertions(+), 25 deletions(-) diff --git a/API-Project/package-lock.json b/API-Project/package-lock.json index 9ff34b9..0b56a9f 100644 --- a/API-Project/package-lock.json +++ b/API-Project/package-lock.json @@ -11,6 +11,7 @@ "dependencies": { "@types/express": "^4.17.21", "@types/morgan": "^1.9.9", + "dotenv": "^16.3.1", "express": "^4.18.2", "morgan": "^1.10.0", "nodemon": "^3.0.1", @@ -2237,6 +2238,17 @@ "node": ">=8" } }, + "node_modules/dotenv": { + "version": "16.3.1", + "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.3.1.tgz", + "integrity": "sha512-IPzF4w4/Rd94bA9imS68tZBaYyBWSCE47V1RGuMrB94iyTOIEwRmVL2x/4An+6mETpLrKJ5hQkB8W4kFAadeIQ==", + "engines": { + "node": ">=12" + }, + "funding": { + "url": "https://github.com/motdotla/dotenv?sponsor=1" + } + }, "node_modules/ee-first": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz", diff --git a/API-Project/package.json b/API-Project/package.json index 9ed2661..1e01c17 100644 --- a/API-Project/package.json +++ b/API-Project/package.json @@ -8,12 +8,13 @@ "build": "tsup src/server.ts --format cjs --clean", "dev": "nodemon --watch src -e js,ts,json --exec \"ts-node src/server.ts\"", "test": "jest --passWithNoTests" - }, + }, "author": "", "license": "ISC", "dependencies": { "@types/express": "^4.17.21", "@types/morgan": "^1.9.9", + "dotenv": "^16.3.1", "express": "^4.18.2", "morgan": "^1.10.0", "nodemon": "^3.0.1", @@ -23,11 +24,9 @@ "typescript": "^5.2.2" }, "devDependencies": { - - "@types/pg": "^8.10.9", "@types/jest": "^29.5.8", + "@types/pg": "^8.10.9", "jest": "^29.7.0", "ts-jest": "^29.1.1" - } } diff --git a/API-Project/src/controllers/ingredients.controller.ts b/API-Project/src/controllers/ingredients.controller.ts index 16ea131..4a99322 100644 --- a/API-Project/src/controllers/ingredients.controller.ts +++ b/API-Project/src/controllers/ingredients.controller.ts @@ -1,9 +1,6 @@ -import { Request, Response, NextFunction } from "express"; import { Router } from "express"; import { Exceptions } from "../utils/exception"; -import { IIngredient, Ingredient } from "../types/ingredients"; -import { pool } from "../database/connection"; -import { Query, QueryResult } from "pg"; +import { Ingredient } from "../types/ingredients"; import { IngredientsGateway } from "../gateways/ingredients.gateway"; diff --git a/API-Project/src/controllers/steps.controller.ts b/API-Project/src/controllers/steps.controller.ts index e2bc432..cf3612c 100644 --- a/API-Project/src/controllers/steps.controller.ts +++ b/API-Project/src/controllers/steps.controller.ts @@ -1,5 +1,4 @@ import { Router } from "express"; -import { Recipe } from "../types/recipes"; import { Exceptions } from "../utils/exception"; import { StepsGateway } from "../gateways/steps.gateway"; diff --git a/API-Project/src/database/connection.ts b/API-Project/src/database/connection.ts index 55c586b..2dadde3 100644 --- a/API-Project/src/database/connection.ts +++ b/API-Project/src/database/connection.ts @@ -1,15 +1,5 @@ import { Client } from "pg" -const Pool = require('pg').Pool - -export const pool = new Pool({ - user: 'rgregnault', - host: 'localhost', - database: 'leftovers', - password: 'motdepasse', - port: 5432, -}) - export class Connection { public client:Client @@ -17,11 +7,11 @@ export class Connection { constructor() { this.client = new Client({ - user: 'leftovers_appuser', - host: 'postgresql-leftovers.alwaysdata.net', - database: 'leftovers_recipedb', - password: 'UsrPsswd', - port: 5432, + user: process.env.DB_USERNAME, + host: process.env.DB_DBHOST, + database: process.env.DB_DBNAME, + password: process.env.DB_USERPASSWORD, + port: Number(process.env.DB_PORT), }) } diff --git a/API-Project/src/gateways/steps.gateway.ts b/API-Project/src/gateways/steps.gateway.ts index 0371b05..99897ab 100644 --- a/API-Project/src/gateways/steps.gateway.ts +++ b/API-Project/src/gateways/steps.gateway.ts @@ -1,4 +1,3 @@ -import { Recipe } from "../types/recipes" import { Connection } from "../database/connection" export class StepsGateway { diff --git a/API-Project/src/server.ts b/API-Project/src/server.ts index 9da9eb0..99cb44d 100644 --- a/API-Project/src/server.ts +++ b/API-Project/src/server.ts @@ -1,4 +1,7 @@ import express from "express"; + +require('dotenv').config(); + import { IngredientsController } from "./controllers/ingredients.controller"; import { RecipesController } from "./controllers/recipes.controller"; import { StepsController } from "./controllers/steps.controller";