Thursday 30 April 2020

Objection.js - How to handle update, delete, ... queries better in Express with MySQL as the database?

Whenever a delete, update,... query is made in Objection using the MySQL database, the query only returns affected rows, but not the affected data.In Express, my current approach would be to make if/else statements on a function level, like the one below: js router.delete("/", async (req, res, next) => { const { id } = req.body; try { const todo = await Todo.query().deleteById(id); // deleteById returns the affected rows; if delete was successful, returns value 1 if (todo) { res.json({ message: "Item successfully deleted" }); } else { res.json({ message: "Item not found" }); } } catch (err) { next(err); } });However, this if/else approach inside a function doesn't seem right to me. What if multiple developers work on the same project and each one of them returns messages with different meanings or information.Does anyone have a suggestion or maybe even a repository that handles this better?

Submitted April 30, 2020 at 02:04PM by Tanckom

No comments:

Post a Comment