diff --git a/lib/models/README.md b/common/models/README.md similarity index 100% rename from lib/models/README.md rename to common/models/README.md diff --git a/lib/models/access-token.js b/common/models/access-token.js similarity index 99% rename from lib/models/access-token.js rename to common/models/access-token.js index 85a9f5f7..8243747d 100644 --- a/lib/models/access-token.js +++ b/common/models/access-token.js @@ -2,7 +2,7 @@ * Module Dependencies. */ -var loopback = require('../loopback') +var loopback = require('../../lib/loopback') , assert = require('assert') , crypto = require('crypto') , uid = require('uid2') diff --git a/lib/models/acl.js b/common/models/acl.js similarity index 99% rename from lib/models/acl.js rename to common/models/acl.js index 81eb93a5..ee33d97d 100644 --- a/lib/models/acl.js +++ b/common/models/acl.js @@ -31,12 +31,12 @@ */ -var loopback = require('../loopback'); +var loopback = require('../../lib/loopback'); var async = require('async'); var assert = require('assert'); var debug = require('debug')('loopback:security:acl'); -var ctx = require('./access-context'); +var ctx = require('../../lib/access-context'); var AccessContext = ctx.AccessContext; var Principal = ctx.Principal; var AccessRequest = ctx.AccessRequest; diff --git a/lib/models/application.js b/common/models/application.js similarity index 99% rename from lib/models/application.js rename to common/models/application.js index 07c101d2..4f3a066d 100644 --- a/lib/models/application.js +++ b/common/models/application.js @@ -1,4 +1,4 @@ -var loopback = require('../loopback'); +var loopback = require('../../lib/loopback'); var assert = require('assert'); // Authentication schemes diff --git a/lib/models/change.js b/common/models/change.js similarity index 99% rename from lib/models/change.js rename to common/models/change.js index bd51faac..ed347eeb 100644 --- a/lib/models/change.js +++ b/common/models/change.js @@ -2,8 +2,8 @@ * Module Dependencies. */ -var PersistedModel = require('./persisted-model') - , loopback = require('../loopback') +var PersistedModel = require('../../lib/loopback').PersistedModel + , loopback = require('../../lib/loopback') , crypto = require('crypto') , CJSON = {stringify: require('canonical-json')} , async = require('async') diff --git a/lib/models/checkpoint.js b/common/models/checkpoint.js similarity index 92% rename from lib/models/checkpoint.js rename to common/models/checkpoint.js index 2d607bf0..9ac1f5b5 100644 --- a/lib/models/checkpoint.js +++ b/common/models/checkpoint.js @@ -2,8 +2,8 @@ * Module Dependencies. */ -var PersistedModel = require('../loopback').PersistedModel - , loopback = require('../loopback') +var PersistedModel = require('../../lib/loopback').PersistedModel + , loopback = require('../../lib/loopback') , assert = require('assert'); /** diff --git a/lib/models/email.js b/common/models/email.js similarity index 94% rename from lib/models/email.js rename to common/models/email.js index fb274065..0d0e7993 100644 --- a/lib/models/email.js +++ b/common/models/email.js @@ -2,8 +2,8 @@ * Module Dependencies. */ -var Model = require('../loopback').Model - , loopback = require('../loopback'); +var Model = require('../../lib/loopback').Model + , loopback = require('../../lib/loopback'); var properties = { to: {type: String, required: true}, diff --git a/lib/models/role.js b/common/models/role.js similarity index 99% rename from lib/models/role.js rename to common/models/role.js index e852b139..1a588040 100644 --- a/lib/models/role.js +++ b/common/models/role.js @@ -1,9 +1,9 @@ -var loopback = require('../loopback'); +var loopback = require('../../lib/loopback'); var debug = require('debug')('loopback:security:role'); var assert = require('assert'); var async = require('async'); -var AccessContext = require('./access-context').AccessContext; +var AccessContext = require('../../lib/access-context').AccessContext; // Role model var RoleSchema = { diff --git a/lib/models/user.js b/common/models/user.js similarity index 99% rename from lib/models/user.js rename to common/models/user.js index 45e4cc60..d4a3ef04 100644 --- a/lib/models/user.js +++ b/common/models/user.js @@ -2,8 +2,8 @@ * Module Dependencies. */ -var PersistedModel = require('../loopback').PersistedModel - , loopback = require('../loopback') +var PersistedModel = require('../../lib/loopback').PersistedModel + , loopback = require('../../lib/loopback') , path = require('path') , SALT_WORK_FACTOR = 10 , crypto = require('crypto') diff --git a/docs.json b/docs.json index c79bfa0d..71f47537 100644 --- a/docs.json +++ b/docs.json @@ -5,22 +5,22 @@ "lib/loopback.js", "lib/runtime.js", "lib/registry.js", -{ "title": "Base models", "depth": 2 }, - "lib/models/model.js", - "lib/models/persisted-model.js", -{ "title": "Middleware", "depth": 2 }, +{ "title": "Base models", "depth": 2 }, + "lib/model.js", + "lib/persisted-model.js", +{ "title": "Middleware", "depth": 2 }, "lib/middleware/rest.js", "lib/middleware/status.js", - "lib/middleware/token.js", + "lib/middleware/token.js", "lib/middleware/urlNotFound.js", -{ "title": "Built-in models", "depth": 2 }, - "lib/models/access-token.js", - "lib/models/acl.js", - "lib/models/application.js", - "lib/models/email.js", - "lib/models/role.js", - "lib/models/user.js", - "lib/models/change.js" +{ "title": "Built-in models", "depth": 2 }, + "common/models/access-token.js", + "common/models/acl.js", + "common/models/application.js", + "common/models/email.js", + "common/models/role.js", + "common/models/user.js", + "common/models/change.js" ], "assets": "/docs/assets" } diff --git a/lib/models/access-context.js b/lib/access-context.js similarity index 98% rename from lib/models/access-context.js rename to lib/access-context.js index 2bf03733..ce8457f1 100644 --- a/lib/models/access-context.js +++ b/lib/access-context.js @@ -1,5 +1,5 @@ -var loopback = require('../loopback'); -var AccessToken = require('./access-token'); +var loopback = require('./loopback'); +var AccessToken = require('../common/models/access-token'); var debug = require('debug')('loopback:security:access-context'); /** @@ -280,7 +280,7 @@ AccessRequest.prototype.exactlyMatches = function(acl) { */ AccessRequest.prototype.isAllowed = function() { - return this.permission !== require('./acl').ACL.DENY; + return this.permission !== require('../common/models/acl').ACL.DENY; } AccessRequest.prototype.debug = function() { diff --git a/lib/builtin-models.js b/lib/builtin-models.js new file mode 100644 index 00000000..f2df14c9 --- /dev/null +++ b/lib/builtin-models.js @@ -0,0 +1,30 @@ +module.exports = function(loopback) { + loopback.Email = require('../common/models/email'); + loopback.User = require('../common/models/user'); + loopback.Application = require('../common/models/application'); + loopback.AccessToken = require('../common/models/access-token'); + loopback.Role = require('../common/models/role').Role; + loopback.RoleMapping = require('../common/models/role').RoleMapping; + loopback.ACL = require('../common/models/acl').ACL; + loopback.Scope = require('../common/models/acl').Scope; + loopback.Change = require('../common/models/change'); + + /*! + * Automatically attach these models to dataSources + */ + + var dataSourceTypes = { + DB: 'db', + MAIL: 'mail' + }; + + loopback.Email.autoAttach = dataSourceTypes.MAIL; + loopback.PersistedModel.autoAttach = dataSourceTypes.DB; + loopback.User.autoAttach = dataSourceTypes.DB; + loopback.AccessToken.autoAttach = dataSourceTypes.DB; + loopback.Role.autoAttach = dataSourceTypes.DB; + loopback.RoleMapping.autoAttach = dataSourceTypes.DB; + loopback.ACL.autoAttach = dataSourceTypes.DB; + loopback.Scope.autoAttach = dataSourceTypes.DB; + loopback.Application.autoAttach = dataSourceTypes.DB; +}; diff --git a/lib/loopback.js b/lib/loopback.js index 1b8168f7..68578da3 100644 --- a/lib/loopback.js +++ b/lib/loopback.js @@ -165,31 +165,4 @@ loopback.template = function (file) { * Built in models / services */ -loopback.Email = require('./models/email'); -loopback.User = require('./models/user'); -loopback.Application = require('./models/application'); -loopback.AccessToken = require('./models/access-token'); -loopback.Role = require('./models/role').Role; -loopback.RoleMapping = require('./models/role').RoleMapping; -loopback.ACL = require('./models/acl').ACL; -loopback.Scope = require('./models/acl').Scope; -loopback.Change = require('./models/change'); - -/*! - * Automatically attach these models to dataSources - */ - -var dataSourceTypes = { - DB: 'db', - MAIL: 'mail' -}; - -loopback.Email.autoAttach = dataSourceTypes.MAIL; -loopback.PersistedModel.autoAttach = dataSourceTypes.DB; -loopback.User.autoAttach = dataSourceTypes.DB; -loopback.AccessToken.autoAttach = dataSourceTypes.DB; -loopback.Role.autoAttach = dataSourceTypes.DB; -loopback.RoleMapping.autoAttach = dataSourceTypes.DB; -loopback.ACL.autoAttach = dataSourceTypes.DB; -loopback.Scope.autoAttach = dataSourceTypes.DB; -loopback.Application.autoAttach = dataSourceTypes.DB; +require('./builtin-models')(loopback); diff --git a/lib/models/model.js b/lib/model.js similarity index 99% rename from lib/models/model.js rename to lib/model.js index af5581fa..054ba65f 100644 --- a/lib/models/model.js +++ b/lib/model.js @@ -1,7 +1,7 @@ /*! * Module Dependencies. */ -var registry = require('../registry'); +var registry = require('./registry'); var assert = require('assert'); var RemoteObjects = require('strong-remoting'); var SharedClass = require('strong-remoting').SharedClass; @@ -250,7 +250,7 @@ Model._ACL = function getACL(ACL) { if(_aclModel) { return _aclModel; } - var aclModel = require('./acl').ACL; + var aclModel = loopback.ACL; _aclModel = registry.getModelByType(aclModel); return _aclModel; }; @@ -268,7 +268,7 @@ Model._ACL = function getACL(ACL) { */ Model.checkAccess = function(token, modelId, sharedMethod, ctx, callback) { - var ANONYMOUS = require('./access-token').ANONYMOUS; + var ANONYMOUS = loopback.AccessToken.ANONYMOUS; token = token || ANONYMOUS; var aclModel = Model._ACL(); diff --git a/lib/models/index.js b/lib/models/index.js deleted file mode 100644 index ec204a42..00000000 --- a/lib/models/index.js +++ /dev/null @@ -1,12 +0,0 @@ -exports.Model = require('./model'); -exports.Email = require('./email'); -exports.User = require('./user'); -exports.AccessToken = require('./access-token'); - -exports.Application = require('./application'); -exports.ACL = require('./acl'); -exports.Role = require('./role'); - - - - diff --git a/lib/models/persisted-model.js b/lib/persisted-model.js similarity index 99% rename from lib/models/persisted-model.js rename to lib/persisted-model.js index ab55caa7..8b98e71c 100644 --- a/lib/models/persisted-model.js +++ b/lib/persisted-model.js @@ -3,7 +3,7 @@ */ var Model = require('./model'); -var runtime = require('../runtime'); +var runtime = require('./runtime'); var assert = require('assert'); var async = require('async'); @@ -960,7 +960,7 @@ PersistedModel.enableChangeTracking = function() { } PersistedModel._defineChangeModel = function() { - var BaseChangeModel = require('./change'); + var BaseChangeModel = require('./../common/models/change'); return this.Change = BaseChangeModel.extend(this.modelName + '-change', {}, { diff --git a/lib/registry.js b/lib/registry.js index f7a03a1f..0b859718 100644 --- a/lib/registry.js +++ b/lib/registry.js @@ -371,8 +371,8 @@ registry.DataSource = DataSource; * @private */ -registry.Model = require('./models/model'); -registry.PersistedModel = require('./models/persisted-model'); +registry.Model = require('./model'); +registry.PersistedModel = require('./persisted-model'); // temporary alias to simplify migration of code based on <=2.0.0-beta3 Object.defineProperty(registry, 'DataModel', { diff --git a/test/acl.test.js b/test/acl.test.js index ba97dda9..eb37a74f 100644 --- a/test/acl.test.js +++ b/test/acl.test.js @@ -1,11 +1,9 @@ var assert = require('assert'); var loopback = require('../index'); -var acl = require('../lib/models/acl'); -var Scope = acl.Scope; -var ACL = acl.ACL; -var role = require('../lib/models/role'); -var Role = role.Role; -var RoleMapping = role.RoleMapping; +var Scope = loopback.Scope; +var ACL = loopback.ACL; +var Role = loopback.Role; +var RoleMapping = loopback.RoleMapping; var User = loopback.User; var testModel; diff --git a/test/checkpoint.test.js b/test/checkpoint.test.js index 53a9a988..c999729f 100644 --- a/test/checkpoint.test.js +++ b/test/checkpoint.test.js @@ -2,7 +2,7 @@ var async = require('async'); var loopback = require('../'); // create a unique Checkpoint model -var Checkpoint = require('../lib/models/checkpoint').extend('TestCheckpoint'); +var Checkpoint = require('../common/models/checkpoint').extend('TestCheckpoint'); Checkpoint.attachTo(loopback.memory()); describe('Checkpoint', function() { diff --git a/test/role.test.js b/test/role.test.js index 9252743e..7e02f7ab 100644 --- a/test/role.test.js +++ b/test/role.test.js @@ -1,10 +1,9 @@ var assert = require('assert'); var loopback = require('../index'); -var role = require('../lib/models/role'); -var Role = role.Role; -var RoleMapping = role.RoleMapping; +var Role = loopback.Role; +var RoleMapping = loopback.RoleMapping; var User = loopback.User; -var ACL = require('../lib/models/acl'); +var ACL = loopback.ACL; function checkResult(err, result) { // console.log(err, result);