Skip to content

Instantly share code, notes, and snippets.

@Chris927
Created July 11, 2014 15:29
Show Gist options
  • Select an option

  • Save Chris927/3736b64a2ed1b278ddf9 to your computer and use it in GitHub Desktop.

Select an option

Save Chris927/3736b64a2ed1b278ddf9 to your computer and use it in GitHub Desktop.
var EventsEmitter = require('events').EventEmitter;
pg.connect(..., function(err, client, done) {
var pageSize = 50;
var emitter = new EventsEmitter();
emitter.on('next', function(offset) {
callHandlersPaginated(offset);
});
emitter.on('end', function() {
done();
});
emitter.emit('next', /* initial offset */ 0);
function callHandlersPaginated(offset) {
var rowsProcessed = 0;
client.query("select payload from events limit " + pageSize + " offset " + offset + " order by id");
client.on('row', function(row, result) {
// do stuff
rowsProcessed++;
});
client.on('end', function(result) {
// do stuff
if (rowsProcessed >= pageSize) {
emitter.emit('next', offset + pageSize);
} else {
emitter.emit('end');
}
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment