mirror of
https://gitlab.com/wgp/dougal/software.git
synced 2025-12-06 11:27:09 +00:00
Add fetchRow DB function.
Helper function to fetch a row at a time using a cursor.
This commit is contained in:
@@ -49,9 +49,31 @@ async function schema2pid (schema, client) {
|
||||
return res.rows[0] && res.rows[0].pid;;
|
||||
}
|
||||
|
||||
/** Fetch one row from a database cursor.
|
||||
*
|
||||
* @a cursor A query cursor
|
||||
*
|
||||
* @returns A row object if there are any rows left to consume.
|
||||
* @returns Undefined when all the rows (if any) have been consumed.
|
||||
*/
|
||||
async function fetchRow (cursor) {
|
||||
return new Promise((resolve, reject) => {
|
||||
cursor.read(1, (err, rows) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
} else if (rows.length) {
|
||||
resolve(rows[0]);
|
||||
} else {
|
||||
resolve(); // undefined
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
pool,
|
||||
transaction,
|
||||
setSurvey,
|
||||
schema2pid
|
||||
schema2pid,
|
||||
fetchRow
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user