diff --git a/lib/scope.js b/lib/scope.js index 312c8999..d5f6b38e 100644 --- a/lib/scope.js +++ b/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 @@ -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; } diff --git a/test/relations.test.js b/test/relations.test.js index ce0e62f3..3986f0fb 100644 --- a/test/relations.test.js +++ b/test/relations.test.js @@ -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) {