You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1.6 KiB

import mariadb from 'mariadb';
import moment from 'moment';
export default class DbManager {
constructor(host, user, password, port, name) {
this.pool = mariadb.createPool({
host: host,
port: port,
user: user,
password: password,
database: name,
connectionLimit: 5,
});
}
async conn() {
return await this.pool.getConnection();
}
async getClients() {
let conn = await this.conn();
try {
const rows = await conn.query('SELECT * FROM client');
rows.map(x => x.date_naissance = moment(x.date_naissance, 'YYYY-MM-DD HH:mm:ss').format('DD/MM/YYYY'));
return rows;
} catch (e) {
console.log(e)
return null;
}
}
async insertClient(client) {
let conn = await this.conn();
try {
const rows = await conn.query('INSERT INTO CLIENT(nom, prenom, email, date_naissance, pays, ville, code_postal) VALUES(?,?,?,?,?,?,?)',
[client.nom, client.prenom, client.email, moment(client.date_naissance, 'DD/MM/YYYY').format('YYYY-MM-DD HH:mm:ss'), client.pays, client.ville, client.code_postal]);
return rows;
} catch (e) {
console.log(e)
return null;
}
}
async deleteClient(id) {
let conn = await this.conn();
try {
const rows = await conn.query('DELETE FROM client WHERE id = ?', id);
return rows;
} catch (e) {
console.log(e)
return null;
}
}
}