You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Inspired by the pg-promise library, I propose that we implement a convenience function db.one(sql, params, tableName). db.one() will call db.exec() internally, but will ensure that only one result is returned.
Proposed behavior:
If the db.exec() call returns no rows, throw a new NotFound() error with the tableName in the description.
If the db.exec() call returns exactly one row, return rows[0]
If the db.exec() call returns multiple rows, log a warning and return rows[0].
For example:
// on the accounts controllerfunctiondetail(req,res,next){db.one('SELECT * FROM account WHERE id = ?',[req.params.id],'account').then(row=>res.status(200).json(row)).catch(next).done();}// in the purchase orders controllerfunctiondetail(req,res,next){db.one(`SELECT p.uuid, p.date, p.supplier_uuid, pi.cost FROM purchase p JOIN purchase_item pi ON p.uuid = pi.purchase_uuid WHERE p.uuid = ?`,[req.params.uuid],'purchase').then(row=>res.status(200).json(row)).catch(next).done()}
The text was updated successfully, but these errors were encountered:
This commit implements db.one() and changes a few controllers to use it
as a proof of work. This allows logic for NotFound and BadRequest to be
implement in the `db` library instead of in each individual controller.
Closes#564.
This commit implements db.one() and changes a few controllers to use it
as a proof of work. This allows logic for NotFound and BadRequest to be
implement in the `db` library instead of in each individual controller.
Closes#564.
Inspired by the pg-promise library, I propose that we implement a convenience function
db.one(sql, params, tableName)
.db.one()
will calldb.exec()
internally, but will ensure that only one result is returned.Proposed behavior:
db.exec()
call returns no rows, throw anew NotFound()
error with thetableName
in the description.db.exec()
call returns exactly one row, returnrows[0]
db.exec()
call returns multiple rows, log a warning and returnrows[0]
.For example:
The text was updated successfully, but these errors were encountered: