fix: the app was crashing when multiple connection where required. I replaced pg.Client by pg.Pool, it should enhanced the usability and durability
continuous-integration/drone/push Build is passing Details

pull/9/head
Rémi REGNAULT 1 year ago
parent 78973cfcc9
commit d84748f08e

@ -1,12 +1,11 @@
import { Client } from "pg" import { Pool, PoolClient } from "pg"
export class Connection { export class Connection {
public client:Client private pool:Pool
clientIsConnected:boolean = false
constructor() { constructor() {
this.client = new Client({ this.pool = new Pool({
user: process.env.DB_USERNAME, user: process.env.DB_USERNAME,
host: process.env.DB_DBHOST, host: process.env.DB_DBHOST,
database: process.env.DB_DBNAME, database: process.env.DB_DBNAME,
@ -15,10 +14,7 @@ export class Connection {
}) })
} }
public async connect() { public async getPoolClient() : Promise<PoolClient> {
if (!this.clientIsConnected) { return await this.pool.connect()
await this.client.connect()
this.clientIsConnected = true
}
} }
} }

@ -9,9 +9,11 @@ export class IngredientsGateway {
} }
async getAll() : Promise<Ingredient[]> { async getAll() : Promise<Ingredient[]> {
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[] = [] let ingredients:Ingredient[] = []
@ -24,14 +26,16 @@ export class IngredientsGateway {
} }
async findOneById(id: number) : Promise<Ingredient | null> { async findOneById(id: number) : Promise<Ingredient | null> {
this.connection.connect() const client = await this.connection.getPoolClient()
const query = { const query = {
text: 'SELECT * FROM Ingredients WHERE id =$1', text: 'SELECT * FROM Ingredients WHERE id =$1',
values: [id], values: [id],
} }
const res = await this.connection.client.query(query) const res = await client.query(query)
client.release()
if (res.rowCount != 1) { if (res.rowCount != 1) {
return null return null
@ -43,15 +47,16 @@ export class IngredientsGateway {
} }
async findIngredientsForRecipe(id: number): Promise<any> { async findIngredientsForRecipe(id: number): Promise<any> {
this.connection.connect(); const client = await this.connection.getPoolClient()
const query = { const query = {
text: 'SELECT i.name, i.id FROM Ingredients i, Composed c WHERE c.idRecipe =$1 AND i.id = c.idIngredient', text: 'SELECT i.name, i.id FROM Ingredients i, Composed c WHERE c.idRecipe =$1 AND i.id = c.idIngredient',
values: [id], values: [id],
}; };
const res = await this.connection.client.query(query); const res = await client.query(query);
console.log(res)
client.release()
if (res.rowCount === 0) { if (res.rowCount === 0) {
return null; return null;
@ -62,7 +67,6 @@ export class IngredientsGateway {
name: row.name, name: row.name,
id: Number(row.id), // Conversion de l'identifiant en nombre id: Number(row.id), // Conversion de l'identifiant en nombre
})); }));
console.log(ingredients);
return ingredients as Ingredient[]; return ingredients as Ingredient[];
} }

@ -15,8 +15,9 @@ export class RecipeGateway {
} }
async getAll() : Promise<Recipe[]> { async getAll() : Promise<Recipe[]> {
this.connection.connect() const client = await this.connection.getPoolClient()
const res = await this.connection.client.query('SELECT * FROM Recipes ORDER BY id'); const res = await client.query('SELECT * FROM Recipes ORDER BY id');
client.release()
const steps: string[] = []; const steps: string[] = [];
let recipes: Recipe[] = [] let recipes: Recipe[] = []
@ -28,20 +29,20 @@ export class RecipeGateway {
recipes.push(recipe); recipes.push(recipe);
} }
console.log(recipes);
return recipes return recipes
} }
async getById(id: number) : Promise<Recipe | null>{ async getById(id: number) : Promise<Recipe | null>{
this.connection.connect() const client = await this.connection.getPoolClient()
const query = { const query = {
text: 'SELECT * FROM Recipes WHERE id =$1', text: 'SELECT * FROM Recipes WHERE id =$1',
values: [id], values: [id],
} }
const res = await this.connection.client.query(query) const res = await client.query(query)
client.release()
if (res.rowCount != 1) { if (res.rowCount != 1) {
return null return null

@ -9,14 +9,16 @@ export class StepsGateway {
async getForRecipes(id: number): Promise<string[]> { async getForRecipes(id: number): Promise<string[]> {
this.connection.connect(); const client = await this.connection.getPoolClient()
const query = { const query = {
text: 'SELECT action FROM Steps WHERE idRecipe = $1 ORDER BY numstep', text: 'SELECT action FROM Steps WHERE idRecipe = $1 ORDER BY numstep',
values: [id], 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); const steps = res.rows.map(row => row.action);

Loading…
Cancel
Save