results_stream.js 933 B

123456789101112131415161718192021222324252627282930313233343536
  1. var Readable = require('stream').Readable;
  2. // copy-paste from https://github.com/felixge/node-mysql/blob/master/lib/protocol/sequences/Query.js
  3. module.exports = function (command, connectionStream) {
  4. command.stream = function(options) {
  5. var stream;
  6. options = options || {};
  7. options.objectMode = true;
  8. stream = new Readable(options),
  9. stream._read = function() {
  10. connectionStream.resume();
  11. };
  12. this.on("result",function(row,i) {
  13. if (!stream.push(row)) connectionStream.pause();
  14. stream.emit("result",row,i); // replicate old emitter
  15. });
  16. this.on("error",function(err) {
  17. stream.emit("error",err); // Pass on any errors
  18. });
  19. this.on("end", function() {
  20. stream.push(null); // pushing null, indicating EOF
  21. });
  22. this.on("fields",function(fields,i) {
  23. stream.emit("fields",fields,i); // replicate old emitter
  24. });
  25. return stream;
  26. };
  27. };