Implement where arg on scoped count and destroyAll

This commit is contained in:
Fabien Franzen 2014-08-26 14:54:19 +02:00
parent d419ae80d0
commit a4db887493
3 changed files with 135 additions and 9 deletions

View File

@ -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;
}

View File

@ -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 () {
});
});
});

View File

@ -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();
});
});
};
});
});