style(services): 🎨 Db

This commit is contained in:
Mathis H (Avnyr) 2024-04-25 11:36:20 +02:00
parent 896b01f8b4
commit adbecfa435
Signed by: Mathis
GPG Key ID: DD9E0666A747D126

View File

@ -2,6 +2,7 @@ import mysql, {type Connection, type ConnectionOptions} from 'mysql2';
import {Logger} from "tslog"; import {Logger} from "tslog";
import type {IDbModel} from "@interfaces/database/IDbModel"; import type {IDbModel} from "@interfaces/database/IDbModel";
import type {IDbUser} from "@interfaces/database/IDbUser"; import type {IDbUser} from "@interfaces/database/IDbUser";
import type IDbCategory from "@interfaces/database/IDbCategory";
const access: ConnectionOptions = { const access: ConnectionOptions = {
@ -88,27 +89,27 @@ const MySqlService = {
* Insert a user into the database. * Insert a user into the database.
* *
* @param {MysqlHandler} handler - The MySQL database handler. * @param {MysqlHandler} handler - The MySQL database handler.
* @param {IDbUser} userData - The user data to insert. * @param {IDbUser} data - The user data to insert.
* @returns {Promise<void>} A promise that resolves if the user was inserted successfully, or rejects with an error. * @returns {Promise<void>} A promise that resolves if the user was inserted successfully, or rejects with an error.
* @throws {Error} If an error occurs while executing the query. * @throws {Error} If an error occurs while executing the query.
*/ */
insert(handler: MysqlHandler, userData: IDbUser) { insert(handler: MysqlHandler, data: IDbUser) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (!userData.id) return reject('Id is undefined'); if (!data.id) return reject('Id is undefined');
if (userData.id.length !== 36) return reject('Id invalid'); if (data.id.length !== 36) return reject('Id invalid');
const _sql = "INSERT INTO `users`(`id`,`username`, `firstname`, `lastname`, `dob`, `email`, `is_mail_verified`, `is_admin`, `gdpr`, `hash`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)" const _sql = "INSERT INTO `users`(`id`,`username`, `firstname`, `lastname`, `dob`, `email`, `is_mail_verified`, `is_admin`, `gdpr`, `hash`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)"
const _values = [ const _values = [
userData.id, data.id,
userData.username, data.username,
userData.firstname, data.firstname,
userData.lastname, data.lastname,
userData.dob, data.dob,
userData.email, data.email,
userData.is_mail_verified, data.is_mail_verified,
userData.is_admin, data.is_admin,
userData.gdpr, data.gdpr,
userData.hash data.hash
] ]
try { try {
resolve(handler.execute(_sql, _values)) resolve(handler.execute(_sql, _values))
@ -121,37 +122,37 @@ const MySqlService = {
/** /**
* Updates user data in the database. * Updates user data in the database.
* @param {MysqlHandler} handler - The MySQL handler object. * @param {MysqlHandler} handler - The MySQL handler object.
* @param {IDbUser} userData - The user data to be updated. * @param {IDbUser} data - The user data to be updated.
* @returns {Promise<unknown>} - A promise that resolves when the update is successful. * @returns {Promise<unknown>} - A promise that resolves when the update is successful.
* @throws {Error} - If an error occurs during the update process. * @throws {Error} - If an error occurs during the update process.
*/ */
update(handler: MysqlHandler, userData: IDbUser): Promise<unknown> { update(handler: MysqlHandler, data: IDbUser): Promise<unknown> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (!userData.id) return reject('Id is undefined'); if (!data.id) return reject('Id is undefined');
if (userData.id.length !== 36) return reject('Id invalid'); if (data.id.length !== 36) return reject('Id invalid');
try { try {
const _template = ` const _template = `
${userData.username ? "`username` = ?," : null} ${data.username ? "`username` = ?," : null}
${userData.firstname ? "`firstname` = ?," : null} ${data.firstname ? "`firstname` = ?," : null}
${userData.lastname ? "`lastname` = ?," : null} ${data.lastname ? "`lastname` = ?," : null}
${userData.dob ? "`dob` = ?," : null} ${data.dob ? "`dob` = ?," : null}
${userData.email ? "`email` = ?," : null} ${data.email ? "`email` = ?," : null}
${userData.is_mail_verified ? "`is_mail_verified` = ?," : null} ${data.is_mail_verified ? "`is_mail_verified` = ?," : null}
${userData.is_admin ? "`is_admin` = ?," : null} ${data.is_admin ? "`is_admin` = ?," : null}
${userData.gdpr ? "`gdpr` = ?," : null} ${data.gdpr ? "`gdpr` = ?," : null}
${userData.hash ? "`hash` = ?" : null}` ${data.hash ? "`hash` = ?" : null}`
const _values = [ const _values = [
userData.username, data.username,
userData.firstname, data.firstname,
userData.lastname, data.lastname,
userData.dob, data.dob,
userData.email, data.email,
userData.is_mail_verified, data.is_mail_verified,
userData.is_admin, data.is_admin,
userData.gdpr, data.gdpr,
userData.hash, data.hash,
userData.id data.id
] ]
const _sql = `UPDATE "users" SET ${_template} WHERE 'id' = ?`; const _sql = `UPDATE "users" SET ${_template} WHERE 'id' = ?`;