Comparing version 0.5.7 to 0.5.8
@@ -1,2 +0,2 @@ | ||
// Bookshelf.js 0.5.7 | ||
// Bookshelf.js 0.5.8 | ||
// --------------- | ||
@@ -86,3 +86,3 @@ | ||
// Keep in sync with `package.json`. | ||
VERSION: '0.5.7', | ||
VERSION: '0.5.8', | ||
@@ -89,0 +89,0 @@ // Helper method to wrap a series of Bookshelf actions in a `knex` transaction block; |
@@ -21,3 +21,2 @@ // Eager Base | ||
this.parentResponse = parentResponse; | ||
_.bindAll(this, 'pushModels', 'eagerFetch'); | ||
}; | ||
@@ -24,0 +23,0 @@ |
@@ -33,8 +33,9 @@ // Base Relation | ||
// Creates a new model, used internally in the eager fetch helper methods. | ||
// Creates a new, unparsed model, used internally in the eager fetch helper | ||
// methods. (Parsing may mutate information necessary for eager pairing.) | ||
createModel: function(data) { | ||
if (this.target.prototype instanceof CollectionBase) { | ||
return new this.target.prototype.model(data, {parse: true})._reset(); | ||
return new this.target.prototype.model(data)._reset(); | ||
} | ||
return new this.target(data, {parse: true})._reset(); | ||
return new this.target(data)._reset(); | ||
}, | ||
@@ -41,0 +42,0 @@ |
@@ -43,3 +43,3 @@ // EagerRelation | ||
var groups = _.groupBy(this.parent, function(m) { | ||
return m.get(relationName + '_type'); | ||
return m.get(relatedData.morphName + '_type'); | ||
}); | ||
@@ -51,3 +51,4 @@ for (var group in groups) { | ||
.query('whereIn', | ||
_.result(target, 'idAttribute'), _.uniq(_.invoke(groups[group], 'get', relationName + '_id')) | ||
_.result(target, 'idAttribute'), | ||
_.uniq(_.invoke(groups[group], 'get', relatedData.morphName + '_id')) | ||
) | ||
@@ -57,3 +58,3 @@ .sync(options) | ||
.tap(eagerLoadHelper(this, relationName, { | ||
relatedData: relatedData.instance('morphTo', Target, {morphName: relationName}) | ||
relatedData: relatedData.instance('morphTo', Target, {morphName: relatedData.morphName}) | ||
}, options))); | ||
@@ -60,0 +61,0 @@ } |
@@ -159,3 +159,5 @@ // Model | ||
// If there are any save constraints, set them on the model. | ||
if (this.relatedData) this.relatedData.saveConstraints(this); | ||
if (this.relatedData && this.relatedData.type !== 'morphTo') { | ||
Helpers.saveConstraints(this, this.relatedData); | ||
} | ||
@@ -162,0 +164,0 @@ var sync = this.sync(options); |
@@ -180,3 +180,4 @@ // Relation | ||
} else { | ||
key = this.isInverse() ? this.targetIdAttribute : this.key('foreignKey'); | ||
key = this.targetTableName + '.' + | ||
(this.isInverse() ? this.targetIdAttribute : this.key('foreignKey')); | ||
} | ||
@@ -187,3 +188,3 @@ | ||
if (this.isMorph()) { | ||
knex.where(this.key('morphKey'), this.key('morphValue')); | ||
knex.where(this.targetTableName + '.' + this.key('morphKey'), this.key('morphValue')); | ||
} | ||
@@ -216,3 +217,3 @@ }, | ||
if (this.isSingle()) { | ||
if (!Target.prototype instanceof ModelBase) { | ||
if (!(Target.prototype instanceof ModelBase)) { | ||
throw new Error('The `'+this.type+'` related object must be a Bookshelf.Model'); | ||
@@ -237,6 +238,9 @@ } | ||
eagerPair: function(relationName, related, parentModels) { | ||
var model; | ||
// If this is a morphTo, we only want to pair on the morphValue for the current relation. | ||
if (this.type === 'morphTo') { | ||
parentModels = _.filter(parentModels, 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); | ||
} | ||
@@ -253,6 +257,6 @@ | ||
// Loop over the `parentModels` and attach the appropriated grouped sub-models, | ||
// keeping the appropriated `relatedData` on the new related instance. | ||
// Loop over the `parentModels` and attach the grouped sub-models, | ||
// keeping the `relatedData` on the new related instance. | ||
for (var i = 0, l = parentModels.length; i < l; i++) { | ||
var model = parentModels[i]; | ||
model = parentModels[i]; | ||
var groupedKey = this.isInverse() ? model.get(this.key('foreignKey')) : model.id; | ||
@@ -262,2 +266,10 @@ var relation = model.relations[relationName] = this.relatedInstance(grouped[groupedKey]); | ||
} | ||
// Now that related models have been successfully paired, update each with | ||
// its parsed attributes | ||
for (i = 0, l = related.length; i < l; i++) { | ||
model = related[i]; | ||
model.attributes = model.parse(model.attributes); | ||
} | ||
return related; | ||
@@ -264,0 +276,0 @@ }, |
{ | ||
"name": "bookshelf", | ||
"version": "0.5.7", | ||
"version": "0.5.8", | ||
"description": "A lightweight ORM for PostgreSQL, MySQL, and SQLite3, influenced by Backbone.js", | ||
@@ -5,0 +5,0 @@ "main": "bookshelf.js", |
@@ -6,2 +6,10 @@ | ||
function _parsed (attributes) { | ||
var parsed = {}; | ||
Object.keys(attributes).forEach(function (name) { | ||
parsed[name + "_parsed"] = attributes[name]; | ||
}); | ||
return parsed; | ||
} | ||
var Info = Bookshelf.Model.extend({ | ||
@@ -46,2 +54,7 @@ tableName: 'info' | ||
// A SiteParsed appends "_parsed" to each field name on fetch | ||
var SiteParsed = Site.extend({ | ||
parse: _parsed | ||
}); | ||
var Sites = Bookshelf.Collection.extend({ | ||
@@ -81,2 +94,7 @@ model: Site | ||
// A AuthorParsed appends "_parsed" to each field name on fetch | ||
var AuthorParsed = Author.extend({ | ||
parse: _parsed | ||
}); | ||
var Authors = Bookshelf.Collection.extend({ | ||
@@ -98,2 +116,5 @@ model: Author | ||
}, | ||
parsedPosts: function () { | ||
return this.hasMany(PostParsed); | ||
}, | ||
validate: function(attrs) { | ||
@@ -135,2 +156,7 @@ if (!attrs.title) return 'A title is required.'; | ||
// A PostParsed appends "_parsed" to each field name on fetch | ||
var PostParsed = Post.extend({ | ||
parse: _parsed | ||
}); | ||
var Posts = Bookshelf.Collection.extend({ | ||
@@ -182,3 +208,6 @@ model: Post | ||
return this.morphTo('imageable', Site, Author); | ||
} | ||
}, | ||
imageableParsed: function() { | ||
return this.morphTo('imageable', SiteParsed, AuthorParsed); | ||
} | ||
}); | ||
@@ -217,7 +246,10 @@ | ||
Site: Site, | ||
SiteParsed: SiteParsed, | ||
SiteMeta: SiteMeta, | ||
Admin: Admin, | ||
Author: Author, | ||
AuthorParsed: AuthorParsed, | ||
Blog: Blog, | ||
Post: Post, | ||
PostParsed: PostParsed, | ||
Comment: Comment, | ||
@@ -224,0 +256,0 @@ Tag: Tag, |
@@ -110,3 +110,3 @@ var equal = require('assert').equal; | ||
equal(_knex.toString(), 'select `doctormeta`.* from `doctormeta` where `doctoring_id` = 1 limit 1'); | ||
equal(_knex.toString(), 'select `doctormeta`.* from `doctormeta` where `doctormeta`.`doctoring_id` = 1 limit 1'); | ||
}); | ||
@@ -259,3 +259,3 @@ | ||
var sql = "select `photos`.* from `photos` where `imageable_id` = 1 and `imageable_type` = 'doctors'"; | ||
var sql = "select `photos`.* from `photos` where `photos`.`imageable_id` = 1 and `photos`.`imageable_type` = 'doctors'"; | ||
@@ -262,0 +262,0 @@ equal(_knex.toString(), sql); |
@@ -0,1 +1,2 @@ | ||
var _ = require('underscore'); | ||
var when = require('when'); | ||
@@ -455,5 +456,40 @@ var equal = require('assert').equal; | ||
describe('Issue #97 - Eager loading on parsed models', function() { | ||
it('correctly pairs eager-loaded models before parse()', function () { | ||
return when.all([ | ||
new Blog({id: 1}).related('parsedPosts').fetch(), | ||
new Blog({id: 1}).fetch({ withRelated: 'parsedPosts' }) | ||
]).then(function (data) { | ||
var parsedPosts = data[0], blog = data[1]; | ||
expect(blog.related('parsedPosts').length).to.equal(parsedPosts.length); | ||
}); | ||
}); | ||
it('parses eager-loaded models after pairing', function () { | ||
return new Blog({id: 1}).fetch({ withRelated: 'parsedPosts' }) | ||
.then(function (blog) { | ||
var attrs = blog.related('parsedPosts').at(0).attributes; | ||
Object.keys(attrs).forEach(function (key) { | ||
expect(/_parsed$/.test(key)).to.be.true; | ||
}); | ||
}); | ||
}); | ||
it('parses eager-loaded morphTo relations (model)', function () { | ||
return new Photos().fetch({ withRelated: 'imageableParsed.meta', log: true }) | ||
.then(function (photos) { | ||
photos.forEach(function(photo) { | ||
var attrs = photo.related('imageableParsed').attributes; | ||
Object.keys(attrs).forEach(function (key) { | ||
expect(/_parsed$/.test(key)).to.be.true; | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}; |
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
254880
6963