From d84748f08ed22e78990cdc5b0f01f246ebf74a82 Mon Sep 17 00:00:00 2001 From: Remi REGNAULT Date: Wed, 29 Nov 2023 15:24:31 +0100 Subject: [PATCH] fix: the app was crashing when multiple connection where required. I replaced pg.Client by pg.Pool, it should enhanced the usability and durability --- API-Project/src/database/connection.ts | 14 +++++-------- .../src/gateways/ingredients.gateway.ts | 20 +++++++++++-------- API-Project/src/gateways/recipe.gateway.ts | 13 ++++++------ API-Project/src/gateways/steps.gateway.ts | 6 ++++-- 4 files changed, 28 insertions(+), 25 deletions(-) diff --git a/API-Project/src/database/connection.ts b/API-Project/src/database/connection.ts index 2dadde3..7c702d4 100644 --- a/API-Project/src/database/connection.ts +++ b/API-Project/src/database/connection.ts @@ -1,12 +1,11 @@ -import { Client } from "pg" +import { Pool, PoolClient } from "pg" export class Connection { - public client:Client - clientIsConnected:boolean = false + private pool:Pool constructor() { - this.client = new Client({ + this.pool = new Pool({ user: process.env.DB_USERNAME, host: process.env.DB_DBHOST, database: process.env.DB_DBNAME, @@ -15,10 +14,7 @@ export class Connection { }) } - public async connect() { - if (!this.clientIsConnected) { - await this.client.connect() - this.clientIsConnected = true - } + public async getPoolClient() : Promise { + return await this.pool.connect() } } \ No newline at end of file diff --git a/API-Project/src/gateways/ingredients.gateway.ts b/API-Project/src/gateways/ingredients.gateway.ts index 6e282d8..22a8e06 100644 --- a/API-Project/src/gateways/ingredients.gateway.ts +++ b/API-Project/src/gateways/ingredients.gateway.ts @@ -9,9 +9,11 @@ export class IngredientsGateway { } async getAll() : Promise { - this.connection.connect() + const client = await this.connection.getPoolClient() - const res = await this.connection.client.query('SELECT * FROM Ingredients ORDER BY id') + const res = await client.query('SELECT * FROM Ingredients ORDER BY id') + + client.release() let ingredients:Ingredient[] = [] @@ -24,14 +26,16 @@ export class IngredientsGateway { } async findOneById(id: number) : Promise { - this.connection.connect() + const client = await this.connection.getPoolClient() const query = { text: 'SELECT * FROM Ingredients WHERE id =$1', values: [id], } - const res = await this.connection.client.query(query) + const res = await client.query(query) + + client.release() if (res.rowCount != 1) { return null @@ -43,15 +47,16 @@ export class IngredientsGateway { } async findIngredientsForRecipe(id: number): Promise { - this.connection.connect(); + const client = await this.connection.getPoolClient() const query = { text: 'SELECT i.name, i.id FROM Ingredients i, Composed c WHERE c.idRecipe =$1 AND i.id = c.idIngredient', values: [id], }; - const res = await this.connection.client.query(query); - console.log(res) + const res = await client.query(query); + + client.release() if (res.rowCount === 0) { return null; @@ -62,7 +67,6 @@ export class IngredientsGateway { name: row.name, id: Number(row.id), // Conversion de l'identifiant en nombre })); - console.log(ingredients); return ingredients as Ingredient[]; } diff --git a/API-Project/src/gateways/recipe.gateway.ts b/API-Project/src/gateways/recipe.gateway.ts index cb9087d..6e99f61 100644 --- a/API-Project/src/gateways/recipe.gateway.ts +++ b/API-Project/src/gateways/recipe.gateway.ts @@ -15,8 +15,9 @@ export class RecipeGateway { } async getAll() : Promise { - this.connection.connect() - const res = await this.connection.client.query('SELECT * FROM Recipes ORDER BY id'); + const client = await this.connection.getPoolClient() + const res = await client.query('SELECT * FROM Recipes ORDER BY id'); + client.release() const steps: string[] = []; let recipes: Recipe[] = [] @@ -28,20 +29,20 @@ export class RecipeGateway { recipes.push(recipe); } - console.log(recipes); - return recipes } async getById(id: number) : Promise{ - this.connection.connect() + const client = await this.connection.getPoolClient() const query = { text: 'SELECT * FROM Recipes WHERE id =$1', values: [id], } - const res = await this.connection.client.query(query) + const res = await client.query(query) + + client.release() if (res.rowCount != 1) { return null diff --git a/API-Project/src/gateways/steps.gateway.ts b/API-Project/src/gateways/steps.gateway.ts index ddeae1b..36a065b 100644 --- a/API-Project/src/gateways/steps.gateway.ts +++ b/API-Project/src/gateways/steps.gateway.ts @@ -9,14 +9,16 @@ export class StepsGateway { async getForRecipes(id: number): Promise { - this.connection.connect(); + const client = await this.connection.getPoolClient() const query = { text: 'SELECT action FROM Steps WHERE idRecipe = $1 ORDER BY numstep', values: [id], }; - const res = await this.connection.client.query(query); + const res = await client.query(query); + + client.release() const steps = res.rows.map(row => row.action);