Compare commits

...

2 Commits

Author SHA1 Message Date
5163d79056
test(services): add DbHandler usage in user.service.ts
This commit includes:
- The usage of `DbHandler.factorize` with sample values
- Testing log trace to monitor the result and fields of factored user data

Issue: #18
Signed-off-by: Mathis <yidhra@tuta.io>
2024-04-30 16:36:59 +02:00
ea3b7aa68b
feat(services): prevent id field from being factorized in mysql service
In the `mysql.service.ts`, the `factorize` function has been updated to exclude the `id` field. This change ensures the 'id' field is not injected into the result to avoid any potential issues. Furthermore, The update operation in the same file has been refactored to use the updated `factorize` function, hence enhancing code reusability.

Issue: #18
Signed-off-by: Mathis <yidhra@tuta.io>
2024-04-30 16:35:51 +02:00
2 changed files with 26 additions and 12 deletions

View File

@ -56,6 +56,7 @@ class MysqlHandler {
/**
* Factorize the input data values into a database query.
* `id` field will not be injected in result to avoid problems.
*
* @param {IDbFactorizeInput} data - The input data containing values to factorize.
* @return {Promise<IDbFactorizeOutput>} - A promise resolving to the factorized output.
@ -63,7 +64,14 @@ class MysqlHandler {
factorize(data: IDbFactorizeInput): Promise<IDbFactorizeOutput> {
return new Promise((resolve, reject)=>{
try {
const _sqlQueryKeys = Object.keys(data.values).map((key: string) => `\'${key}\' = ?`)
// @ts-ignore
data.values.id ? delete data.values.id : null;
const _sqlQueryKeys = Object.keys(data.values).map((key: string) => {
if (key !== 'id') {
return `\'${key}\' = ?`
}
return '';
})
const values = Object.values(data.values).map((val)=>val)
this.Logger.debug(`\n\n>-> Factorized ${_sqlQueryKeys.length} keys for a prepare Query.\n>-> Action: ${data.actionName}\n`)
const sqlQueryKeys = _sqlQueryKeys.join(', ')
@ -358,17 +366,10 @@ const MySqlService = {
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.image_blob ? "`slug_name` = ?," : null}`;
const _values = [
data.slug_name,
data.display_name,
data.image_blob,
data.id,
];
handler.factorize({
values: data,
actionName: `Update user ID::${data.id}`
})
const _sql = `UPDATE "brands" SET ${_template} WHERE 'id' = ?`;
handler.execute(_sql, _values).then((result) => {
return resolve(result as unknown as IDbStatusResult);

View File

@ -75,6 +75,19 @@ async function getUserFromIdService(id: string): Promise<IDbUser | ISError> {
}
}
/*
DbHandler.factorize({
values: {
id: '011010101',
username: 'avnyr',
age: 42,
is_admin: true
},
actionName: "Testing"
}).then((result)=>{
logger.trace(`\n\n> ${result._valuesArray.join(', ')}\n\n> ${result.totalFields}\n\n> ${result._keysTemplate}\n`)
})*/
//ToTest
/**
* Registers a new user.