This method has been deprecated, as an improved alternative has been provided: the transformer api.
Going forward groupColumnsToObjects
will be kept the same (improvements are not planned, though)
and the new transformer API will also be provided (with updates and improvements) separately
through the transformer
helper.
TLDR: don't use this for new projects. If already using it, consider migrating, but no need to rush as this one isn't going anywhere.
PostgreSQL and MySQL
const groupColumnsToObjects = require('sqlutils/pg/groupColumnsToObjects'); //or require('sqlutils/mysql/groupColumnsToObjects');
const rows = [ //in real-world applications this would be the result of a database query
{ ssn: 'abcd', name: 'John Doe', email: '[email protected]' },
{ ssn: 'abcd', name: 'John Doe', email: '[email protected]' },
{ ssn: 'defg', name: 'Jimmy', email: '[email protected]' },
];
const employees = groupColumnsToObjects(rows, 'ssn', [
{ foreign_key: 'email', out: 'emails' }
]);
console.log(employees);
/*
[
{
"ssn": "abcd",
"name": "John Doe",
"emails": [
"[email protected]",
"[email protected]"
]
},
{
"ssn": "defg",
"name": "Jimmy",
"emails": [
"[email protected]"
]
}
]
*/
This method is much more powerful than it seems. For sofisticated examples take a look here.