I'm using PostgreSQL with nodejs and pg. Everything works fine, but I want to output a result from PostgreSQL as a nested json - as if I was working with MongoDB or similar.
My 2 tables from PostgreSQL are:
portfolio (id int, name text)
cars (portfolio_id int, name text);
Is there a "correct" way of returning a JSON object with the following structure:
{
{ name: 'Portfolio #1', cars: { name: 'Car #1', name: 'Car #2' },
{ name: 'Portfolio #2', cars: { name: 'Car #3' }
}
My general way of querying the database in nodejs/pg is:
client.query('SELECT ...', [params], function(err, result) {
done();
if (err) {
res.status(500).json({ error: err });
} else {
res.json({ portfolios: result.rows });
}
});