From 30bd5a0dbe7bb51e9ca35923d220443679b76fec Mon Sep 17 00:00:00 2001 From: Mathis Date: Fri, 26 Apr 2024 11:34:15 +0200 Subject: [PATCH] feat(services): update database operations return type in mysql.service This commit includes significant changes in `mysql.service.ts` in the `services` scope. - Imported `IDbStatusResult` type. - Updated the return type of all operations (insert, update, delete) on both `Brand` and `Model`. These operations now return `Promise` instead of `Promise` or `Promise`. - Also, adjusted the functions documentations to reflect these changes, providing more clarity about the returned result from database operations. Signed-off-by: Mathis --- src/services/mysql.service.ts | 127 ++++++++++++++++++++++++++-------- 1 file changed, 97 insertions(+), 30 deletions(-) diff --git a/src/services/mysql.service.ts b/src/services/mysql.service.ts index 3bdf5fe..533f646 100644 --- a/src/services/mysql.service.ts +++ b/src/services/mysql.service.ts @@ -2,6 +2,7 @@ import type {IDbCategory} from "@interfaces/database/IDbCategory"; import type {IDbModel} from "@interfaces/database/IDbModel"; import type {IDbUser} from "@interfaces/database/IDbUser"; import type {IDbBrand} from "@interfaces/database/IDbBrand"; +import type {IDbStatusResult} from "@interfaces/database/IDbStatusResult"; import mysql, {type Connection, type ConnectionOptions} from 'mysql2'; import {Logger} from "tslog"; @@ -271,15 +272,16 @@ const MySqlService = { } }, Brand: { + /** - * Inserts a record into the `brands` table. + * Inserts a new record into the `brands` table. * - * @param {MysqlHandler} handler - The MySQL handler instance. - * @param {IDbBrand} data - The data object representing the record to be inserted. - * @returns {Promise} A promise that resolves with the result of the insertion. - * The promise is rejected with an error if the insertion fails. + * @param {MysqlHandler} handler - The MySQL handler object used to execute the SQL query. + * @param {IDbBrand} data - The data to be inserted into the table. + * @returns {Promise} - A Promise that resolves to the status result of the insert operation. + * @throws {Error} - If an error occurs during the execution of the SQL query. */ - insert(handler: MysqlHandler, data: IDbBrand): Promise { + insert(handler: MysqlHandler, data: IDbBrand): Promise { return new Promise((resolve, reject) => { if (!data.id) return reject('Id is undefined'); if (data.id.length !== 36) return reject('Id invalid'); @@ -292,21 +294,23 @@ const MySqlService = { data.image_blob ] try { - resolve(handler.execute(_sql, _values)) + resolve(handler.execute(_sql, _values) as unknown as IDbStatusResult) } catch (err: unknown) { reject(err as Error); } }) }, + /** - * Updates a brand in the database. - * @param {MysqlHandler} handler - The MySQL handler. - * @param {IDbBrand} data - The brand data to be updated. - * @returns {Promise} - A promise that resolves with the number of affected rows in the database. - * @throws {Error} - If an error occurs during the update process. + * Update the brand information in the database. + * + * @param {MysqlHandler} handler - The MySQL handler object used for executing the SQL query. + * @param {IDbBrand} data - The data object containing the updated brand information. + * @returns {Promise} - A promise that resolves to the status result of the update operation. + * @throws {Error} - If any error occurs during the process. */ - update(handler: MysqlHandler, data: IDbBrand): Promise { + update(handler: MysqlHandler, data: IDbBrand): Promise { return new Promise((resolve, reject) => { if (!data.id) return reject('Id is undefined'); if (data.id.length !== 36) return reject('Id invalid'); @@ -324,7 +328,7 @@ const MySqlService = { data.id ] const _sql = `UPDATE "brands" SET ${_template} WHERE 'id' = ?`; - return resolve(handler.execute(_sql, _values) as unknown as number); + return resolve(handler.execute(_sql, _values) as unknown as IDbStatusResult); } catch (err: unknown) { reject(err as Error); @@ -397,16 +401,12 @@ const MySqlService = { /** * Deletes a brand from the database. * - * @param {MysqlHandler} handler - The MySQL handler object used to interact with the database. - * @param {string} brandId - The ID of the brand to be deleted. - * - * @return {Promise} - A Promise that resolves with the result of the deletion operation. - * If the deletion is successful, the Promise resolves with the result. - * If an error occurs, the Promise rejects with the error. - * - * @throws {Error} If the brandId is undefined or invalid. + * @param {MysqlHandler} handler - The database handler object. + * @param {string} brandId - The ID of the brand to delete. + * @returns {Promise} A promise that resolves to the database status result. + * @throws {Error} If an error occurs while deleting the brand. */ - delete(handler: MysqlHandler, brandId: string): Promise { + delete(handler: MysqlHandler, brandId: string): Promise { //TODO check if has models linked before actions return new Promise((resolve, reject) => { if (!brandId) return reject('Id is undefined'); @@ -414,7 +414,7 @@ const MySqlService = { const _sql = "DELETE FROM `brands` WHERE `id` = ?"; const _values = [brandId]; try { - resolve(handler.execute(_sql, _values)); + resolve(handler.execute(_sql, _values) as unknown as IDbStatusResult); } catch (err: unknown) { reject(err as Error); } @@ -482,13 +482,14 @@ const MySqlService = { }, /** - * Inserts a new record into the `models` table. + * Inserts a record into the `models` table. * - * @param {MysqlHandler} handler - The MySQL handler instance. - * @param {IDbModel} data - The data to be inserted. - * @returns {Promise} - A Promise that resolves with the execution result or rejects with an error. + * @param {MysqlHandler} handler - The MySQL handler object. + * @param {IDbModel} data - The data object containing the record properties. + * @throws {string} - Throws an error message if the id is undefined or invalid. + * @returns {Promise} - A promise that resolves to the status result of the insert operation. */ - insert(handler: MysqlHandler, data: IDbModel): Promise { + insert(handler: MysqlHandler, data: IDbModel): Promise { return new Promise((resolve, reject) => { if (!data.id) return reject('Id is undefined'); if (data.id.length !== 36) return reject('Id invalid'); @@ -505,7 +506,73 @@ const MySqlService = { data.id ] try { - resolve(handler.execute(_sql, _values)) + resolve(handler.execute(_sql, _values) as unknown as IDbStatusResult) + } catch (err: unknown) { + reject(err as Error); + } + }) + }, + + //TODO get linked vehicles + + /** + * Updates a database model in the "models" table. + * + * @param {MysqlHandler} handler - The MySQL handler object. + * @param {IDbModel} data - The data object containing the updated model details. + * @return {Promise} - A promise that resolves to the status result of the update operation. + * @throws {Error} - If an error occurs during the update process. + */ + update(handler: MysqlHandler, data: IDbModel): Promise { + return new Promise((resolve, reject) => { + if (!data.id) return reject('Id is undefined'); + if (data.id.length !== 36) return reject('Id invalid'); + + try { + const _template = ` + ${data.slug_name ? "`slug_name` = ?," : null} + ${data.display_name ? "`display_name` = ?," : null} + ${data.brand_id ? "`brand_id` = ?," : null} + ${data.category_id ? "`category_id` = ?," : null} + ${data.image_blob ? "`image_blob` = ?," : null} + ${data.is_trending ? "`is_trending` = ?," : null} + ${data.base_price ? "`base_price` = ?," : null}` + + const _values = [ + data.slug_name, + data.display_name, + data.brand_id, + data.category_id, + data.image_blob, + data.is_trending, + data.base_price, + data.id + ] + const _sql = `UPDATE "models" SET ${_template} WHERE 'id' = ?`; + return resolve(handler.execute(_sql, _values) as unknown as IDbStatusResult); + } catch (err: unknown) { + reject(err as Error); + } + }) + }, + + /** + * Deletes a model from the database. + * + * @param {MysqlHandler} handler - The MySQL handler object. + * @param {string} modelId - The ID of the model to delete. + * @returns {Promise} A promise that resolves to the result of the delete operation. + * @throws {Error} If an error occurs during the delete operation. + */ + delete(handler: MysqlHandler, modelId: string): Promise { + //TODO check if has models linked before actions + return new Promise((resolve, reject) => { + if (!modelId) return reject('Id is undefined'); + if (modelId.length !== 36) return reject('Id invalid'); + const _sql = "DELETE FROM `models` WHERE `id` = ?"; + const _values = [modelId]; + try { + resolve(handler.execute(_sql, _values) as unknown as IDbStatusResult); } catch (err: unknown) { reject(err as Error); }