Merge pull request #212 from offlinehacker/relations_count
add count to relations
This commit is contained in:
commit
62db2155e4
13
lib/scope.js
13
lib/scope.js
|
@ -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
|
||||
|
@ -249,6 +257,11 @@ function defineScope(cls, targetClass, name, params, methods, options) {
|
|||
targetClass.destroyAll(where, cb);
|
||||
}
|
||||
|
||||
function count(cb) {
|
||||
var where = (this._scope && this._scope.where) || {};
|
||||
targetClass.count(where, cb);
|
||||
}
|
||||
|
||||
return definition;
|
||||
}
|
||||
|
||||
|
|
|
@ -538,6 +538,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) {
|
||||
c.productType = 'tool'; // temporary, for scoping
|
||||
|
|
Loading…
Reference in New Issue