feat: create a client factory and an request example for the db

This commit is contained in:
Nemo D'ACREMONT 2024-10-25 11:04:47 +02:00
parent f0b04ff710
commit b16d648fbd
No known key found for this signature in database
GPG Key ID: 6E5BCE8022FA8276
2 changed files with 39 additions and 0 deletions

View File

@ -0,0 +1,23 @@
import pg from 'pg';
const db_user = process.env.DB_USER ?? "admin";
const db_password = process.env.DB_PASSWORD ?? "admin";
const db_name = process.env.DB_NAME ?? "briques";
const db_host = process.env.DB_HOST ?? "localhost";
const db_port = parseInt(process.env.DB_PORT ?? "5432");
console.log("=== DB CONFIG ===");
console.log(`DB_USER:\t\t${db_user}`);
console.log(`DB_PASSWORD:\t${db_user}`);
console.log(`DB_NAME:\t\t${db_name}`);
console.log(`DB_HOST:\t\t${db_host}`);
console.log(`DB_PORT:\t\t${db_port}`);
export const new_client = () => new pg.Client({
user: db_user,
password: db_password,
database: db_name,
host: db_host,
port: db_port,
});

16
back-end/src/db/index.ts Normal file
View File

@ -0,0 +1,16 @@
import { new_client } from './db_client';
async function example_request() {
const client = new_client();
await client.connect();
const res = await client.query('SELECT $1::text as message', ['Hello world!']);
const message = res.rows[0].message; // Hello world!
await client.end();
return message;
}
export default {
example_request,
};