title: Nested query object mapping with pg-promise
body: <p>I'm going through an example from <a href="https://github.com/vitaly-t/pg-promise" rel="nofollow">pg-promise</a> for method <a href="http://vitaly-t.github.io/pg-promise/Database.html#.map" rel="nofollow">map</a>:</p>
  
  <pre><code>// Build a list of active users, each with the list of user events:
  db.task(t =&gt; {
      return t.map('SELECT id FROM Users WHERE status = $1', ['active'], user =&gt; {
          return t.any('SELECT * FROM Events WHERE userId = $1', user.id)
              .then(events=&gt; {
                  user.events = events;
                  return user;
              });
      }).then(t.batch);
  })
      .then(data =&gt; {
          // success
      })
      .catch(error =&gt; {
          // error
      });
  </code></pre>
  
  <p>Let's say the <code>Event</code> entity has a one to many relationship with e.g. <code>Cars</code>, and I want to list all the <code>cars</code> connected to each <code>event</code>, how can I use the <a href="http://vitaly-t.github.io/pg-promise/Database.html#.map" rel="nofollow">map</a> function when the object I want is more than one level deep?</p>
  
  <p>The result I want could look something like this:</p>
  
  <pre><code>[{
      //This is a user
      id: 2,
      first_name: "John",
      last_name: "Doe",
      events: [{
          id: 4,
          type: 'type',
          cars: [{
              id: 4,
              brand: 'bmw'
          }]
      }]
  }]
  </code></pre>