From 02383b8c8a39adaa56e2561432c8c48197800414 Mon Sep 17 00:00:00 2001 From: Mathis Date: Thu, 25 Apr 2024 12:18:33 +0200 Subject: [PATCH] feat(services): add getAll function to mysql service This commit adds a new `getAll` function in the `mysql.service.ts` file, which retrieves all categories from the database. It executes a SQL query and includes error handling that either resolves with an array of category objects or rejects with an Error object. BREAKING-CHANGE: #6 Signed-off-by: Mathis --- src/services/mysql.service.ts | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/services/mysql.service.ts b/src/services/mysql.service.ts index 69b384f..6f8745e 100644 --- a/src/services/mysql.service.ts +++ b/src/services/mysql.service.ts @@ -402,7 +402,25 @@ const MySqlService = { } }) }, - + /** + * Retrieves all categories from the database. + * + * @param {MysqlHandler} handler - The MySQL handler used for executing the query. + * + * @return {Promise>} - A promise that resolves with an array of category objects from the database. + * - The category objects are of type IDbCategory. + * - If an error occurs, the promise will be rejected with an Error object. + */ + getAll(handler: MysqlHandler) { + return new Promise((resolve, reject) => { + const _sql = "SELECT * FROM `categories`"; + try { + resolve(handler.query(_sql) as unknown as Array); + } catch (err: unknown) { + reject(err as Error); + } + }); + } } }