diff --git a/lib/collection.js b/lib/collection.js index 4929809..797912b 100644 --- a/lib/collection.js +++ b/lib/collection.js @@ -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; @@ -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' }); @@ -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 diff --git a/lib/cursor.js b/lib/cursor.js index fda8a67..b025c72 100644 --- a/lib/cursor.js +++ b/lib/cursor.js @@ -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 })); }); }); diff --git a/test/test-find-tailable.js b/test/test-find-tailable.js new file mode 100644 index 0000000..a87c01e --- /dev/null +++ b/test/test-find-tailable.js @@ -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'); + }); + }); + }); + }); + }); +});