Using the latest syntax supported by pg-promise :
db.task(t => { return t.map('SELECT * FROM Users', [], user => { return user.emailid ? t.one('SELECT * FROM Emails WHERE id = $1', user.emailid, e=> { user.email = e.email; return user; }) : user; }).then(t.batch); }) .then(data => {
See API: Database.map , Database.one .
See also the corresponding question: Get parent tree + tree with pg promises .
NOTE. . A more convenient approach would be to use a single query with an INNER JOIN .
source share