feat(services): update getBySlug function in MySQL services

The `getBySlug` function originally returned a single database model by slug from the MySQL handler. It has been updated to return a list of database models by slug name. Error handling for failed query executions is also included.

Signed-off-by: Mathis <yidhra@tuta.io>
This commit is contained in:
Mathis H (Avnyr) 2024-05-03 13:17:37 +02:00
parent bdba2f6e29
commit 5afb6e22bf
Signed by: Mathis
GPG Key ID: DD9E0666A747D126

View File

@ -569,20 +569,20 @@ const MySqlService = {
}, },
/** /**
* Retrieves a database model by slug from a given MySQL handler. * Retrieves a list of database models by slug name.
* *
* @param {MysqlHandler} handler - The MySQL handler instance. * @param {MysqlHandler} handler - The MySQL handler used to execute the query.
* @param {string} modelSlug - The slug of the model to retrieve. * @param {string} modelSlug - The slug name of the model to retrieve.
* @return {Promise<IDbModel>} A promise that resolves with the retrieved model. * @return {Promise<Array<IDbModel>>} - A promise that resolves to an array of database models.
* @throws {Error} If there was an error executing the query. * @throws {Error} - If an error occurs during the execution of the query.
*/ */
getBySlug(handler: MysqlHandler, modelSlug: string): Promise<IDbModel> { getBySlug(handler: MysqlHandler, modelSlug: string): Promise<Array<IDbModel>> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const _sql = "SELECT * FROM models WHERE slug_name VALUES(?)"; const _sql = "SELECT * FROM models WHERE slug_name VALUES(?)";
const _values = [modelSlug]; const _values = [modelSlug];
try { try {
handler.execute(_sql, _values).then((result) => { handler.execute(_sql, _values).then((result) => {
return resolve(result as unknown as IDbModel); return resolve(result as unknown as Array<IDbModel>);
}); });
} catch (err: unknown) { } catch (err: unknown) {
reject(err as Error); reject(err as Error);