title: Memory issues on knex.js when using streams
body: <p>I'm trying to export a whole sqlite3 database table to CSV using <em>knex.js</em>. As the table can up to 300000 rows, i use streams to don't have memory issues. But if i look the memory usage of my app it up to 800MB or i have an "out of memory" error. </p>
  
  <p>How can i handle a large query result with knex.js on sqlite3 database?</p>
  
  <p>Below a sample of code :</p>
  
  <pre><code>knex.select().from(table).stream(function (stream) {
      var stringifier = stringify(opts);
      var fileStream = fs.createWriteStream(file);
  
      var i = 0;
      stringifier.on('readable', function() {
        var row;
        while (row = stringifier.read()) {
          fileStream.write(row);
          console.log("row " + i++); //debug
        }
      });
  
      fileStream.once('open', function(fd) {
        stream.pipe(stringifier);
      });
  });
  </code></pre>
  
  <p><strong>EDIT</strong></p>
  
  <p>Seems knex.js streams for sqlite3 database are "fake" streams.
  Below the source code of the stream function for sqlite3 in knex :</p>
  
  <pre><code>Runner_SQLite3.prototype._stream = Promise.method(function(sql, stream, options) {
      /*jshint unused: false*/
      var runner = this;
      return new Promise(function(resolver, rejecter) {
          stream.on('error', rejecter);
          stream.on('end', resolver);
          return runner.query(sql).map(function(row) {
              stream.write(row);
          }).catch(function(err) {
              stream.emit('error', err);
          }).then(function() {
              stream.end();
          });
      });
  });
  </code></pre>
  
  <p>We see that it waits for the request to be executed before create the stream 
  from the result array.</p>
  
  <p><strong>VERSION:</strong> </p>
  
  <ul>
  <li>Knex.Js 0.7.5 </li>
  <li>node 0.12</li>
  </ul>
  
  <p>Thx for your help.</p>