Merge pull request #331 from strongloop/make-get-model-throw-when-not-found

[2.0] Make getModel throw when not found
This commit is contained in:
Miroslav Bajtoš 2014-06-16 19:50:07 +02:00
commit 75406f0891
4 changed files with 26 additions and 5 deletions

View File

@ -261,7 +261,7 @@ ACL.resolvePermission = function resolvePermission(acls, req) {
* @return {Object[]} An array of ACLs * @return {Object[]} An array of ACLs
*/ */
ACL.getStaticACLs = function getStaticACLs(model, property) { ACL.getStaticACLs = function getStaticACLs(model, property) {
var modelClass = loopback.getModel(model); var modelClass = loopback.findModel(model);
var staticACLs = []; var staticACLs = [];
if (modelClass && modelClass.settings.acls) { if (modelClass && modelClass.settings.acls) {
modelClass.settings.acls.forEach(function (acl) { modelClass.settings.acls.forEach(function (acl) {
@ -343,7 +343,7 @@ ACL.checkPermission = function checkPermission(principalType, principalId,
acls = acls.concat(dynACLs); acls = acls.concat(dynACLs);
resolved = self.resolvePermission(acls, req); resolved = self.resolvePermission(acls, req);
if(resolved && resolved.permission === ACL.DEFAULT) { if(resolved && resolved.permission === ACL.DEFAULT) {
var modelClass = loopback.getModel(model); var modelClass = loopback.findModel(model);
resolved.permission = (modelClass && modelClass.settings.defaultPermission) || ACL.ALLOW; resolved.permission = (modelClass && modelClass.settings.defaultPermission) || ACL.ALLOW;
} }
callback && callback(null, resolved); callback && callback(null, resolved);

View File

@ -128,7 +128,7 @@ Change.idForModel = function(modelName, modelId) {
*/ */
Change.findOrCreateChange = function(modelName, modelId, callback) { 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 id = this.idForModel(modelName, modelId);
var Change = this; var Change = this;

View File

@ -176,10 +176,26 @@ registry.configureModel = function(ModelCtor, config) {
* @param {String} modelName The model name * @param {String} modelName The model name
* @returns {Model} The model class * @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) * @header loopback.getModel(modelName)
*/ */
registry.getModel = function(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);
}; };
/** /**

View File

@ -107,7 +107,7 @@ describe('loopback', function() {
}); });
assert(loopback.getModel('MyModel') === MyModel); assert(loopback.getModel('MyModel') === MyModel);
assert(loopback.getModel('MyCustomModel') === MyCustomModel); 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 () { it('should be able to get model by type', function () {
var MyModel = loopback.createModel('MyModel', {}, { var MyModel = loopback.createModel('MyModel', {}, {
@ -124,6 +124,11 @@ describe('loopback', function() {
assert(loopback.getModelByType(MyModel) === MyCustomModel); assert(loopback.getModelByType(MyModel) === MyCustomModel);
assert(loopback.getModelByType(MyCustomModel) === 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));
});
}); });
}); });