mirror of
https://gitlab.com/wgp/dougal/software.git
synced 2025-12-06 11:47:09 +00:00
Add database utilities file.
Intended to contain reusable functions.
This commit is contained in:
28
lib/www/server/lib/db/utils.js
Normal file
28
lib/www/server/lib/db/utils.js
Normal file
@@ -0,0 +1,28 @@
|
||||
/** @file utils.js Database utility functions
|
||||
*/
|
||||
|
||||
|
||||
/** Project a query
|
||||
*
|
||||
* @a rows are the tuples to operate on
|
||||
* @a columns are the attributes to extract
|
||||
*
|
||||
* @return {Array} of projected tuples
|
||||
*/
|
||||
function project (rows, columns) {
|
||||
if (columns) {
|
||||
const tokens = Array.isArray(columns)
|
||||
? columns
|
||||
: columns.split(/\s*[,;:\s]\s*/).filter(e => e.length);
|
||||
const project = tokens.map(i => i.replace(/^([^.]+)\..*$/, "$1"));
|
||||
return rows.map( r =>
|
||||
Object.fromEntries(Object.entries(r).filter(entry => project.includes(entry[0])))
|
||||
);
|
||||
} else {
|
||||
return rows;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
project
|
||||
};
|
||||
Reference in New Issue
Block a user