Compare commits

...

2 Commits

Author SHA1 Message Date
30bd5a0dbe
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<IDbStatusResult>` instead of `Promise<unknown>` or `Promise<number>`.
- Also, adjusted the functions documentations to reflect these changes, providing more clarity about the returned result from database operations.

Signed-off-by: Mathis <yidhra@tuta.io>
2024-04-26 11:34:15 +02:00
61c546459a
feat(interfaces): add IDbStatusResult interface
A new interface, `IDbStatusResult`, has been added to the 'interfaces' scope. This interface includes details about database operations such as field count, affected rows, insert id, info, server status, warning status, and changed rows.

Signed-off-by: Mathis <yidhra@tuta.io>
2024-04-26 11:18:41 +02:00
2 changed files with 106 additions and 30 deletions

View File

@ -0,0 +1,9 @@
export interface IDbStatusResult {
fieldCount: number;
affectedRows: number;
insertId: number;
info: string;
serverStatus: number;
warningStatus: number;
changedRows: number;
}

View File

@ -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<IDbStatusResult>} - 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<unknown> {
insert(handler: MysqlHandler, data: IDbBrand): Promise<IDbStatusResult> {
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<number>} - 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<IDbStatusResult>} - 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<number> {
update(handler: MysqlHandler, data: IDbBrand): Promise<IDbStatusResult> {
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<unknown>} - 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<IDbStatusResult>} 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<unknown> {
delete(handler: MysqlHandler, brandId: string): Promise<IDbStatusResult> {
//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<unknown>} - 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<IDbStatusResult>} - A promise that resolves to the status result of the insert operation.
*/
insert(handler: MysqlHandler, data: IDbModel): Promise<unknown> {
insert(handler: MysqlHandler, data: IDbModel): Promise<IDbStatusResult> {
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<IDbStatusResult>} - 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<IDbStatusResult> {
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<IDbStatusResult>} 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<IDbStatusResult> {
//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);
}