diff --git a/lib/loopback.js b/lib/loopback.js index 568441d7..0f39a56d 100644 --- a/lib/loopback.js +++ b/lib/loopback.js @@ -10,9 +10,7 @@ var express = require('express') , proto = require('./application') , utils = require('express/node_modules/connect').utils , DataSource = require('loopback-datasource-juggler').DataSource - , ModelBuilder = require('loopback-datasource-juggler').ModelBuilder - , assert = require('assert') - , i8n = require('inflection'); + , assert = require('assert'); /** * `loopback` is the main entry for LoopBack core module. It provides static @@ -148,7 +146,7 @@ loopback.createModel = function (name, properties, options) { } catch(e) {} return model; -} +}; /** * Add a remote method to a model. @@ -164,7 +162,7 @@ loopback.remoteMethod = function (fn, options) { }); } fn.http = fn.http || {verb: 'get'}; -} +}; /** * Create a template helper. @@ -180,7 +178,7 @@ loopback.template = function (file) { var templates = this._templates || (this._templates = {}); var str = templates[file] || (templates[file] = fs.readFileSync(file, 'utf8')); return ejs.compile(str); -} +}; /** * Get an in-memory data source. Use one if it already exists. @@ -202,7 +200,7 @@ loopback.memory = function (name) { } return memory; -} +}; /** * Look up a model class by name from all models created by loopback.createModel() @@ -246,7 +244,7 @@ loopback.setDefaultDataSourceForType = function(type, dataSource) { defaultDataSources[type] = dataSource; return dataSource; -} +}; /** * Get the default `dataSource` for a given `type`. @@ -256,7 +254,7 @@ loopback.setDefaultDataSourceForType = function(type, dataSource) { loopback.getDefaultDataSourceForType = function(type) { return this.defaultDataSources && this.defaultDataSources[type]; -} +}; /** * Attach any model that does not have a dataSource to @@ -275,7 +273,7 @@ loopback.autoAttach = function() { loopback.autoAttachModel(ModelCtor); } }); -} +}; loopback.autoAttachModel = function(ModelCtor) { if(ModelCtor.autoAttach) { @@ -287,7 +285,7 @@ loopback.autoAttachModel = function(ModelCtor) { ModelCtor.attachTo(ds); } -} +}; /* * Built in models / services diff --git a/lib/models/access-token.js b/lib/models/access-token.js index e920cde9..f5f66d20 100644 --- a/lib/models/access-token.js +++ b/lib/models/access-token.js @@ -53,7 +53,14 @@ var AccessToken = module.exports = Model.extend('AccessToken', properties, { property: 'create', permission: 'ALLOW' } - ] + ], + relations: { + user: { + type: 'belongsTo', + model: 'User', + foreignKey: 'userId' + } + } }); /** diff --git a/lib/models/user.js b/lib/models/user.js index b6ed1273..f35053b3 100644 --- a/lib/models/user.js +++ b/lib/models/user.js @@ -53,7 +53,7 @@ var options = { { principalType: ACL.ROLE, principalId: Role.EVERYONE, - permission: ACL.DENY, + permission: ACL.DENY }, { principalType: ACL.ROLE, @@ -91,7 +91,14 @@ var options = { permission: ACL.ALLOW, property: "updateAttributes" } - ] + ], + relations: { + accessTokens: { + type: 'hasMany', + model: 'AccessToken', + foreignKey: 'userId' + } + } }; /** @@ -182,7 +189,7 @@ User.login = function (credentials, include, fn) { fn(defaultError); } }); -} +}; /** * Logout a user with the given accessToken id. diff --git a/test/role.test.js b/test/role.test.js index 163e5610..3c43bc9e 100644 --- a/test/role.test.js +++ b/test/role.test.js @@ -16,6 +16,8 @@ describe('role model', function () { beforeEach(function() { ds = loopback.createDataSource({connector: 'memory'}); + // Re-attach the models so that they can have isolated store to avoid + // pollutions from other tests User.attachTo(ds); Role.attachTo(ds); RoleMapping.attachTo(ds); diff --git a/test/user.test.js b/test/user.test.js index 9a056180..81764783 100644 --- a/test/user.test.js +++ b/test/user.test.js @@ -4,7 +4,7 @@ var passport = require('passport'); var MailConnector = require('../lib/connectors/mail'); var userMemory = loopback.createDataSource({ - connector: loopback.Memory + connector: 'memory' }); describe('User', function(){ @@ -13,12 +13,13 @@ describe('User', function(){ User = loopback.User.extend('user'); User.email = loopback.Email.extend('email'); loopback.autoAttach(); + + // Update the AccessToken relation to use the subclass of User + AccessToken.belongsTo(User); // allow many User.afterRemote's to be called User.setMaxListeners(0); - - User.hasMany(AccessToken, {as: 'accessTokens', foreignKey: 'userId'}); - AccessToken.belongsTo(User); + }); beforeEach(function (done) {