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 {
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<PoolClient> {
return await this.pool.connect()
}
}

@ -9,9 +9,11 @@ export class IngredientsGateway {
}
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[] = []
@ -24,14 +26,16 @@ export class IngredientsGateway {
}
async findOneById(id: number) : Promise<Ingredient | null> {
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<any> {
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[];
}

@ -15,8 +15,9 @@ export class RecipeGateway {
}
async getAll() : Promise<Recipe[]> {
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<Recipe | null>{
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

@ -9,14 +9,16 @@ export class StepsGateway {
async getForRecipes(id: number): Promise<string[]> {
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);

Loading…
Cancel
Save