Comparing version 0.5.4 to 0.5.5
@@ -1,2 +0,2 @@ | ||
// Bookshelf.js 0.5.4 | ||
// Bookshelf.js 0.5.5 | ||
// --------------- | ||
@@ -86,3 +86,3 @@ | ||
// Keep in sync with `package.json`. | ||
VERSION: '0.5.4', | ||
VERSION: '0.5.5', | ||
@@ -89,0 +89,0 @@ // Helper method to wrap a series of Bookshelf actions in a `knex` transaction block; |
@@ -31,17 +31,6 @@ // EagerRelation | ||
var relation = this; | ||
return handled | ||
.sync(_.extend({}, options, {parentResponse: this.parentResponse})) | ||
.select() | ||
.then(function(resp) { | ||
var relatedModels = relation.pushModels(relationName, handled, resp); | ||
// If there is a response, fetch additional nested eager relations, if any. | ||
if (resp.length > 0 && options.withRelated) { | ||
return new EagerRelation(relatedModels, resp, relatedData.createModel()) | ||
.fetch(options) | ||
.then(function() { return resp; }); | ||
} | ||
return resp; | ||
}); | ||
.tap(eagerLoadHelper(this, relationName, handled, options)); | ||
}, | ||
@@ -66,4 +55,5 @@ | ||
.select() | ||
.then(this.morphToHandler(relationName, | ||
relatedData.instance('morphTo', Target, {morphName: relationName})))); | ||
.tap(eagerLoadHelper(this, relationName, { | ||
relatedData: relatedData.instance('morphTo', Target, {morphName: relationName}) | ||
}, options))); | ||
} | ||
@@ -73,12 +63,2 @@ return when.all(pending).then(function(resps) { | ||
}); | ||
}, | ||
// Handler for the individual `morphTo` fetches, | ||
// attaching any of the related models onto the parent objects, | ||
// stopping at this level of the eager relation loading. | ||
morphToHandler: function(relationName, relatedData) { | ||
var eager = this; | ||
return function(resp) { | ||
eager.pushModels(relationName, {relatedData: relatedData}, resp); | ||
}; | ||
} | ||
@@ -88,2 +68,39 @@ | ||
// Handles the eager load for both the `morphTo` and regular cases. | ||
var eagerLoadHelper = function(relation, relationName, handled, options) { | ||
return function(resp) { | ||
var relatedModels = relation.pushModels(relationName, handled, resp); | ||
var relatedData = handled.relatedData; | ||
// If there is a response, fetch additional nested eager relations, if any. | ||
if (resp.length > 0 && options.withRelated) { | ||
var relatedModel = relatedData.createModel(); | ||
// If this is a `morphTo` relation, we need to do additional processing | ||
// to ensure we don't try to load any relations that don't look to exist. | ||
if (relatedData.type === 'morphTo') { | ||
var withRelated = filterRelated(relatedModel, options); | ||
if (withRelated.length === 0) return; | ||
options = _.extend({}, options, {withRelated: withRelated}); | ||
} | ||
return new EagerRelation(relatedModels, resp, relatedModel).fetch(options).yield(resp); | ||
} | ||
}; | ||
}; | ||
// Filters the `withRelated` on a `morphTo` relation, to ensure that only valid | ||
// relations are attempted for loading. | ||
var filterRelated = function(relatedModel, options) { | ||
// By this point, all withRelated should be turned into a hash, so it should | ||
// be fairly simple to process by splitting on the dots. | ||
return _.reduce(options.withRelated, function(memo, val) { | ||
for (var key in val) { | ||
var seg = key.split('.')[0]; | ||
if (_.isFunction(relatedModel[seg])) memo.push(val); | ||
} | ||
return memo; | ||
}, []); | ||
}; | ||
}); | ||
@@ -90,0 +107,0 @@ |
{ | ||
"name": "bookshelf", | ||
"version": "0.5.4", | ||
"version": "0.5.5", | ||
"description": "A lightweight ORM for PostgreSQL, MySQL, and SQLite3, influenced by Backbone.js", | ||
@@ -8,3 +8,3 @@ "main": "bookshelf.js", | ||
"test": "mocha -R spec test/index.js", | ||
"doc": "groc -o docs --verbose dialects/**/*.js plugins/*.js bookshelf.js" | ||
"doc": "groc -o docs --verbose dialects/base/collection.js dialects/**/*.js plugins/*.js bookshelf.js" | ||
}, | ||
@@ -11,0 +11,0 @@ "homepage": "http://bookshelfjs.org", |
@@ -317,2 +317,6 @@ var when = require('when'); | ||
it('eager loads beyond the morphTo, where possible', function() { | ||
return new Photos().fetch({log: true, withRelated: ['imageable.authors']}); | ||
}); | ||
}); | ||
@@ -319,0 +323,0 @@ |
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
241628
6580