Merge pull request #212 from offlinehacker/relations_count

add count to relations
This commit is contained in:
Raymond Feng 2014-08-15 09:37:50 -07:00
commit 62db2155e4
2 changed files with 23 additions and 0 deletions

View File

@ -147,6 +147,7 @@ function defineScope(cls, targetClass, name, params, methods, options) {
f.build = build;
f.create = create;
f.destroyAll = destroyAll;
f.count = count;
for (var i in definition.methods) {
f[i] = definition.methods[i].bind(self);
}
@ -192,6 +193,13 @@ function defineScope(cls, targetClass, name, params, methods, options) {
cls['__delete__' + name] = fn_delete;
var fn_count = function (cb) {
var f = this[name].count;
f.apply(this[name], arguments);
};
cls['__count__' + name] = fn_count;
/*
* Extracting fixed property values for the scope from the where clause into
* the data object
@ -248,6 +256,11 @@ function defineScope(cls, targetClass, name, params, methods, options) {
var where = (this._scope && this._scope.where) || {};
targetClass.destroyAll(where, cb);
}
function count(cb) {
var where = (this._scope && this._scope.where) || {};
targetClass.count(where, cb);
}
return definition;
}

View File

@ -537,6 +537,16 @@ describe('relations', function () {
});
});
});
it('should find count of records on scope - scoped', function (done) {
Category.findOne(function (err, c) {
c.productType = 'tool'; // temporary, for scoping
c.products.count(function(err, count) {
count.should.equal(1);
done();
});
});
});
it('should delete records on scope - scoped', function (done) {
Category.findOne(function (err, c) {