From 34acc6c50a48632fa6503238c21a3244926208ef Mon Sep 17 00:00:00 2001 From: Fabien Franzen Date: Fri, 20 Mar 2015 16:37:46 +0100 Subject: [PATCH] Implement scope.findById --- lib/scope.js | 24 +++++++++++++++++++++--- test/scope.test.js | 23 ++++++++++++++++++++++- 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/lib/scope.js b/lib/scope.js index fddfde5e..b7f879ff 100644 --- a/lib/scope.js +++ b/lib/scope.js @@ -189,6 +189,7 @@ function defineScope(cls, targetClass, name, params, methods, options) { f.build = build; f.create = create; f.destroyAll = destroyAll; + f.findById = findById; f.count = count; for (var i in definition.methods) { f[i] = definition.methods[i].bind(self); @@ -237,6 +238,13 @@ function defineScope(cls, targetClass, name, params, methods, options) { cls['__delete__' + name] = fn_delete; + var fn_findById = function (cb) { + var f = this[name].findById; + f.apply(this[name], arguments); + }; + + cls['__findById__' + name] = fn_findById; + var fn_count = function (cb) { var f = this[name].count; f.apply(this[name], arguments); @@ -270,19 +278,29 @@ function defineScope(cls, targetClass, name, params, methods, options) { */ function destroyAll(where, cb) { if (typeof where === 'function') cb = where, where = {}; + var targetModel = definition.targetModel(this._receiver); var scoped = (this._scope && this._scope.where) || {}; var filter = mergeQuery({ where: scoped }, { where: where || {} }); - var targetModel = definition.targetModel(this._receiver); targetModel.destroyAll(filter.where, cb); } + function findById(id, cb) { + var targetModel = definition.targetModel(this._receiver); + var idName = targetModel.definition.idName(); + var where = {}; + where[idName] = id; + var scoped = (this._scope && this._scope.where) || {}; + var filter = mergeQuery({ where: scoped }, { where: where }); + targetModel.findOne(filter, cb); + } + function count(where, cb) { if (typeof where === 'function') cb = where, where = {}; + var targetModel = definition.targetModel(this._receiver); var scoped = (this._scope && this._scope.where) || {}; var filter = mergeQuery({ where: scoped }, { where: where || {} }); - var targetModel = definition.targetModel(this._receiver); targetModel.count(filter.where, cb); } - + return definition; } diff --git a/test/scope.test.js b/test/scope.test.js index b708870d..c7ac2ee1 100644 --- a/test/scope.test.js +++ b/test/scope.test.js @@ -133,6 +133,8 @@ describe('scope - order', function () { describe('scope - filtered count and destroyAll', function () { + var stationA; + before(function () { db = getSchema(); Station = db.define('Station', { @@ -154,7 +156,10 @@ describe('scope - filtered count and destroyAll', function () { }); beforeEach(function (done) { - Station.create({ name: 'a', order: 1 }, done); + Station.create({ name: 'a', order: 1 }, function(err, inst) { + stationA = inst; + done(); + }); }); beforeEach(function (done) { @@ -177,6 +182,22 @@ describe('scope - filtered count and destroyAll', function () { }); }); + it('should find by id - match', function(done) { + Station.active.findById(stationA.id, function(err, station) { + should.not.exist(err); + station.name.should.equal('a'); + done(); + }); + }); + + it('should find by id - no match', function(done) { + Station.inactive.findById(stationA.id, function(err, station) { + should.not.exist(err); + should.not.exist(station); + done(); + }); + }); + it('should count all in scope - active', function(done) { Station.active.count(function(err, count) { should.not.exist(err);