Comparing version 0.6.11 to 0.6.12
@@ -1,2 +0,2 @@ | ||
// Bookshelf.js 0.6.11 | ||
// Bookshelf.js 0.6.12 | ||
// --------------- | ||
@@ -90,3 +90,3 @@ | ||
// Keep in sync with `package.json`. | ||
VERSION: '0.6.11', | ||
VERSION: '0.6.12', | ||
@@ -93,0 +93,0 @@ // Helper method to wrap a series of Bookshelf actions in a `knex` transaction block; |
@@ -114,3 +114,3 @@ // Model | ||
.then(function() { | ||
return [this.toJSON({shallow: true})]; | ||
return [this.format(this.attributes)]; | ||
}) | ||
@@ -117,0 +117,0 @@ .then(handleEager(_.extend({}, options, { |
@@ -256,5 +256,9 @@ // Relation | ||
model = parentModels[i]; | ||
var groupedKey = !this.isInverse() ? model.id : | ||
this.isThrough() ? model.get(this.key('throughForeignKey')) : | ||
model.get(this.key('foreignKey')); | ||
var groupedKey; | ||
if (!this.isInverse()) { | ||
groupedKey = model.id; | ||
} else { | ||
var formatted = model.format(model.attributes); | ||
groupedKey = this.isThrough() ? formatted[this.key('throughForeignKey')] : formatted[this.key('foreignKey')]; | ||
} | ||
var relation = model.relations[relationName] = this.relatedInstance(grouped[groupedKey]); | ||
@@ -261,0 +265,0 @@ relation.relatedData = this; |
{ | ||
"name": "bookshelf", | ||
"version": "0.6.11", | ||
"version": "0.6.12", | ||
"description": "A lightweight ORM for PostgreSQL, MySQL, and SQLite3, influenced by Backbone.js", | ||
"main": "bookshelf.js", | ||
"scripts": { | ||
"test": "mocha --check-leaks -R spec test/index.js", | ||
"test": "mocha -t 5000 --check-leaks -R spec test/index.js", | ||
"doc": "groc -o docs --verbose dialects/base/collection.js dialects/**/*.js plugins/*.js bookshelf.js", | ||
@@ -9,0 +9,0 @@ "release:patch": "git checkout master && export BOOKSHELF_DEV=0 && npm run-script test && npm run-script doc && git add . && git commit -m 'docs prep for release' && grunt release:patch", |
@@ -208,4 +208,8 @@ var Promise = global.testPromise; | ||
{id: 10, name: 'computers'}, | ||
]) | ||
]), | ||
knex('parsed_users').insert({id: 10, name: 'test'}), | ||
knex('tokens').insert({parsed_user_id: 10, token: 'testing'}) | ||
]).then(null, function(e) { | ||
@@ -212,0 +216,0 @@ console.log(e.stack); |
@@ -9,3 +9,4 @@ var _ = require('lodash'); | ||
'users', 'roles', 'photos', 'users_roles', 'info', | ||
'Customer', 'Settings', 'hostnames', 'instances', 'uuid_test' | ||
'Customer', 'Settings', 'hostnames', 'instances', 'uuid_test', | ||
'parsed_users', 'tokens' | ||
]; | ||
@@ -148,2 +149,13 @@ | ||
table.string('name'); | ||
}), | ||
schema.createTable('parsed_users', function(table) { | ||
table.increments(); | ||
table.string('name'); | ||
}), | ||
schema.createTable('tokens', function(table) { | ||
table.increments(); | ||
table.string('parsed_user_id'); | ||
table.string('token'); | ||
}) | ||
@@ -150,0 +162,0 @@ |
@@ -6,2 +6,5 @@ | ||
var _ = require('lodash'); | ||
_.str = require('underscore.string'); | ||
function _parsed (attributes) { | ||
@@ -246,2 +249,28 @@ var parsed = {}; | ||
var ParsedModel = Bookshelf.Model.extend({ | ||
format: function (attrs) { | ||
return _.transform(attrs, function (result, val, key) { | ||
result[_.str.underscored(key)] = val; | ||
}); | ||
}, | ||
parse: function (attrs) { | ||
return _.transform(attrs, function (result, val, key) { | ||
result[_.str.camelize(key)] = val; | ||
}); | ||
} | ||
}); | ||
// Has columns: id, user_id, token | ||
var UserTokenParsed = ParsedModel.extend({ | ||
tableName: 'tokens', | ||
user: function () { | ||
return this.belongsTo(UserParsed); | ||
} | ||
}); | ||
// Has columns: id, email, first_name, last_name | ||
var UserParsed = ParsedModel.extend({ | ||
tableName: 'parsed_users', | ||
}); | ||
return { | ||
@@ -261,2 +290,4 @@ Models: { | ||
User: User, | ||
UserParsed: UserParsed, | ||
UserTokenParsed: UserTokenParsed, | ||
Role: Role, | ||
@@ -263,0 +294,0 @@ Photo: Photo, |
@@ -30,2 +30,5 @@ var _ = require('lodash'); | ||
var UserParsed = Models.UserParsed; | ||
var UserTokenParsed = Models.UserTokenParsed; | ||
// Collections | ||
@@ -581,3 +584,3 @@ var Sites = Collections.Sites; | ||
describe('Issue #97 - Eager loading on parsed models', function() { | ||
describe('Issue #97, #377 - Eager loading on parsed models', function() { | ||
@@ -616,2 +619,19 @@ it('correctly pairs eager-loaded models before parse()', function () { | ||
it('eager fetches belongsTo correctly on a dual parse', function() { | ||
return UserTokenParsed.forge({token: 'testing'}).fetch({ | ||
withRelated: ['user'] | ||
}).then(function (model) { | ||
expect(model.related('user').get('id')).to.equal(10); | ||
}); | ||
}); | ||
it('eager fetches belongsTo correctly on a dual parse', function() { | ||
return UserTokenParsed.forge({token: 'testing'}).fetch().then(function(model) { | ||
return model.load('user'); | ||
}).then(function(model) { | ||
expect(model.related('user').get('id')).to.equal(10); | ||
}); | ||
}); | ||
}); | ||
@@ -709,4 +729,4 @@ | ||
}); | ||
describe('Issue #353 - wrong key set on a belongsTo relation', function() { | ||
@@ -713,0 +733,0 @@ |
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
290729
7975