Skip to content

Commit

Permalink
Add Query#aggregate support #12
Browse files Browse the repository at this point in the history
  • Loading branch information
junosuarez committed Oct 23, 2013
1 parent 5e30720 commit 1ad3969
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 3 deletions.
9 changes: 9 additions & 0 deletions mongodb.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ proto.run = function (query) {
case 'upsert':
case 'remove':
case 'removeAll':
case 'aggregate':
return self._collection(query).then(function (collection) {
return self['_' + query.command](collection, query)
})
Expand Down Expand Up @@ -136,6 +137,14 @@ proto._exists = function (collection, query) {
})
}

// (Query) => Promise
proto._aggregate = function (collection, query) {
if (!Array.isArray(query.commandArg)) {
return Q.reject(new TypeError('Argument must be an array'))
}
return Q.ninvoke(collection, 'aggregate', query.commandArg, query.options)
}

// (Query) => Promise
proto._insert = function (collection, query) {
return Q.ninvoke(collection, 'insert', query.commandArg, query.options)
Expand Down
1 change: 1 addition & 0 deletions query.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ proto.byIds = function (ids) {

proto.count = command('count')
proto.exists = command('exists')
proto.aggregate = command('aggregate')

// finalizers
proto.assert = function (assertion, message) {
Expand Down
52 changes: 51 additions & 1 deletion test/mongodb.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,12 @@ describe('MongoDb', function () {

describe('#run', function () {
['read','count','exists','insert', 'update', 'findAndModify', 'modifyAndFind', 'pull',
'upsert', 'remove', 'removeAll'].forEach(function (command) {
'upsert', 'remove', 'removeAll', 'aggregate'].forEach(function (command) {
it('dispatches to ' + command + 'command', function (done) {
var q = StubQuery()
q.command = command
var mdb = new MongoDb
mdb.should.have.property('_'+command)
mdb['_'+command] = sinon.stub().returns(Q('result'))
var collection = {}
mdb._collection = sinon.stub().returns(Q(collection))
Expand Down Expand Up @@ -367,6 +368,55 @@ describe('MongoDb', function () {

})
})

describe('#_aggregate', function () {
it('calls underlying aggregate', function (done) {
var aggregate = function (array, options, callback) {
aggregate.args = arguments
process.nextTick(function () {
callback(null, [])
})
}
var collection = {aggregate:aggregate}

var q = StubQuery()
q.collection = 'fooCollection'
q.command = 'aggregate'
q.commandArg = []
q.options = {}
var mdb = new MongoDb()

mdb._aggregate(collection, q).then(function (val) {
aggregate.args[0].should.equal(q.commandArg)
aggregate.args[1].should.equal(q.options)
})
.then(done, done)
})
it('rejects if commandArg is not an array', function (done) {
var aggregate = function (array, options, callback) {
aggregate.args = arguments
process.nextTick(function () {
callback(null, [])
})
}
var collection = {aggregate:aggregate}

var q = StubQuery()
q.collection = 'fooCollection'
q.command = 'aggregate'
q.commandArg = {not:Array}
q.options = {}
var mdb = new MongoDb()

mdb._aggregate(collection, q).then(function () {
throw new Error('should not be resolved')
}, function (err) {
err.should.match(/array/)
})
.then(done, done)
})
})

describe('#_insert', function () {
it('calls underlying insert', function (done) {

Expand Down
4 changes: 2 additions & 2 deletions test/query.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -330,10 +330,10 @@ describe('Query', function () {
})


describe('mutators', function () {
describe('commands', function () {

['insert', 'update', 'findAndModify', 'modifyAndFind', 'pull',
'upsert', 'remove', 'removeAll', 'count', 'exists'].forEach(function (command) {
'upsert', 'remove', 'removeAll', 'count', 'exists', 'aggregate'].forEach(function (command) {

it('runs ' + command + ' command', function (done) {

Expand Down

0 comments on commit 1ad3969

Please sign in to comment.