Compare commits

..

No commits in common. "30bd5a0dbe7bb51e9ca35923d220443679b76fec" and "adaf4e30dbaa06afa5f4a8de7150b5e3ec832746" have entirely different histories.

2 changed files with 30 additions and 106 deletions

View File

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

View File

@ -2,7 +2,6 @@ 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";
@ -272,16 +271,15 @@ const MySqlService = {
}
},
Brand: {
/**
* Inserts a new record into the `brands` table.
* Inserts a record into the `brands` table.
*
* @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.
* @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.
*/
insert(handler: MysqlHandler, data: IDbBrand): Promise<IDbStatusResult> {
insert(handler: MysqlHandler, data: IDbBrand): Promise<unknown> {
return new Promise((resolve, reject) => {
if (!data.id) return reject('Id is undefined');
if (data.id.length !== 36) return reject('Id invalid');
@ -294,23 +292,21 @@ const MySqlService = {
data.image_blob
]
try {
resolve(handler.execute(_sql, _values) as unknown as IDbStatusResult)
resolve(handler.execute(_sql, _values))
} catch (err: unknown) {
reject(err as Error);
}
})
},
/**
* 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.
* 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(handler: MysqlHandler, data: IDbBrand): Promise<IDbStatusResult> {
update(handler: MysqlHandler, data: IDbBrand): Promise<number> {
return new Promise((resolve, reject) => {
if (!data.id) return reject('Id is undefined');
if (data.id.length !== 36) return reject('Id invalid');
@ -328,7 +324,7 @@ const MySqlService = {
data.id
]
const _sql = `UPDATE "brands" SET ${_template} WHERE 'id' = ?`;
return resolve(handler.execute(_sql, _values) as unknown as IDbStatusResult);
return resolve(handler.execute(_sql, _values) as unknown as number);
} catch (err: unknown) {
reject(err as Error);
@ -401,12 +397,16 @@ const MySqlService = {
/**
* Deletes a brand from the database.
*
* @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.
* @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.
*/
delete(handler: MysqlHandler, brandId: string): Promise<IDbStatusResult> {
delete(handler: MysqlHandler, brandId: string): Promise<unknown> {
//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) as unknown as IDbStatusResult);
resolve(handler.execute(_sql, _values));
} catch (err: unknown) {
reject(err as Error);
}
@ -482,14 +482,13 @@ const MySqlService = {
},
/**
* Inserts a record into the `models` table.
* Inserts a new record into the `models` table.
*
* @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.
* @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.
*/
insert(handler: MysqlHandler, data: IDbModel): Promise<IDbStatusResult> {
insert(handler: MysqlHandler, data: IDbModel): Promise<unknown> {
return new Promise((resolve, reject) => {
if (!data.id) return reject('Id is undefined');
if (data.id.length !== 36) return reject('Id invalid');
@ -506,73 +505,7 @@ const MySqlService = {
data.id
]
try {
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);
resolve(handler.execute(_sql, _values))
} catch (err: unknown) {
reject(err as Error);
}