Add fetchRow DB function.

Helper function to fetch a row at a time using a cursor.
This commit is contained in:
D. Berge
2022-03-07 21:16:43 +01:00
parent 8debe60d5c
commit d3336c6cf7

View File

@@ -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
};