Skip to content

Commit

Permalink
Merge pull request #194 from mcollina/tailable
Browse files Browse the repository at this point in the history
Added back tailable cursor support.
  • Loading branch information
sorribas committed Jun 10, 2015
2 parents 508c5f2 + d820996 commit a34c547
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 14 deletions.
29 changes: 16 additions & 13 deletions lib/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,19 @@ Collection.prototype._fullColName = function() {
return this._dbname + '.' + this._name;
};

Collection.prototype.find = function(query, projection, cb) {
if (typeof query === 'function') return this.find({}, null, query);
if (typeof projection === 'function') return this.find(query, null, projection);
Collection.prototype.find = function(query, projection, opts, cb) {
if (typeof query === 'function') return this.find({}, null, null, query);
if (typeof projection === 'function') return this.find(query, null, null, projection);
if (typeof opts === 'function') return this.find(query, projection, null, opts);

var cursor = new Cursor({
query: query,
projection: projection,
onserver: this._getServer,
fullCollectionName: this._fullColName()
});
opts = opts || {}

opts.query = query
opts.projection = projection
opts.onserver = this._getServer
opts.fullCollectionName = this._fullColName()

var cursor = new Cursor(opts);

if (cb) return cursor.toArray(cb);
return cursor;
Expand Down Expand Up @@ -191,9 +194,9 @@ Collection.prototype.ensureIndex = function(index, opts, cb) {

Collection.prototype.getIndexes = function(cb) {
var cursor = new Cursor({
query: {ns: this._fullColName()},
projection: {},
onserver: this._getServer,
query: {ns: this._fullColName()},
projection: {},
onserver: this._getServer,
fullCollectionName: this._dbname + '.system.indexes'
});

Expand Down Expand Up @@ -258,7 +261,7 @@ Collection.prototype.aggregate = function() {
return;
}
var strm = new AggregationCursor({
onserver: this._getServer,
onserver: this._getServer,
colName: this._name,
fullCollectionName: this._fullColName(),
pipeline: pipeline
Expand Down
6 changes: 5 additions & 1 deletion lib/cursor.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ var Cursor = function(opts) {
skip: self._opts.skip,
limit: self._opts.limit,
batchSize: self._opts.batchSize,
explain: self._opts.explain
explain: self._opts.explain,
tailable: self._opts.tailable,
timeout: self._opts.timeout,
awaitData: self._opts.awaitData,
numberOfRetries: self._opts.numberOfRetries
}));
});
});
Expand Down
37 changes: 37 additions & 0 deletions test/test-find-tailable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
var test = require('./tape');
var mongojs = require('../index');
var db = mongojs('test', ['tailable']);

test('tailable find', function(t) {
db.tailable.drop(function(err) {
db.createCollection('tailable', {capped: true, size: 1024}, function(err) {
t.notOk(err, 'no error in creating the collection');

var expected1 = { hello: 'world' };
var expected2 = { hello: 'matteo' };

var stream = db.tailable.find({}, {}, {
tailable: true,
timeout: false,
awaitData: true,
numberOfRetries: -1
});

db.tailable.insert(expected1, function(err) {
t.notOk(err, 'no error in insert');
stream.once('data', function(obj) {
t.deepEqual(obj, expected1, 'fetched object match');
stream.once('data', function(obj) {
t.deepEqual(obj, expected2, 'fetched object match');
stream.destroy();
db.tailable.drop(t.end.bind(t));
});

db.tailable.insert(expected2, function(err) {
t.notOk(err, 'no error in insert');
});
});
});
});
});
});

0 comments on commit a34c547

Please sign in to comment.