Comparing version 0.1.5 to 0.1.6
@@ -1,2 +0,2 @@ | ||
// Bookshelf.js 0.1.5 | ||
// Bookshelf.js 0.1.6 | ||
@@ -28,3 +28,3 @@ // (c) 2013 Tim Griesser | ||
// Keep in sync with `package.json`. | ||
Bookshelf.VERSION = '0.1.5'; | ||
Bookshelf.VERSION = '0.1.6'; | ||
@@ -367,6 +367,8 @@ // We're using `Backbone.Events` rather than `EventEmitter`, | ||
_relatesTo: function(Target, options) { | ||
var target; | ||
var target, data; | ||
var type = options.type; | ||
var multi = (type === 'hasMany' || type === 'belongsToMany'); | ||
if (!multi) { | ||
data = {}; | ||
if (!Target.prototype instanceof Model) { | ||
@@ -400,2 +402,3 @@ throw new Error('The `'+type+'` related object must be a Bookshelf.Model'); | ||
} | ||
if (!multi) data[options.foreignKey] = options.fkValue; | ||
} | ||
@@ -405,3 +408,3 @@ | ||
// `_relation` options as a property on the instance. | ||
target = new Target(); | ||
target = new Target(data); | ||
target._relation = options; | ||
@@ -638,2 +641,5 @@ | ||
// Standard constraints for regular or eager loaded relations. | ||
// If the model isn't an eager load or a collection, it doesn't need | ||
// to be populated with the additional `where` clause, as that's already taken | ||
// care of during model creation. | ||
var constraints = function(target, resp) { | ||
@@ -643,3 +649,3 @@ var relation = target._relation; | ||
target.query('whereIn', relation.foreignKey, _.uniq(_.pluck(resp, relation.parentIdAttr))); | ||
} else { | ||
} else if (target instanceof Collection) { | ||
target.query('where', relation.foreignKey, '=', relation.fkValue); | ||
@@ -655,5 +661,3 @@ } | ||
builder = target.query(), | ||
tableName = _.result(target, 'tableName'), | ||
idAttribute = _.result(target, 'idAttribute'), | ||
@@ -676,6 +680,6 @@ otherKey = relation.otherKey, | ||
builder.join(joinTableName, tableName + '.' + idAttribute, '=', joinTableName + '.' + foreignKey); | ||
builder.join(joinTableName, tableName + '.' + _.result(target, 'idAttribute'), '=', joinTableName + '.' + foreignKey); | ||
if (resp) { | ||
builder.whereIn(joinTableName + '.' + otherKey, _.pluck(resp, idAttribute)); | ||
builder.whereIn(joinTableName + '.' + otherKey, _.pluck(resp, relation.parentIdAttr)); | ||
} else { | ||
@@ -788,4 +792,3 @@ builder.where(joinTableName + '.' + otherKey, '=', relation.fkValue); | ||
if (model instanceof Model) { | ||
model.set(model.parse(resp[0], options), _.extend({silent: true}, options)); | ||
model._previousAttributes = extendNull(model.attributes); | ||
model.set(model.parse(resp[0], options), _.extend({silent: true}, options))._reset(); | ||
} else { | ||
@@ -792,0 +795,0 @@ model.reset(resp, {silent: true, parse: true}).each(function(m) { m._reset(); }); |
{ | ||
"name": "bookshelf", | ||
"version": "0.1.5", | ||
"version": "0.1.6", | ||
"description": "A lightweight ORM for Postgres, MySQL, and SQLite3 with some Backbone", | ||
@@ -5,0 +5,0 @@ "main": "bookshelf.js", |
@@ -17,3 +17,3 @@ var _ = require('underscore'); | ||
}); | ||
var SubUsers = Users.extend({ | ||
@@ -62,3 +62,3 @@ otherMethod: function() { return this.getData(); } | ||
describe('model', function() { | ||
it('does not permit polymorphic models', function(ok) { | ||
@@ -102,3 +102,3 @@ var Collection = Bookshelf.Collection.extend({ | ||
describe('fetch', function() { | ||
it ('fetches the models in a collection', function (ok) { | ||
@@ -108,5 +108,5 @@ Bookshelf.Collection.extend({tableName: 'posts'}) | ||
.fetch() | ||
.then(handler(ok), ok); | ||
.then(handler(this, ok), ok); | ||
}); | ||
}); | ||
@@ -113,0 +113,0 @@ |
@@ -20,2 +20,4 @@ var When = require('when'); | ||
var Tag = Models.Tag; | ||
var User = Models.User; | ||
var Role = Models.Role; | ||
@@ -47,3 +49,3 @@ // Collections | ||
}) | ||
.then(handler(ok), ok); | ||
.then(handler(this, ok), ok); | ||
}); | ||
@@ -57,3 +59,3 @@ | ||
}) | ||
.then(handler(ok), ok); | ||
.then(handler(this, ok), ok); | ||
}); | ||
@@ -65,3 +67,3 @@ | ||
.fetch() | ||
.then(handler(ok), ok); | ||
.then(handler(this, ok), ok); | ||
}); | ||
@@ -73,3 +75,3 @@ | ||
.fetch() | ||
.then(handler(ok), ok); | ||
.then(handler(this, ok), ok); | ||
}); | ||
@@ -84,3 +86,3 @@ | ||
withRelated: ['meta'] | ||
}).then(handler(ok), ok); | ||
}).then(handler(this, ok), ok); | ||
}); | ||
@@ -91,3 +93,3 @@ | ||
withRelated: ['authors', 'blogs'] | ||
}).then(handler(ok), ok); | ||
}).then(handler(this, ok), ok); | ||
}); | ||
@@ -98,3 +100,3 @@ | ||
withRelated: ['site'] | ||
}).then(handler(ok), ok); | ||
}).then(handler(this, ok), ok); | ||
}); | ||
@@ -111,3 +113,3 @@ | ||
withRelated: ['tags'] | ||
}).then(handler(ok), ok); | ||
}).then(handler(this, ok), ok); | ||
}); | ||
@@ -122,3 +124,3 @@ | ||
withRelated: ['admins'] | ||
}).then(handler(ok), ok); | ||
}).then(handler(this, ok), ok); | ||
}); | ||
@@ -129,3 +131,3 @@ | ||
withRelated: ['site'] | ||
}).then(handler(ok), ok); | ||
}).then(handler(this, ok), ok); | ||
}); | ||
@@ -136,3 +138,3 @@ | ||
withRelated: ['blogs'] | ||
}).then(handler(ok), ok); | ||
}).then(handler(this, ok), ok); | ||
}); | ||
@@ -146,3 +148,3 @@ | ||
}) | ||
.then(handler(ok), ok); | ||
.then(handler(this, ok), ok); | ||
}); | ||
@@ -157,3 +159,3 @@ | ||
withRelated: ['authors.ownPosts'] | ||
}).then(handler(ok), ok); | ||
}).then(handler(this, ok), ok); | ||
}); | ||
@@ -164,3 +166,3 @@ | ||
withRelated: ['authors.posts'] | ||
}).then(handler(ok), ok); | ||
}).then(handler(this, ok), ok); | ||
}); | ||
@@ -171,3 +173,3 @@ | ||
withRelated: ['authors.ownPosts', 'authors.site', 'blogs.posts'] | ||
}).then(handler(ok), ok); | ||
}).then(handler(this, ok), ok); | ||
}); | ||
@@ -183,3 +185,3 @@ | ||
withRelated: ['authors.ownPosts'] | ||
}).then(handler(ok), ok); | ||
}).then(handler(this, ok), ok); | ||
}); | ||
@@ -194,3 +196,3 @@ | ||
return m.load(['blogs', 'authors.site']); | ||
}).then(handler(ok), ok); | ||
}).then(handler(this, ok), ok); | ||
}); | ||
@@ -201,3 +203,3 @@ | ||
return c.load(['blogs', 'authors.site']); | ||
}).then(handler(ok), ok); | ||
}).then(handler(this, ok), ok); | ||
}); | ||
@@ -253,4 +255,22 @@ }); | ||
describe('Custom foreignKey & otherKey', function() { | ||
it('works with many-to-many', function(ok) { | ||
new User({uid: 1}) | ||
.roles() | ||
.fetch() | ||
.then(handler(this, ok), ok); | ||
}); | ||
it('works with eager loaded many-to-many', function(ok) { | ||
new User({uid: 1}) | ||
.fetch({withRelated: ['roles']}) | ||
.then(handler(this, ok), ok); | ||
}); | ||
}); | ||
}); | ||
}; |
@@ -45,6 +45,6 @@ var _ = require('underscore'); | ||
var item = 1; | ||
return function(resolver, isAll) { | ||
return function(test, resolver, isAll) { | ||
var fn = function(data) { | ||
data = JSON.parse(JSON.stringify(data)); | ||
var label = '' + section + '.' + item; | ||
var label = '' + section + '.' + item + ' - ' + test.test.title; | ||
if (dev) { | ||
@@ -51,0 +51,0 @@ if (_.isArray(data)) data = _.map(data, omitDates); |
@@ -139,6 +139,14 @@ var When = require('when'); | ||
comment: 'this is neat.' | ||
}]) | ||
}]), | ||
]); | ||
Knex('users').idAttribute('uid').insert({uid: 1, username: 'root'}), | ||
Knex('roles').idAttribute('rid').insert({rid: 1, name: 'admin'}), | ||
Knex('users_roles').idAttribute(null).insert({uid: 1, rid: 1}) | ||
]).then(null, function(e) { | ||
console.log(e.stack); | ||
}); | ||
}; |
@@ -7,3 +7,4 @@ var _ = require('underscore'); | ||
'admins_sites', 'authors', 'authors_posts', | ||
'blogs', 'posts', 'tags', 'posts_tags', 'comments' | ||
'blogs', 'posts', 'tags', 'posts_tags', 'comments', | ||
'users', 'roles', 'users_roles' | ||
]; | ||
@@ -90,4 +91,19 @@ | ||
table.text('comment'); | ||
}), | ||
Schema.createTable('users', function(table) { | ||
table.increments('uid'); | ||
table.string('username'); | ||
}), | ||
Schema.createTable('roles', function(table) { | ||
table.increments('rid'); | ||
table.string('name'); | ||
}), | ||
Schema.createTable('users_roles', function(table) { | ||
table.integer('rid'); | ||
table.integer('uid'); | ||
}) | ||
]); | ||
@@ -94,0 +110,0 @@ |
@@ -21,4 +21,4 @@ | ||
}, | ||
blogs: function() { | ||
return this.hasMany(Blog); | ||
blogs: function() { | ||
return this.hasMany(Blog); | ||
}, | ||
@@ -50,10 +50,10 @@ meta: function() { | ||
tableName: 'authors', | ||
site: function() { | ||
return this.belongsTo(Site); | ||
site: function() { | ||
return this.belongsTo(Site); | ||
}, | ||
posts: function() { | ||
return this.belongsToMany(Post); | ||
posts: function() { | ||
return this.belongsToMany(Post); | ||
}, | ||
ownPosts: function() { | ||
return this.hasMany(Post, 'owner_id'); | ||
ownPosts: function() { | ||
return this.hasMany(Post, 'owner_id'); | ||
} | ||
@@ -69,3 +69,3 @@ }); | ||
site: function() { | ||
return this.belongsTo(Site); | ||
return this.belongsTo(Site); | ||
}, | ||
@@ -94,13 +94,13 @@ posts: function() { | ||
hasTimestamps: true, | ||
blog: function() { | ||
return this.belongsTo(Blog); | ||
blog: function() { | ||
return this.belongsTo(Blog); | ||
}, | ||
authors: function() { | ||
authors: function() { | ||
return this.belongsToMany(Author); | ||
}, | ||
tags: function() { | ||
return this.belongsToMany(Tag); | ||
tags: function() { | ||
return this.belongsToMany(Tag); | ||
}, | ||
comments: function() { | ||
return this.hasMany(Comment); | ||
comments: function() { | ||
return this.hasMany(Comment); | ||
} | ||
@@ -130,7 +130,23 @@ }); | ||
tableName: 'tags', | ||
posts: function() { | ||
return this.belongsToMany(Post); | ||
posts: function() { | ||
return this.belongsToMany(Post); | ||
} | ||
}); | ||
var User = Shelf.Model.extend({ | ||
tableName: 'users', | ||
idAttribute: 'uid', | ||
roles: function() { | ||
return this.belongsToMany(Role, 'users_roles', 'rid', 'uid'); | ||
} | ||
}); | ||
var Role = Shelf.Model.extend({ | ||
tableName: 'roles', | ||
idAttribute: 'rid', | ||
users: function(){ | ||
return this.belongsToMany(User, 'users_roles', 'uid', 'rid'); | ||
} | ||
}); | ||
return { | ||
@@ -145,3 +161,5 @@ Models: { | ||
Comment: Comment, | ||
Tag: Tag | ||
Tag: Tag, | ||
User: User, | ||
Role: Role | ||
}, | ||
@@ -148,0 +166,0 @@ Collections: { |
module.exports = { | ||
db: { | ||
'collection.1': { | ||
'collection.1 - fetches the models in a collection': { | ||
mysql: [{ | ||
@@ -98,3 +98,3 @@ id: 1, | ||
}, | ||
'relations.1': { | ||
'relations.1 - handles belongsTo': { | ||
mysql: [{ | ||
@@ -137,3 +137,3 @@ blog: { | ||
}, | ||
'relations.2': { | ||
'relations.2 - handles hasMany': { | ||
mysql: [{ | ||
@@ -179,20 +179,20 @@ id: 1, | ||
}, | ||
'relations.3': { | ||
'relations.3 - handles hasOne': { | ||
mysql: { | ||
site_id: 1, | ||
id: 1, | ||
site_id: 1, | ||
description: 'This is a description for the Knexjs Site' | ||
}, | ||
postgres: { | ||
site_id: 1, | ||
id: 1, | ||
site_id: 1, | ||
description: 'This is a description for the Knexjs Site' | ||
}, | ||
sqlite3: { | ||
site_id: 1, | ||
id: 1, | ||
site_id: 1, | ||
description: 'This is a description for the Knexjs Site' | ||
} | ||
}, | ||
'relations.4': { | ||
'relations.4 - handles belongsToMany': { | ||
mysql: [{ | ||
@@ -226,3 +226,3 @@ id: 1, | ||
}, | ||
'relations.5': { | ||
'relations.5 - eager loads "hasOne" relationships correctly': { | ||
mysql: { | ||
@@ -256,3 +256,3 @@ id: 1, | ||
}, | ||
'relations.6': { | ||
'relations.6 - eager loads "hasMany" relationships correctly': { | ||
mysql: { | ||
@@ -331,3 +331,3 @@ id: 1, | ||
}, | ||
'relations.7': { | ||
'relations.7 - eager loads "belongsTo" relationships correctly': { | ||
mysql: { | ||
@@ -361,3 +361,3 @@ id: 3, | ||
}, | ||
'relations.8': { | ||
'relations.8 - eager loads "belongsToMany" models correctly': { | ||
mysql: { | ||
@@ -433,3 +433,3 @@ id: 1, | ||
}, | ||
'relations.9': { | ||
'relations.9 - eager loads "hasOne" models correctly': { | ||
mysql: [{ | ||
@@ -463,3 +463,3 @@ id: 1, | ||
}, | ||
'relations.10': { | ||
'relations.10 - eager loads "belongsTo" models correctly': { | ||
mysql: [{ | ||
@@ -565,3 +565,3 @@ id: 1, | ||
}, | ||
'relations.11': { | ||
'relations.11 - eager loads "hasMany" models correctly': { | ||
mysql: { | ||
@@ -607,3 +607,3 @@ id: 1, | ||
}, | ||
'relations.12': { | ||
'relations.12 - eager loads "belongsToMany" models correctly': { | ||
mysql: [{ | ||
@@ -700,3 +700,3 @@ id: 1, | ||
}, | ||
'relations.13': { | ||
'relations.13 - eager loads "hasMany" -> "hasMany"': { | ||
mysql: { | ||
@@ -808,3 +808,3 @@ id: 1, | ||
}, | ||
'relations.14': { | ||
'relations.14 - eager loads "hasMany" -> "belongsToMany"': { | ||
mysql: { | ||
@@ -934,3 +934,3 @@ id: 1, | ||
}, | ||
'relations.15': { | ||
'relations.15 - does multi deep eager loads': { | ||
mysql: { | ||
@@ -1153,3 +1153,3 @@ id: 1, | ||
}, | ||
'relations.16': { | ||
'relations.16 - eager loads "hasMany" -> "hasMany"': { | ||
mysql: [{ | ||
@@ -1345,3 +1345,3 @@ id: 1, | ||
}, | ||
'relations.17': { | ||
'relations.17 - eager loads relations on a populated model': { | ||
mysql: { | ||
@@ -1444,3 +1444,3 @@ id: 1, | ||
}, | ||
'relations.18': { | ||
'relations.18 - eager loads attributes on a collection': { | ||
mysql: [{ | ||
@@ -1635,4 +1635,56 @@ id: 1, | ||
}] | ||
}, | ||
'relations.19 - works with many-to-many': { | ||
mysql: [{ | ||
rid: 1, | ||
name: 'admin', | ||
_pivot_uid: 1, | ||
_pivot_rid: 1 | ||
}], | ||
postgres: [{ | ||
rid: 1, | ||
name: 'admin', | ||
_pivot_uid: 1, | ||
_pivot_rid: 1 | ||
}], | ||
sqlite3: [{ | ||
rid: 1, | ||
name: 'admin', | ||
_pivot_uid: 1, | ||
_pivot_rid: 1 | ||
}] | ||
}, | ||
'relations.20 - works with eager loaded many-to-many': { | ||
mysql: { | ||
uid: 1, | ||
username: 'root', | ||
roles: [{ | ||
rid: 1, | ||
name: 'admin', | ||
_pivot_uid: 1, | ||
_pivot_rid: 1 | ||
}] | ||
}, | ||
postgres: { | ||
uid: 1, | ||
username: 'root', | ||
roles: [{ | ||
rid: 1, | ||
name: 'admin', | ||
_pivot_uid: 1, | ||
_pivot_rid: 1 | ||
}] | ||
}, | ||
sqlite3: { | ||
uid: 1, | ||
username: 'root', | ||
roles: [{ | ||
rid: 1, | ||
name: 'admin', | ||
_pivot_uid: 1, | ||
_pivot_rid: 1 | ||
}] | ||
} | ||
} | ||
} | ||
} |
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
141099
3775