Mailspring/packages/nylas-core/database-extensions.js
2016-06-22 14:41:32 -07:00

25 lines
608 B
JavaScript

const Rx = require('rx');
const Sequelize = require('sequelize');
Sequelize.Model.prototype.streamAll = function streamAll(options = {}) {
return Rx.Observable.create((observer) => {
const chunkSize = options.chunkSize || 1000;
options.offset = 0;
options.limit = chunkSize;
const findFn = (opts) => {
this.findAll(opts).then((models = []) => {
observer.onNext(models)
if (models.length === chunkSize) {
opts.offset = chunkSize;
findFn(opts)
} else {
observer.onCompleted()
}
})
}
findFn(options)
})
}