feat: create a client factory and an request example for the db
This commit is contained in:
parent
f0b04ff710
commit
b16d648fbd
23
back-end/src/db/db_client.ts
Normal file
23
back-end/src/db/db_client.ts
Normal 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
16
back-end/src/db/index.ts
Normal 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,
|
||||
};
|
Loading…
x
Reference in New Issue
Block a user