title: How to have the proper results using KnexJS to run a PostgreSQL query which works in a SQL script editor in a DB
body: <p>I'm trying to convert a PSQL query which works in a SQL script editor to Knex.js.</p>
  <p>I have no idea how to get my results I'm getting in DB query via Knex.js and I tried what is below.</p>
  <p>The result of the Knex.js query is not what I want and I don't know how to get the result as explained below.</p>
  <p>The query is as follow</p>
  <pre class="lang-sql prettyprint-override"><code>select
      *
  from
      (
      select
          d.id id,
          unnest(d.recipients) recipient
      from
          documents d) as result
  where
      recipient = 'xxxx'
  </code></pre>
  <p>This query gave back a result from DB as</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>id</th>
  <th>recipient</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>12</td>
  <td>xxx</td>
  </tr>
  <tr>
  <td>22</td>
  <td>xxx</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>The recipient is always the same and the ID is the document that recipient has.
  I want same result or even better with the Knex.js but I'm getting a different result and don't know what to do.</p>
  <p>I tried to make the knex function as follow</p>
  <pre><code>findDocuments(recipientId) {
      return this.tx.raw(
        `
        SELECT
            *
          FROM
            (
            SELECT
              d.id id,
              UNNEST(d.recipients) recipient
            FROM
              documents d) as result
          WHERE
            recipient = '${recipientId}';
            `
      );
    }
  </code></pre>
  <p>But this in console gave a different result</p>
  <pre><code>{
     command: 'SELECT',
     rowCount: 0,
     oid: null,
     rows: [],
     fields: [
       Field {
         name: 'id',
         tableID: 19834,
         columnID: 1,
         dataTypeID: 23,
         dataTypeSize: 4,
         dataTypeModifier: -1,
         format: 'text'
       },
       Field {
         name: 'recipient',
         tableID: 0,
         columnID: 0,
         dataTypeID: 25,
         dataTypeSize: -1,
         dataTypeModifier: -1,
         format: 'text'
       }
     ],
     _parsers: [ [Function: parseInteger], [Function: noParse] ],
     _types: TypeOverrides {
       _types: {
         getTypeParser: [Function: getTypeParser],
         setTypeParser: [Function: setTypeParser],
         arrayParser: [Object],
         builtins: [Object]
       },
       text: {},
       binary: {}
     },
     RowCtor: null,
     rowAsArray: false
   }
  </code></pre>