Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[2.0] Make getModel throw when not found #331

Merged
merged 1 commit into from
Jun 16, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/models/acl.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion lib/models/change.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
18 changes: 17 additions & 1 deletion lib/registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};

/**
Expand Down
7 changes: 6 additions & 1 deletion test/loopback.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', {}, {
Expand All @@ -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));
});
});
});

Expand Down