knex: what is the appropriate way to create an array from results?
<p>I have an endpoint that joins the <code>user</code> and <code>user_emails</code> table as a one-to-many relationship (postgresql). It look as follows.</p>

<pre><code>router.get('/', function (req, res, next) {
  db.select('users.id', 'users.name', 'user_emails.address')
    .from('users')
    .leftJoin('user_emails', 'users.id', 'user_emails.user_id')
    .then(users =&gt; res.status(200).json(users))
    .catch(next)  // go to error handler
});
</code></pre>

<p>However, this will return a new document for each email address. What I want is an array of documents that looks as follows:</p>

<pre><code>[{
  id: 1,
  name: 'Steve',
  emails: [
    { address: 'hello@world.org' },
    { address: 'meow@meow.org' }
  ]
}, {
  id: 2,
  name: 'Jimmy',
  emails: [
    { address: 'jimmy@jimbo.org' }
  ]
}]
</code></pre>

<p>How should this be done in knex?</p>