Comparing version 0.1.9 to 0.2.0
@@ -1,2 +0,2 @@ | ||
// Bookshelf.js 0.1.9 | ||
// Bookshelf.js 0.2.0 | ||
@@ -28,3 +28,3 @@ // (c) 2013 Tim Griesser | ||
// Keep in sync with `package.json`. | ||
Bookshelf.VERSION = '0.1.9'; | ||
Bookshelf.VERSION = '0.2.0'; | ||
@@ -52,2 +52,4 @@ // We're using `Backbone.Events` rather than `EventEmitter`, | ||
// call the query builder with the first argument, applying the rest. | ||
// If the first argument is an object, assume the keys are query builder | ||
// methods, and the values are the arguments for the query. | ||
query: function() { | ||
@@ -57,3 +59,11 @@ this._builder || (this._builder = this.builder(_.result(this, 'tableName'))); | ||
if (args.length === 0) return this._builder; | ||
this._builder[args[0]].apply(this._builder, args.slice(1)); | ||
var method = args[0]; | ||
if (_.isObject(method)) { | ||
for (var key in method) { | ||
var target = _.isArray(method[key]) ? method[key] : [method[key]]; | ||
this._builder[key].apply(this._builder, target); | ||
} | ||
return this; | ||
} | ||
this._builder[method].apply(this._builder, args.slice(1)); | ||
return this; | ||
@@ -354,2 +364,3 @@ }, | ||
} | ||
return null; | ||
}); | ||
@@ -381,5 +392,4 @@ }, | ||
// Determine whether the model is new, typically based on whether the model has | ||
// an `idAttribute` or not. | ||
var method = options.method || (this.isNew(options) ? 'insert' : 'update'); | ||
// Determine whether the model is new, based on whether the model has an `idAttribute` or not. | ||
var method = options.method || (options.method = this.isNew(options) ? 'insert' : 'update'); | ||
var vals = attrs; | ||
@@ -599,5 +609,5 @@ | ||
if (!options.withRelated) return resp; | ||
return new EagerRelation(collection, resp) | ||
.fetch(options) | ||
.then(function() { return resp; }); | ||
return new EagerRelation(collection, resp) | ||
.fetch(options) | ||
.then(function() { return resp; }); | ||
}) | ||
@@ -722,3 +732,3 @@ .then(function(resp) { | ||
// Handles an eagerFetch, passing the name of the item we're fetching for, | ||
// Handles an eager loaded fetch, passing the name of the item we're fetching for, | ||
// and any options needed for the current fetch. | ||
@@ -734,14 +744,12 @@ eagerFetch: function(name, handled, options) { | ||
.then(function(resp) { | ||
if (resp && resp.length > 0) { | ||
// If there are additional related items, fetch them and figure out the latest | ||
var relatedModels = that.pushModels(name, handled, resp); | ||
if (options.withRelated) { | ||
return new EagerRelation(relatedModels, resp, { | ||
tempModel: new handled.relatedData.eager.ModelCtor() | ||
}).fetch(options).then(function() { | ||
return resp; | ||
}); | ||
} | ||
return resp; | ||
var relatedModels = that.pushModels(name, 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, { | ||
tempModel: new handled.relatedData.eager.ModelCtor() | ||
}).fetch(options).then(function() { | ||
return resp; | ||
}); | ||
} | ||
return resp; | ||
}); | ||
@@ -772,8 +780,9 @@ }, | ||
// Handler for the individual `morphTo` fetches. | ||
// 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(name, settings, Target) { | ||
var that = this; | ||
return function(resp) { | ||
// If there are additional related items, fetch them and figure out the latest | ||
var relatedModels = that.pushModels(name, { | ||
that.pushModels(name, { | ||
relatedData: { | ||
@@ -793,5 +802,4 @@ type: 'morphTo', | ||
// Pushes each of the incoming models onto a new `RelatedModels` object, which is set on the | ||
// `eagerModels hash with the current fetch value, so we can attach the correct models & | ||
// collections onto their parent objects. | ||
// Pushes each of the incoming models onto a new `RelatedModels` object, which is used to | ||
// correcly pair additional nested relations. | ||
pushModels: function(name, handled, resp) { | ||
@@ -798,0 +806,0 @@ var parent = this.parent; |
{ | ||
"name": "bookshelf", | ||
"version": "0.1.9", | ||
"version": "0.2.0", | ||
"description": "A lightweight Active Record ORM for PostgreSQL, MySQL, and SQLite3, influenced by Backbone.js", | ||
@@ -5,0 +5,0 @@ "main": "bookshelf.js", |
@@ -17,3 +17,3 @@ var Bookshelf = require('../bookshelf'); | ||
result || (result = targetMethod.apply(ctx, args)); | ||
return targetMethod.apply(ctx, args).then(function(resp) { | ||
return result.then(function(resp) { | ||
callback(null, resp); | ||
@@ -20,0 +20,0 @@ }, function(err) { |
@@ -116,8 +116,17 @@ /*global describe, it */ | ||
var qb = model.resetQuery().query(); | ||
equal(qb.wheres.length, (0)); | ||
equal(qb.wheres.length, 0); | ||
var q = model.query('where', {id:1}); | ||
equal(q, (model)); | ||
equal(qb.wheres.length, (1)); | ||
equal(qb.wheres.length, 1); | ||
}); | ||
it('allows passing an object to query', function() { | ||
var qb = model.resetQuery().query(); | ||
equal(qb.wheres.length, 0); | ||
var q = model.query({where: {id: 1}, orWhere: ['id', '>', '10']}); | ||
equal(q, model); | ||
equal(qb.wheres.length, 2); | ||
}); | ||
}); | ||
@@ -253,11 +262,11 @@ | ||
it('saves an new object', function(ok) { | ||
new Site({name: 'Third Site'}).save().then(function(m) { | ||
equal(m.get('id'), 3); | ||
it('saves a new object', function(ok) { | ||
new Site({name: 'Fourth Site'}).save().then(function(m) { | ||
equal(m.get('id'), 4); | ||
return new Bookshelf.Collection(null, {model: Site}).fetch(); | ||
}) | ||
.then(function(c) { | ||
equal(c.last().id, 3); | ||
equal(c.last().get('name'), 'Third Site'); | ||
equal(c.length, 3); | ||
equal(c.last().id, 4); | ||
equal(c.last().get('name'), 'Fourth Site'); | ||
equal(c.length, 4); | ||
ok(); | ||
@@ -268,16 +277,16 @@ }).then(null, ok); | ||
it('updates an existing object', function(ok) { | ||
new Site({id: 3, name: 'Third Site Updated'}).save() | ||
.then(function() { | ||
return new Bookshelf.Collection(null, {model: Site}).fetch(); | ||
}) | ||
.then(function(c) { | ||
equal(c.last().id, 3); | ||
equal(c.last().get('name'), 'Third Site Updated'); | ||
equal(c.length, 3); | ||
ok(); | ||
}); | ||
new Site({id: 4, name: 'Fourth Site Updated'}).save() | ||
.then(function() { | ||
return new Bookshelf.Collection(null, {model: Site}).fetch(); | ||
}) | ||
.then(function(c) { | ||
equal(c.last().id, 4); | ||
equal(c.last().get('name'), 'Fourth Site Updated'); | ||
equal(c.length, 4); | ||
ok(); | ||
}).then(null, ok); | ||
}); | ||
it('allows passing a method to save, to call insert or update explicitly', function(ok) { | ||
new Site({id: 4, name: 'Fourth site, explicity created'}).save(null, {method: 'insert'}) | ||
new Site({id: 5, name: 'Fifth site, explicity created'}).save(null, {method: 'insert'}) | ||
.then(function() { | ||
@@ -287,7 +296,7 @@ return new Bookshelf.Collection(null, {model: Site}).fetch(); | ||
.then(function(c) { | ||
equal(c.length, 4); | ||
equal(c.last().id, 4); | ||
equal(c.last().get('name'), 'Fourth site, explicity created'); | ||
equal(c.length, 5); | ||
equal(c.last().id, 5); | ||
equal(c.last().get('name'), 'Fifth site, explicity created'); | ||
ok(); | ||
}); | ||
}).then(null, ok); | ||
}); | ||
@@ -302,7 +311,7 @@ | ||
it('issues a delete to the Knex, returning a promise', function(ok) { | ||
new Site({id:4}).destroy().then(function() { | ||
new Site({id: 5}).destroy().then(function() { | ||
return new Bookshelf.Collection(null, {model: Site}).fetch(); | ||
}) | ||
.then(function(c) { | ||
equal(c.length, 3); | ||
equal(c.length, 4); | ||
ok(); | ||
@@ -321,3 +330,3 @@ }) | ||
it('triggers a destroying event on the model', function(ok) { | ||
var m = new Site({id: 3}); | ||
var m = new Site({id: 4}); | ||
m.on('destroying', function() { | ||
@@ -480,2 +489,3 @@ m.off(); | ||
}); | ||
}); | ||
@@ -482,0 +492,0 @@ |
@@ -110,2 +110,8 @@ var When = require('when'); | ||
it('Attaches an empty related model or collection if the `EagerRelation` comes back blank', function(ok) { | ||
new Site({id: 3}).fetch({ | ||
withRelated: ['meta', 'blogs', 'authors.posts'] | ||
}).then(handler(this, ok), ok); | ||
}); | ||
}); | ||
@@ -112,0 +118,0 @@ |
@@ -15,2 +15,4 @@ var When = require('when'); | ||
name: 'bookshelfjs.org' | ||
}, { | ||
name: 'backbonejs.org' | ||
}]), | ||
@@ -17,0 +19,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
170003
4571