title: NodeJS/Knex Creating Json Response
body: <p>I'm currently using NodeJS with knex (Postgresql) for database stuff.</p>
  
  <p><strong>Problem:</strong></p>
  
  <p>Imagine the following two tables in database:</p>
  
  <p><strong>Table 1</strong></p>
  
  <hr>
  
  <pre><code>PROJECT
  id (pk)
  name
  </code></pre>
  
  <p><strong>Table 2</strong></p>
  
  <hr>
  
  <pre><code>EMPLOYEE
  id (pk)
  name
  project_id (fk)
  </code></pre>
  
  <p>I want to create a json-response to the user that looks like the following:</p>
  
  <pre><code>{
    projects: [
      {
        id: 1,
        name: 'emxample 1',
        employees: [
         {
           id: 1,
           name: 'example 1'
         },
         {
           id: 2,
           name: 'example 2'
         }
        ]
      }
    ] 
  }
  </code></pre>
  
  <p>and so on.</p>
  
  <p>Making a query like:</p>
  
  <pre><code>let query = knex('project').select('project.*', 'employee.*').join('employee', 'employee.project_id', '=', 'project.id');
  
  query.then((projects) =&gt; { res.json(projects); }); 
  </code></pre>
  
  <p>And using <code>res.json()</code> does not return an array of <code>employees</code>. What is the way to go to achieve that?</p>