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>
This commit is contained in:
parent
61c546459a
commit
30bd5a0dbe
@ -2,6 +2,7 @@ import type {IDbCategory} from "@interfaces/database/IDbCategory";
|
|||||||
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 {IDbBrand} from "@interfaces/database/IDbBrand";
|
import type {IDbBrand} from "@interfaces/database/IDbBrand";
|
||||||
|
import type {IDbStatusResult} from "@interfaces/database/IDbStatusResult";
|
||||||
import mysql, {type Connection, type ConnectionOptions} from 'mysql2';
|
import mysql, {type Connection, type ConnectionOptions} from 'mysql2';
|
||||||
import {Logger} from "tslog";
|
import {Logger} from "tslog";
|
||||||
|
|
||||||
@ -271,15 +272,16 @@ const MySqlService = {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
Brand: {
|
Brand: {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Inserts a record into the `brands` table.
|
* Inserts a new record into the `brands` table.
|
||||||
*
|
*
|
||||||
* @param {MysqlHandler} handler - The MySQL handler instance.
|
* @param {MysqlHandler} handler - The MySQL handler object used to execute the SQL query.
|
||||||
* @param {IDbBrand} data - The data object representing the record to be inserted.
|
* @param {IDbBrand} data - The data to be inserted into the table.
|
||||||
* @returns {Promise} A promise that resolves with the result of the insertion.
|
* @returns {Promise<IDbStatusResult>} - A Promise that resolves to the status result of the insert operation.
|
||||||
* The promise is rejected with an error if the insertion fails.
|
* @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) => {
|
return new Promise((resolve, reject) => {
|
||||||
if (!data.id) return reject('Id is undefined');
|
if (!data.id) return reject('Id is undefined');
|
||||||
if (data.id.length !== 36) return reject('Id invalid');
|
if (data.id.length !== 36) return reject('Id invalid');
|
||||||
@ -292,21 +294,23 @@ const MySqlService = {
|
|||||||
data.image_blob
|
data.image_blob
|
||||||
]
|
]
|
||||||
try {
|
try {
|
||||||
resolve(handler.execute(_sql, _values))
|
resolve(handler.execute(_sql, _values) as unknown as IDbStatusResult)
|
||||||
} catch (err: unknown) {
|
} catch (err: unknown) {
|
||||||
reject(err as Error);
|
reject(err as Error);
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Updates a brand in the database.
|
* Update the brand information in the database.
|
||||||
* @param {MysqlHandler} handler - The MySQL handler.
|
*
|
||||||
* @param {IDbBrand} data - The brand data to be updated.
|
* @param {MysqlHandler} handler - The MySQL handler object used for executing the SQL query.
|
||||||
* @returns {Promise<number>} - A promise that resolves with the number of affected rows in the database.
|
* @param {IDbBrand} data - The data object containing the updated brand information.
|
||||||
* @throws {Error} - If an error occurs during the update process.
|
* @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) => {
|
return new Promise((resolve, reject) => {
|
||||||
if (!data.id) return reject('Id is undefined');
|
if (!data.id) return reject('Id is undefined');
|
||||||
if (data.id.length !== 36) return reject('Id invalid');
|
if (data.id.length !== 36) return reject('Id invalid');
|
||||||
@ -324,7 +328,7 @@ const MySqlService = {
|
|||||||
data.id
|
data.id
|
||||||
]
|
]
|
||||||
const _sql = `UPDATE "brands" SET ${_template} WHERE '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) {
|
} catch (err: unknown) {
|
||||||
reject(err as Error);
|
reject(err as Error);
|
||||||
@ -397,16 +401,12 @@ const MySqlService = {
|
|||||||
/**
|
/**
|
||||||
* Deletes a brand from the database.
|
* Deletes a brand from the database.
|
||||||
*
|
*
|
||||||
* @param {MysqlHandler} handler - The MySQL handler object used to interact with the database.
|
* @param {MysqlHandler} handler - The database handler object.
|
||||||
* @param {string} brandId - The ID of the brand to be deleted.
|
* @param {string} brandId - The ID of the brand to delete.
|
||||||
*
|
* @returns {Promise<IDbStatusResult>} A promise that resolves to the database status result.
|
||||||
* @return {Promise<unknown>} - A Promise that resolves with the result of the deletion operation.
|
* @throws {Error} If an error occurs while deleting the brand.
|
||||||
* 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.
|
|
||||||
*/
|
*/
|
||||||
delete(handler: MysqlHandler, brandId: string): Promise<unknown> {
|
delete(handler: MysqlHandler, brandId: string): Promise<IDbStatusResult> {
|
||||||
//TODO check if has models linked before actions
|
//TODO check if has models linked before actions
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
if (!brandId) return reject('Id is undefined');
|
if (!brandId) return reject('Id is undefined');
|
||||||
@ -414,7 +414,7 @@ const MySqlService = {
|
|||||||
const _sql = "DELETE FROM `brands` WHERE `id` = ?";
|
const _sql = "DELETE FROM `brands` WHERE `id` = ?";
|
||||||
const _values = [brandId];
|
const _values = [brandId];
|
||||||
try {
|
try {
|
||||||
resolve(handler.execute(_sql, _values));
|
resolve(handler.execute(_sql, _values) as unknown as IDbStatusResult);
|
||||||
} catch (err: unknown) {
|
} catch (err: unknown) {
|
||||||
reject(err as Error);
|
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 {MysqlHandler} handler - The MySQL handler object.
|
||||||
* @param {IDbModel} data - The data to be inserted.
|
* @param {IDbModel} data - The data object containing the record properties.
|
||||||
* @returns {Promise<unknown>} - A Promise that resolves with the execution result or rejects with an error.
|
* @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) => {
|
return new Promise((resolve, reject) => {
|
||||||
if (!data.id) return reject('Id is undefined');
|
if (!data.id) return reject('Id is undefined');
|
||||||
if (data.id.length !== 36) return reject('Id invalid');
|
if (data.id.length !== 36) return reject('Id invalid');
|
||||||
@ -505,7 +506,73 @@ const MySqlService = {
|
|||||||
data.id
|
data.id
|
||||||
]
|
]
|
||||||
try {
|
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) {
|
} catch (err: unknown) {
|
||||||
reject(err as Error);
|
reject(err as Error);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user