From a4db8874935a9e59fc90617c5d3c256977be8a38 Mon Sep 17 00:00:00 2001 From: Fabien Franzen Date: Tue, 26 Aug 2014 14:54:19 +0200 Subject: [PATCH] Implement where arg on scoped count and destroyAll --- lib/scope.js | 19 ++++---- test/relations.test.js | 26 ++++++++++- test/scope.test.js | 99 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 135 insertions(+), 9 deletions(-) diff --git a/lib/scope.js b/lib/scope.js index d5f6b38e..0dd2b40b 100644 --- a/lib/scope.js +++ b/lib/scope.js @@ -245,21 +245,25 @@ function defineScope(cls, targetClass, name, params, methods, options) { } this.build(data).save(cb); } - + /* Callback - The callback will be called after all elements are destroyed - For every destroy call which results in an error - If fetching the Elements on which destroyAll is called results in an error */ - function destroyAll(cb) { - var where = (this._scope && this._scope.where) || {}; - targetClass.destroyAll(where, cb); + function destroyAll(where, cb) { + if (typeof where === 'function') cb = where, where = {}; + var scoped = (this._scope && this._scope.where) || {}; + var filter = mergeQuery({ where: scoped }, { where: where || {} }); + targetClass.destroyAll(filter.where, cb); } - function count(cb) { - var where = (this._scope && this._scope.where) || {}; - targetClass.count(where, cb); + function count(where, cb) { + if (typeof where === 'function') cb = where, where = {}; + var scoped = (this._scope && this._scope.where) || {}; + var filter = mergeQuery({ where: scoped }, { where: where || {} }); + targetClass.count(filter.where, cb); } return definition; @@ -321,4 +325,3 @@ function mergeQuery(base, update) { } return base; } - diff --git a/test/relations.test.js b/test/relations.test.js index f4b8e79e..ef212363 100644 --- a/test/relations.test.js +++ b/test/relations.test.js @@ -119,6 +119,30 @@ describe('relations', function () { }); } }); + + it('should count scoped records - all and filtered', function (done) { + Book.create(function (err, book) { + book.chapters.create({name: 'a'}, function (err, ch) { + book.chapters.create({name: 'b'}, function () { + book.chapters.create({name: 'c'}, function () { + verify(book); + }); + }); + }); + }); + + function verify(book) { + book.chapters.count(function (err, count) { + should.not.exist(err); + count.should.equal(3); + book.chapters.count({ name: 'b' }, function (err, count) { + should.not.exist(err); + count.should.equal(1); + done(); + }); + }); + } + }); it('should set targetClass on scope property', function() { should.equal(Book.prototype.chapters._targetClass, 'Chapter'); @@ -2657,4 +2681,4 @@ describe('relations', function () { }); -}); +}); \ No newline at end of file diff --git a/test/scope.test.js b/test/scope.test.js index c8033c82..cb464ce4 100644 --- a/test/scope.test.js +++ b/test/scope.test.js @@ -75,6 +75,7 @@ describe('scope', function () { }); }); }); + }); describe('scope - order', function () { @@ -130,3 +131,101 @@ describe('scope - order', function () { }); +describe('scope - filtered count and destroyAll', function () { + + before(function () { + db = getSchema(); + Station = db.define('Station', { + name: {type: String, index: true}, + order: {type: Number, index: true}, + active: {type: Boolean, index: true, default: true} + }); + Station.scope('ordered', {order: 'order'}); + Station.scope('active', {where: { active: true}}); + Station.scope('inactive', {where: { active: false}}); + }); + + beforeEach(function (done) { + Station.destroyAll(done); + }); + + beforeEach(function (done) { + Station.create({ name: 'b', order: 2, active: false }, done); + }); + + beforeEach(function (done) { + Station.create({ name: 'a', order: 1 }, done); + }); + + beforeEach(function (done) { + Station.create({ name: 'd', order: 4, active: false }, done); + }); + + beforeEach(function (done) { + Station.create({ name: 'c', order: 3 }, done); + }); + + it('should find all - verify', function(done) { + Station.ordered(function(err, stations) { + should.not.exist(err); + stations.should.have.length(4); + stations[0].name.should.equal('a'); + stations[1].name.should.equal('b'); + stations[2].name.should.equal('c'); + stations[3].name.should.equal('d'); + done(); + }); + }); + + it('should count all in scope - active', function(done) { + Station.active.count(function(err, count) { + should.not.exist(err); + count.should.equal(2); + done(); + }); + }); + + it('should count all in scope - inactive', function(done) { + Station.inactive.count(function(err, count) { + should.not.exist(err); + count.should.equal(2); + done(); + }); + }); + + it('should count filtered - active', function(done) { + Station.active.count({ order: { gt: 1 } }, function(err, count) { + should.not.exist(err); + count.should.equal(1); + done(); + }); + }); + + it('should count filtered - inactive', function(done) { + Station.inactive.count({ order: 2 }, function(err, count) { + should.not.exist(err); + count.should.equal(1); + done(); + }); + }); + + it('should allow filtered destroyAll', function(done) { + Station.ordered.destroyAll({ active: false }, function(err) { + should.not.exist(err); + verify(); + }); + + var verify = function() { + Station.ordered.count(function(err, count) { + should.not.exist(err); + count.should.equal(2); + Station.inactive.count(function(err, count) { + should.not.exist(err); + count.should.equal(0); + done(); + }); + }); + }; + }); + +});