felixge/node-mysql — pull request #165 by thomaslee (2012-01-25 06:44:27+00:00)
Title: Add Client.queryWithEvents() (refactor branch)

(This is the same as https://github.com/felixge/node-mysql/pull/164, but ported to the refactor branch).

queryWithEvents() behaves just like query(), but it accepts an EventEmitter instead of a callback. This allows retrieved rows to be passed back to the user one at a time, allowing memory-efficient processing of large result sets.

e.g.

var client = mysql.createClient();

var emitter = new EventEmitter();
emitter.on("row", function(row) { console.log(util.inspect(row)); });
emitter.on("end", function(result) { client.end(); });

client.queryWithEvents("SELECT * FROM table_with_millions_of_rows", emitter);