Comparing version 0.5.3 to 0.5.4
@@ -1,2 +0,2 @@ | ||
// Bookshelf.js 0.5.3 | ||
// Bookshelf.js 0.5.4 | ||
// --------------- | ||
@@ -86,3 +86,3 @@ | ||
// Keep in sync with `package.json`. | ||
VERSION: '0.5.3', | ||
VERSION: '0.5.4', | ||
@@ -89,0 +89,0 @@ // Helper method to wrap a series of Bookshelf actions in a `knex` transaction block; |
@@ -30,16 +30,13 @@ // Relation | ||
// If the parent object is eager loading, we don't need the | ||
// id attribute, because we'll just be creating a `whereIn` from the | ||
// previous response anyway. | ||
if (!parent._isEager) { | ||
if (this.isInverse()) { | ||
if (this.type === 'morphTo') { | ||
this.target = Helpers.morphCandidate(this.candidates, parent.get(this.key('morphKey'))); | ||
this.targetTableName = _.result(this.target.prototype, 'tableName'); | ||
this.targetIdAttribute = _.result(this.target.prototype, 'idAttribute'); | ||
} | ||
this.parentFk = parent.get(this.key('foreignKey')); | ||
} else { | ||
this.parentFk = parent.id; | ||
if (this.isInverse()) { | ||
// If the parent object is eager loading, and it's a polymorphic `morphTo` relation, | ||
// we can't know what the target will be until the models are sorted and matched. | ||
if (this.type === 'morphTo' && !parent._isEager) { | ||
this.target = Helpers.morphCandidate(this.candidates, parent.get(this.key('morphKey'))); | ||
this.targetTableName = _.result(this.target.prototype, 'tableName'); | ||
this.targetIdAttribute = _.result(this.target.prototype, 'idAttribute'); | ||
} | ||
this.parentFk = parent.get(this.key('foreignKey')); | ||
} else { | ||
this.parentFk = parent.id; | ||
} | ||
@@ -237,7 +234,7 @@ | ||
// we're handling, for easy attachment to the parent models. | ||
eagerPair: function(relationName, related, models) { | ||
eagerPair: function(relationName, related, parentModels) { | ||
// If this is a morphTo, we only want to pair on the morphValue for the current relation. | ||
if (this.type === 'morphTo') { | ||
models = _.filter(models, function(model) { return model.get(this.key('morphKey')) === this.key('morphValue'); }, this); | ||
parentModels = _.filter(parentModels, function(model) { return model.get(this.key('morphKey')) === this.key('morphValue'); }, this); | ||
} | ||
@@ -248,2 +245,3 @@ | ||
// Group all of the related models for easier association with their parent models. | ||
var grouped = _.groupBy(related, function(model) { | ||
@@ -254,6 +252,9 @@ return model.pivot ? model.pivot.get(this.key('foreignKey')) : | ||
for (var i = 0, l = models.length; i < l; i++) { | ||
var model = models[i]; | ||
// Loop over the `parentModels` and attach the appropriated grouped sub-models, | ||
// keeping the appropriated `relatedData` on the new related instance. | ||
for (var i = 0, l = parentModels.length; i < l; i++) { | ||
var model = parentModels[i]; | ||
var groupedKey = this.isInverse() ? model.get(this.key('foreignKey')) : model.id; | ||
model.relations[relationName] = this.relatedInstance(grouped[groupedKey]); | ||
var relation = model.relations[relationName] = this.relatedInstance(grouped[groupedKey]); | ||
relation.relatedData = this; | ||
} | ||
@@ -260,0 +261,0 @@ return related; |
{ | ||
"name": "bookshelf", | ||
"version": "0.5.3", | ||
"version": "0.5.4", | ||
"description": "A lightweight ORM for PostgreSQL, MySQL, and SQLite3, influenced by Backbone.js", | ||
@@ -5,0 +5,0 @@ "main": "bookshelf.js", |
@@ -431,5 +431,25 @@ var when = require('when'); | ||
describe('Issue #77 - Using collection.create() on relations', function() { | ||
it('maintains the correct parent model references when using related()', function() { | ||
return new Site().fetch({withRelated: 'authors'}).then(function(site) { | ||
return site.related('authors').create({first_name: 'Dummy', last_name: 'Account'}).then(function(model) { | ||
expect(model.attributes).to.eql({first_name: 'Dummy', last_name: 'Account', site_id: site.id, id: model.id}); | ||
expect(site.related('authors')).to.have.length(3); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}; |
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
230083
6204