feat(services): add update method to mysql service
An `update` method is added to the `mysql.service.ts` for handling database updates. This new function checks for validity of the `id` before constructing and executing an SQL update statement. Errors are also caught and handled. Issue: #5 Signed-off-by: Mathis <yidhra@tuta.io>
This commit is contained in:
parent
016f7fa9d4
commit
64aa814d2c
@ -289,6 +289,31 @@ const MySqlService = {
|
||||
reject(err as Error);
|
||||
}
|
||||
})
|
||||
},
|
||||
update(handler: MysqlHandler, data: IDbBrand) {
|
||||
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.image_blob ? "`slug_name` = ?," : null}`
|
||||
|
||||
const _values = [
|
||||
data.slug_name,
|
||||
data.display_name,
|
||||
data.image_blob,
|
||||
data.id
|
||||
]
|
||||
const _sql = `UPDATE "brands" SET ${_template} WHERE 'id' = ?`;
|
||||
return resolve(handler.execute(_sql, _values));
|
||||
|
||||
} catch (err: unknown) {
|
||||
reject(err as Error);
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
},
|
||||
|
Loading…
x
Reference in New Issue
Block a user