From 7316048afc1164ed91a004a8f4f58f3603df456f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20Bajto=C5=A1?= Date: Mon, 16 Jun 2014 16:13:24 +0200 Subject: [PATCH] lib/registry: `getModel` throws, add `findModel` Rename `loopback.getModel` to `loopback.findModel`. Implement `loopback.getModel` as a wrapper around `findModel` that throws an error when the model as not found. --- lib/models/acl.js | 4 ++-- lib/models/change.js | 2 +- lib/registry.js | 18 +++++++++++++++++- test/loopback.test.js | 7 ++++++- 4 files changed, 26 insertions(+), 5 deletions(-) diff --git a/lib/models/acl.js b/lib/models/acl.js index a3126b87..2c0abe57 100644 --- a/lib/models/acl.js +++ b/lib/models/acl.js @@ -261,7 +261,7 @@ ACL.resolvePermission = function resolvePermission(acls, req) { * @return {Object[]} An array of ACLs */ ACL.getStaticACLs = function getStaticACLs(model, property) { - var modelClass = loopback.getModel(model); + var modelClass = loopback.findModel(model); var staticACLs = []; if (modelClass && modelClass.settings.acls) { modelClass.settings.acls.forEach(function (acl) { @@ -343,7 +343,7 @@ ACL.checkPermission = function checkPermission(principalType, principalId, acls = acls.concat(dynACLs); resolved = self.resolvePermission(acls, req); if(resolved && resolved.permission === ACL.DEFAULT) { - var modelClass = loopback.getModel(model); + var modelClass = loopback.findModel(model); resolved.permission = (modelClass && modelClass.settings.defaultPermission) || ACL.ALLOW; } callback && callback(null, resolved); diff --git a/lib/models/change.js b/lib/models/change.js index 65420725..e66a8453 100644 --- a/lib/models/change.js +++ b/lib/models/change.js @@ -128,7 +128,7 @@ Change.idForModel = function(modelName, modelId) { */ Change.findOrCreateChange = function(modelName, modelId, callback) { - assert(loopback.getModel(modelName), modelName + ' does not exist'); + assert(loopback.findModel(modelName), modelName + ' does not exist'); var id = this.idForModel(modelName, modelId); var Change = this; diff --git a/lib/registry.js b/lib/registry.js index b3666c0b..59c3d94d 100644 --- a/lib/registry.js +++ b/lib/registry.js @@ -176,10 +176,26 @@ registry.configureModel = function(ModelCtor, config) { * @param {String} modelName The model name * @returns {Model} The model class * + * @header loopback.findModel(modelName) + */ +registry.findModel = function(modelName) { + return this.Model.modelBuilder.models[modelName]; +}; + +/** + * Look up a model class by name from all models created by + * `loopback.createModel()`. Throw an error when no such model exists. + * + * @param {String} modelName The model name + * @returns {Model} The model class + * * @header loopback.getModel(modelName) */ registry.getModel = function(modelName) { - return this.Model.modelBuilder.models[modelName]; + var model = this.findModel(modelName); + if (model) return model; + + throw new Error('Model not found: ' + modelName); }; /** diff --git a/test/loopback.test.js b/test/loopback.test.js index bb0ef95a..c4dcf313 100644 --- a/test/loopback.test.js +++ b/test/loopback.test.js @@ -107,7 +107,7 @@ describe('loopback', function() { }); assert(loopback.getModel('MyModel') === MyModel); assert(loopback.getModel('MyCustomModel') === MyCustomModel); - assert(loopback.getModel('Invalid') === undefined); + assert(loopback.findModel('Invalid') === undefined); }); it('should be able to get model by type', function () { var MyModel = loopback.createModel('MyModel', {}, { @@ -124,6 +124,11 @@ describe('loopback', function() { assert(loopback.getModelByType(MyModel) === MyCustomModel); assert(loopback.getModelByType(MyCustomModel) === MyCustomModel); }); + + it('should throw when the model does not exist', function() { + expect(function() { loopback.getModel(uniqueModelName); }) + .to.throw(Error, new RegExp('Model not found: ' + uniqueModelName)); + }); }); });