From ad22b025a6c3ac79f7f1bc0ad03498d4f5afb6c2 Mon Sep 17 00:00:00 2001 From: Raymond Feng Date: Wed, 16 Jul 2014 16:36:43 -0700 Subject: [PATCH 1/4] Move relation remoting to loopback --- lib/relation-definition.js | 56 -------------------------------------- 1 file changed, 56 deletions(-) diff --git a/lib/relation-definition.js b/lib/relation-definition.js index 13d1914e..a4d15773 100644 --- a/lib/relation-definition.js +++ b/lib/relation-definition.js @@ -387,36 +387,12 @@ RelationDefinition.hasMany = function hasMany(modelFrom, modelTo, params) { }; var findByIdFunc = scopeMethods.findById; - findByIdFunc.shared = true; - findByIdFunc.http = {verb: 'get', path: '/' + relationName + '/:fk'}; - findByIdFunc.accepts = {arg: 'fk', type: 'any', - description: 'Foreign key for ' + relationName, required: true, - http: {source: 'path'}}; - findByIdFunc.description = 'Find a related item by id for ' + relationName; - findByIdFunc.returns = {arg: 'result', type: 'object', root: true}; - modelFrom.prototype['__findById__' + relationName] = findByIdFunc; var destroyByIdFunc = scopeMethods.destroy; - destroyByIdFunc.shared = true; - destroyByIdFunc.http = {verb: 'delete', path: '/' + relationName + '/:fk'}; - destroyByIdFunc.accepts = {arg: 'fk', type: 'any', - description: 'Foreign key for ' + relationName, required: true, - http: {source: 'path'}}; - destroyByIdFunc.description = 'Delete a related item by id for ' + relationName; - destroyByIdFunc.returns = {}; - modelFrom.prototype['__destroyById__' + relationName] = destroyByIdFunc; var updateByIdFunc = scopeMethods.updateById; - updateByIdFunc.shared = true; - updateByIdFunc.http = {verb: 'put', path: '/' + relationName + '/:fk'}; - updateByIdFunc.accepts = {arg: 'fk', type: 'any', - description: 'Foreign key for ' + relationName, required: true, - http: {source: 'path'}}; - updateByIdFunc.description = 'Update a related item by id for ' + relationName; - updateByIdFunc.returns = {arg: 'result', type: 'object', root: true}; - modelFrom.prototype['__updateById__' + relationName] = updateByIdFunc; if(definition.modelThrough) { @@ -425,42 +401,10 @@ RelationDefinition.hasMany = function hasMany(modelFrom, modelTo, params) { scopeMethods.remove = scopeMethod(definition, 'remove'); var addFunc = scopeMethods.add; - addFunc.shared = true; - addFunc.http = {verb: 'put', path: '/' + relationName + '/rel/:fk'}; - addFunc.accepts = {arg: 'fk', type: 'any', - description: 'Foreign key for ' + relationName, required: true, - http: {source: 'path'}}; - addFunc.description = 'Add a related item by id for ' + relationName; - addFunc.returns = {arg: relationName, type: 'object', root: true}; - modelFrom.prototype['__link__' + relationName] = addFunc; var removeFunc = scopeMethods.remove; - removeFunc.shared = true; - removeFunc.http = {verb: 'delete', path: '/' + relationName + '/rel/:fk'}; - removeFunc.accepts = {arg: 'fk', type: 'any', - description: 'Foreign key for ' + relationName, required: true, - http: {source: 'path'}}; - removeFunc.description = 'Remove the ' + relationName + ' relation to an item by id'; - removeFunc.returns = {}; - modelFrom.prototype['__unlink__' + relationName] = removeFunc; - - // FIXME: [rfeng] How to map a function with callback(err, true|false) to HEAD? - // true --> 200 and false --> 404? - /* - var existsFunc = scopeMethods.exists; - existsFunc.shared = true; - existsFunc.http = {verb: 'head', path: '/' + relationName + '/rel/:fk'}; - existsFunc.accepts = {arg: 'fk', type: 'any', - description: 'Foreign key for ' + relationName, required: true, - http: {source: 'path'}}; - existsFunc.description = 'Check the existence of ' + relationName + ' relation to an item by id'; - existsFunc.returns = {}; - - modelFrom.prototype['__exists__' + relationName] = existsFunc; - */ - } else { scopeMethods.create = scopeMethod(definition, 'create'); scopeMethods.build = scopeMethod(definition, 'build'); From b1a1894635c3d8b16470bbd57aefd08e0d1f393d Mon Sep 17 00:00:00 2001 From: Jaka Hudoklin Date: Mon, 21 Jul 2014 22:39:06 +0200 Subject: [PATCH 2/4] add support for relationship options Signed-off-by: Jaka Hudoklin --- lib/datasource.js | 9 ++++++--- lib/relation-definition.js | 10 +++++++--- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/lib/datasource.js b/lib/datasource.js index 8b5d51ab..774362c7 100644 --- a/lib/datasource.js +++ b/lib/datasource.js @@ -399,7 +399,8 @@ DataSource.prototype.defineRelations = function (modelClass, relations) { var params = { foreignKey: relation.foreignKey, as: name, - model: model + model: model, + options: relation.options }; if (throughModel) { params.through = throughModel; @@ -418,7 +419,8 @@ DataSource.prototype.defineRelations = function (modelClass, relations) { foreignKey: relation.foreignKey, as: name, model: targetModel, - through: model + through: model, + options: relation.options }; modelClass[relation.type].call(modelClass, name, params); } @@ -444,7 +446,8 @@ DataSource.prototype.defineRelations = function (modelClass, relations) { var params = { foreignKey: r.foreignKey, as: rn, - model: targetModel + model: targetModel, + options: r.options }; if (throughModel) { params.through = throughModel; diff --git a/lib/relation-definition.js b/lib/relation-definition.js index 13d1914e..1b590bf3 100644 --- a/lib/relation-definition.js +++ b/lib/relation-definition.js @@ -70,6 +70,7 @@ function RelationDefinition(definition) { this.keyThrough = definition.keyThrough; this.multiple = (this.type !== 'belongsTo' && this.type !== 'hasOne'); this.properties = definition.properties || {}; + this.options = definition.options || {}; this.scope = definition.scope; } @@ -363,7 +364,8 @@ RelationDefinition.hasMany = function hasMany(modelFrom, modelTo, params) { modelTo: modelTo, multiple: true, properties: params.properties, - scope: params.scope + scope: params.scope, + options: params.options }); if (params.through) { @@ -872,7 +874,8 @@ RelationDefinition.belongsTo = function (modelFrom, modelTo, params) { modelFrom: modelFrom, keyFrom: fk, keyTo: idName, - modelTo: modelTo + modelTo: modelTo, + options: params.options }); modelFrom.dataSource.defineForeignKey(modelFrom.modelName, fk, modelTo.modelName); @@ -1082,7 +1085,8 @@ RelationDefinition.hasOne = function (modelFrom, modelTo, params) { keyFrom: pk, keyTo: fk, modelTo: modelTo, - properties: params.properties + properties: params.properties, + options: params.options }); modelFrom.dataSource.defineForeignKey(modelTo.modelName, fk, modelFrom.modelName); From 11679beb14d4a78e2944e81b2c0dda71cb5399d6 Mon Sep 17 00:00:00 2001 From: Jaka Hudoklin Date: Mon, 21 Jul 2014 22:39:29 +0200 Subject: [PATCH 3/4] add support for disabling relationship includes Signed-off-by: Jaka Hudoklin --- lib/include.js | 6 ++++++ test/include.test.js | 34 +++++++++++++++++++++++++++++++++- 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/lib/include.js b/lib/include.js index f276b3d9..342a2bde 100644 --- a/lib/include.js +++ b/lib/include.js @@ -101,6 +101,12 @@ Inclusion.include = function (objects, include, cb) { return; } + // Just skip if inclusion is disabled + if (relation.options.disableInclude) { + cb(); + return; + } + // Calling the relation method for each object async.each(objs, function (obj, callback) { if(relation.type === 'belongsTo') { diff --git a/test/include.test.js b/test/include.test.js index 72980903..394862a2 100644 --- a/test/include.test.js +++ b/test/include.test.js @@ -1,7 +1,7 @@ // This test written in mocha+should.js var should = require('./init.js'); -var db, User, Post, Passport, City, Street, Building, Assembly, Part; +var db, User, AccessToken, Post, Passport, City, Street, Building, Assembly, Part; describe('include', function () { @@ -141,6 +141,19 @@ describe('include', function () { }); }); + it('should not fetch User - AccessTokens', function (done) { + User.find({include: ['accesstokens']}, function (err, users) { + should.not.exist(err); + should.exist(users); + users.length.should.be.ok; + users.forEach(function (user) { + var userObj = user.toJSON(); + userObj.should.not.have.property('accesstokens'); + }); + done(); + }); + }); + it('should support hasAndBelongsToMany', function (done) { Assembly.destroyAll(function(err) { @@ -185,6 +198,9 @@ function setup(done) { name: String, age: Number }); + AccessToken = db.define('AccessToken', { + token: String + }); Passport = db.define('Passport', { number: String }); @@ -195,6 +211,10 @@ function setup(done) { Passport.belongsTo('owner', {model: User}); User.hasMany('passports', {foreignKey: 'ownerId'}); User.hasMany('posts', {foreignKey: 'userId'}); + User.hasMany('accesstokens', { + foreignKey: 'userId', + options: {disableInclude: true} + }); Post.belongsTo('author', {model: User, foreignKey: 'userId'}); Assembly = db.define('Assembly', { @@ -226,10 +246,22 @@ function setup(done) { function (items) { createdUsers = items; createPassports(); + createAccessTokens(); } ); } + function createAccessTokens() { + clearAndCreate( + AccessToken, + [ + {token: '1', userId: createdUsers[0].id}, + {token: '2', userId: createdUsers[1].id} + ], + function (items) {} + ); + } + function createPassports() { clearAndCreate( Passport, From cbab9d754a45ac97b6e8e7af149a37eb79f35274 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20Bajto=C5=A1?= Date: Tue, 22 Jul 2014 20:29:42 +0200 Subject: [PATCH 4/4] 2.0.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 215a871c..5dd5eb03 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "loopback-datasource-juggler", - "version": "2.0.0-beta5", + "version": "2.0.0", "description": "LoopBack DataSoure Juggler", "keywords": [ "StrongLoop",