Comparing version 0.5.8 to 0.6.0
@@ -1,2 +0,2 @@ | ||
// Bookshelf.js 0.5.8 | ||
// Bookshelf.js 0.6.0 | ||
// --------------- | ||
@@ -15,3 +15,3 @@ | ||
// All external libraries needed in this scope. | ||
var _ = require('underscore'); | ||
var _ = require('lodash'); | ||
var Knex = require('knex'); | ||
@@ -87,3 +87,3 @@ | ||
// Keep in sync with `package.json`. | ||
VERSION: '0.5.8', | ||
VERSION: '0.6.0', | ||
@@ -90,0 +90,0 @@ // Helper method to wrap a series of Bookshelf actions in a `knex` transaction block; |
@@ -11,4 +11,3 @@ // Base Collection | ||
// All exernal dependencies required in this scope. | ||
var _ = require('underscore'); | ||
var when = require('when'); | ||
var _ = require('lodash'); | ||
var Backbone = require('backbone'); | ||
@@ -18,2 +17,3 @@ | ||
var Events = require('./events').Events; | ||
var Promise = require('./promise').Promise; | ||
var ModelBase = require('./model').ModelBase; | ||
@@ -30,3 +30,2 @@ | ||
if (models) this.reset(models, _.extend({silent: true}, options)); | ||
_.bindAll(this, '_handleResponse', '_handleEager'); | ||
}; | ||
@@ -142,13 +141,15 @@ | ||
// Convenience method for map, returning a `when.all` promise. | ||
// Convenience method for map, returning a `Promise.all` promise. | ||
mapThen: function(iterator, context) { | ||
return when.all(this.map(iterator, context)); | ||
return Promise.all(this.map(iterator, context)); | ||
}, | ||
// Convenience method for invoke, returning a `when.all` promise. | ||
// Convenience method for invoke, returning a `Promise.all` promise. | ||
invokeThen: function() { | ||
return when.all(this.invoke.apply(this, arguments)); | ||
return Promise.all(this.invoke.apply(this, arguments)); | ||
}, | ||
fetch: function() {}, | ||
fetch: function() { | ||
return Promise.rejected('The fetch method has not been implemented'); | ||
}, | ||
@@ -155,0 +156,0 @@ _handleResponse: function() {}, |
@@ -13,5 +13,5 @@ // Eager Base | ||
var _ = require('underscore'); | ||
var when = require('when'); | ||
var _ = require('lodash'); | ||
var Backbone = require('backbone'); | ||
var Promise = require('./promise').Promise; | ||
@@ -28,3 +28,3 @@ var EagerBase = function(parent, parentResponse, target) { | ||
// are necessary for fetching based on the `model.load` or `withRelated` option. | ||
fetch: function(options) { | ||
fetch: Promise.method(function(options) { | ||
var relationName, related, relation; | ||
@@ -81,4 +81,4 @@ var target = this.target; | ||
// returning the original response when these syncs & pairings are complete. | ||
return when.all(pendingDeferred).yield(this.parentResponse); | ||
}, | ||
return Promise.all(pendingDeferred).yield(this.parentResponse); | ||
}), | ||
@@ -85,0 +85,0 @@ // Prep the `withRelated` object, to normalize into an object where each |
@@ -9,3 +9,3 @@ // Events | ||
var when = require('when'); | ||
var Promise = require('./promise').Promise; | ||
var Backbone = require('backbone'); | ||
@@ -16,6 +16,5 @@ var triggerThen = require('trigger-then'); | ||
// so we can have event driven async validations, functions, etc. | ||
triggerThen(Backbone, when); | ||
triggerThen(Backbone, Promise); | ||
exports.Events = Backbone.Events; | ||
}); | ||
@@ -22,0 +21,0 @@ |
@@ -9,6 +9,7 @@ // Base Model | ||
var _ = require('underscore'); | ||
var _ = require('lodash'); | ||
var Backbone = require('backbone'); | ||
var Events = require('./events').Events; | ||
var Promise = require('./promise').Promise; | ||
@@ -39,3 +40,2 @@ // A list of properties that are omitted from the `Backbone.Model.prototype`, to create | ||
this.initialize.apply(this, arguments); | ||
_.bindAll(this, '_handleResponse', '_handleEager'); | ||
}; | ||
@@ -163,13 +163,13 @@ | ||
// during the "destroying" event, the model will not be destroyed. | ||
destroy: function(options) { | ||
var model = this; | ||
options = options || {}; | ||
return model.triggerThen('destroying', model, options) | ||
.then(function() { return model.sync(options).del(); }) | ||
.then(function(resp) { | ||
model.clear(); | ||
return model.triggerThen('destroyed', model, resp, options); | ||
}) | ||
.then(function() { return model._reset(); }); | ||
}, | ||
destroy: Promise.method(function(options) { | ||
options = options ? _.clone(options) : {}; | ||
return Promise.bind(this).then(function() { | ||
return this.triggerThen('destroying', this, options); | ||
}).then(function() { | ||
return this.sync(options).del(); | ||
}).then(function(resp) { | ||
this.clear(); | ||
return this.triggerThen('destroyed', this, resp, options); | ||
}).then(this._reset); | ||
}), | ||
@@ -176,0 +176,0 @@ _handleResponse: function() {}, |
@@ -9,3 +9,3 @@ // Base Relation | ||
var _ = require('underscore'); | ||
var _ = require('lodash'); | ||
var Backbone = require('backbone'); | ||
@@ -12,0 +12,0 @@ |
@@ -13,3 +13,3 @@ // Base Sync | ||
var when = require('when'); | ||
var Promise = require('./promise').Promise; | ||
var Backbone = require('backbone'); | ||
@@ -24,8 +24,5 @@ | ||
// May be used for any setup for the class. | ||
initialize: function() {}, | ||
// Return a single model object. | ||
first: function() { | ||
return when.resolve({}); | ||
return Promise.fulfilled({}); | ||
}, | ||
@@ -36,3 +33,3 @@ | ||
select: function() { | ||
return when.resolve([]); | ||
return Promise.fulfilled([]); | ||
}, | ||
@@ -43,3 +40,3 @@ | ||
insert: function() { | ||
return when.resolve({}); | ||
return Promise.fulfilled({}); | ||
}, | ||
@@ -49,3 +46,3 @@ | ||
update: function() { | ||
return when.resolve({}); | ||
return Promise.fulfilled({}); | ||
}, | ||
@@ -55,3 +52,3 @@ | ||
del: function() { | ||
return when.resolve({}); | ||
return Promise.fulfilled({}); | ||
} | ||
@@ -58,0 +55,0 @@ |
@@ -9,10 +9,10 @@ // Collection | ||
var _ = require('underscore'); | ||
var when = require('when'); | ||
var _ = require('lodash'); | ||
var Sync = require('./sync').Sync; | ||
var Helpers = require('./helpers').Helpers; | ||
var Sync = require('./sync').Sync; | ||
var Helpers = require('./helpers').Helpers; | ||
var EagerRelation = require('./eager').EagerRelation; | ||
var CollectionBase = require('../base/collection').CollectionBase; | ||
var Promise = require('../base/promise').Promise; | ||
@@ -29,11 +29,11 @@ exports.Collection = CollectionBase.extend({ | ||
// for the query when they arrive. | ||
fetch: function(options) { | ||
options = options || {}; | ||
var collection = this, relatedData = this.relatedData; | ||
fetch: Promise.method(function(options) { | ||
options = options ? _.clone(options) : {}; | ||
var sync = this.sync(options) | ||
.select() | ||
.bind(this) | ||
.tap(function(response) { | ||
if (!response || response.length === 0) { | ||
if (options.require) throw new Error('EmptyResponse'); | ||
return when.reject(null); | ||
return Promise.reject(null); | ||
} | ||
@@ -45,22 +45,22 @@ }) | ||
// If the "withRelated" is specified, we also need to eager load all of the | ||
// data on the collection, as a side-effect, before we ultimately jump into the | ||
// next step of the collection. Since the `columns` are only relevant to the current | ||
// level, ensure those are omitted from the options. | ||
if (options.withRelated) { | ||
sync = sync.tap(this._handleEager(_.omit(options, 'columns'))); | ||
} | ||
// If the "withRelated" is specified, we also need to eager load all of the | ||
// data on the collection, as a side-effect, before we ultimately jump into the | ||
// next step of the collection. Since the `columns` are only relevant to the current | ||
// level, ensure those are omitted from the options. | ||
if (options.withRelated) { | ||
sync = sync.tap(this._handleEager(_.omit(options, 'columns'))); | ||
} | ||
return sync.tap(function(response) { | ||
return collection.triggerThen('fetched', collection, response, options); | ||
}) | ||
.otherwise(function(err) { | ||
if (err !== null) throw err; | ||
collection.reset([], {silent: true}); | ||
}) | ||
.yield(this); | ||
}, | ||
return sync.tap(function(response) { | ||
return this.triggerThen('fetched', this, response, options); | ||
}) | ||
.caught(function(err) { | ||
if (err !== null) throw err; | ||
this.reset([], {silent: true}); | ||
}) | ||
.yield(this); | ||
}), | ||
// Fetches a single model from the collection, useful on related collections. | ||
fetchOne: function(options) { | ||
fetchOne: Promise.method(function(options) { | ||
var model = new this.model; | ||
@@ -70,7 +70,6 @@ model._knex = this.query().clone(); | ||
return model.fetch(options); | ||
}, | ||
}), | ||
// Eager loads relationships onto an already populated `Collection` instance. | ||
load: function(relations, options) { | ||
var collection = this; | ||
load: Promise.method(function(relations, options) { | ||
_.isArray(relations) || (relations = [relations]); | ||
@@ -81,3 +80,3 @@ options = _.extend({}, options, {shallow: true, withRelated: relations}); | ||
.yield(this); | ||
}, | ||
}), | ||
@@ -89,8 +88,5 @@ // Shortcut for creating a new model, saving, and adding to the collection. | ||
// automatically create the joining model upon insertion. | ||
create: function(model, options) { | ||
options = options || {}; | ||
var collection = this; | ||
create: Promise.method(function(model, options) { | ||
options = options ? _.clone(options) : {}; | ||
var relatedData = this.relatedData; | ||
model = this._prepareModel(model, options); | ||
@@ -108,12 +104,13 @@ | ||
.save(null, options) | ||
.bind(this) | ||
.then(function() { | ||
if (relatedData && (relatedData.type === 'belongsToMany' || relatedData.isThrough())) { | ||
return collection.attach(model, options); | ||
return this.attach(model, options); | ||
} | ||
}) | ||
.then(function() { | ||
collection.add(model, options); | ||
this.add(model, options); | ||
return model; | ||
}); | ||
}, | ||
}), | ||
@@ -148,5 +145,4 @@ // Reset the query builder, called internally | ||
_handleEager: function(options) { | ||
var collection = this; | ||
return function(response) { | ||
return new EagerRelation(collection.models, response, new collection.model()).fetch(options); | ||
return new EagerRelation(this.models, response, new this.model()).fetch(options); | ||
}; | ||
@@ -153,0 +149,0 @@ } |
@@ -9,7 +9,7 @@ // EagerRelation | ||
var _ = require('underscore'); | ||
var when = require('when'); | ||
var _ = require('lodash'); | ||
var Helpers = require('./helpers').Helpers; | ||
var Helpers = require('./helpers').Helpers; | ||
var EagerBase = require('../base/eager').EagerBase; | ||
var Promise = require('../base/promise').Promise; | ||
@@ -24,3 +24,3 @@ // An `EagerRelation` object temporarily stores the models from an eager load, | ||
// and any options needed for the current fetch. | ||
eagerFetch: function(relationName, handled, options) { | ||
eagerFetch: Promise.method(function(relationName, handled, options) { | ||
var relatedData = handled.relatedData; | ||
@@ -37,3 +37,3 @@ | ||
.tap(eagerLoadHelper(this, relationName, handled, options)); | ||
}, | ||
}), | ||
@@ -43,3 +43,3 @@ // Special handler for the eager loaded morph-to relations, this handles | ||
// pairing them up onto a single response for the eager loading. | ||
morphToFetch: function(relationName, relatedData, options) { | ||
morphToFetch: Promise.method(function(relationName, relatedData, options) { | ||
var pending = []; | ||
@@ -63,6 +63,6 @@ var groups = _.groupBy(this.parent, function(m) { | ||
} | ||
return when.all(pending).then(function(resps) { | ||
return Promise.all(pending).then(function(resps) { | ||
return _.flatten(resps); | ||
}); | ||
} | ||
}) | ||
@@ -72,3 +72,3 @@ }); | ||
// Handles the eager load for both the `morphTo` and regular cases. | ||
var eagerLoadHelper = function(relation, relationName, handled, options) { | ||
function eagerLoadHelper(relation, relationName, handled, options) { | ||
return function(resp) { | ||
@@ -92,7 +92,8 @@ var relatedModels = relation.pushModels(relationName, handled, resp); | ||
}; | ||
}; | ||
} | ||
// Filters the `withRelated` on a `morphTo` relation, to ensure that only valid | ||
// relations are attempted for loading. | ||
var filterRelated = function(relatedModel, options) { | ||
function filterRelated(relatedModel, options) { | ||
// By this point, all withRelated should be turned into a hash, so it should | ||
@@ -107,3 +108,3 @@ // be fairly simple to process by splitting on the dots. | ||
}, []); | ||
}; | ||
} | ||
@@ -110,0 +111,0 @@ |
@@ -9,3 +9,3 @@ // Helpers | ||
var _ = require('underscore'); | ||
var _ = require('lodash'); | ||
@@ -12,0 +12,0 @@ exports.Helpers = { |
@@ -9,10 +9,10 @@ // Model | ||
var _ = require('underscore'); | ||
var when = require('when'); | ||
var _ = require('lodash'); | ||
var Sync = require('./sync').Sync; | ||
var Helpers = require('./helpers').Helpers; | ||
var Sync = require('./sync').Sync; | ||
var Helpers = require('./helpers').Helpers; | ||
var EagerRelation = require('./eager').EagerRelation; | ||
var ModelBase = require('../base/model').ModelBase; | ||
var Promise = require('../base/promise').Promise; | ||
@@ -79,8 +79,9 @@ exports.Model = ModelBase.extend({ | ||
// a failure if the model comes up blank. | ||
fetch: function(options) { | ||
options = options || {}; | ||
var model = this; | ||
fetch: Promise.method(function(options) { | ||
options = options ? _.clone(options) : {}; | ||
// Run the `first` call on the `sync` object to fetch a single model. | ||
var sync = this.sync(options).first() | ||
var sync = this.sync(options) | ||
.first() | ||
.bind(this) | ||
@@ -91,3 +92,3 @@ // Jump the rest of the chain if the response doesn't exist... | ||
if (options.require) throw new Error('EmptyResponse'); | ||
return when.reject(null); | ||
return Promise.reject(null); | ||
} | ||
@@ -108,17 +109,23 @@ }) | ||
return sync.tap(function(response) { | ||
return model.triggerThen('fetched', model, response, options); | ||
return this.triggerThen('fetched', this, response, options); | ||
}) | ||
.yield(model) | ||
.otherwise(function(err) { | ||
.yield(this) | ||
.caught(function(err) { | ||
if (err === null) return err; | ||
throw err; | ||
}); | ||
}, | ||
}), | ||
// Eager loads relationships onto an already populated `Model` instance. | ||
load: function(relations, options) { | ||
_.isArray(relations) || (relations = [relations]); | ||
var handler = this._handleEager(_.extend({}, options, {shallow: true, withRelated: relations})); | ||
return handler([this.toJSON({shallow: true})]).yield(this); | ||
}, | ||
load: Promise.method(function(relations, options) { | ||
return Promise.bind(this) | ||
.then(function() { | ||
return [this.toJSON({shallow: true})]; | ||
}) | ||
.then(this._handleEager(_.extend({}, options, { | ||
shallow: true, | ||
withRelated: _.isArray(relations) ? relations : [relations] | ||
}))).yield(this); | ||
}), | ||
@@ -129,3 +136,3 @@ // Sets and saves the hash of model attributes, triggering | ||
// If an error is thrown during these events, the model will not be saved. | ||
save: function(key, val, options) { | ||
save: Promise.method(function(key, val, options) { | ||
var attrs; | ||
@@ -138,64 +145,70 @@ | ||
} else { | ||
options || (options = {}); | ||
(attrs = {})[key] = val; | ||
options = options ? _.clone(options) : {}; | ||
} | ||
// If the model has timestamp columns, | ||
// set them as attributes on the model, even | ||
// if the "patch" option is specified. | ||
if (this.hasTimestamps) _.extend(attrs, this.timestamp(options)); | ||
return Promise.bind(this).then(function() { | ||
return this.isNew(options); | ||
}).then(function(isNew) { | ||
// 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; | ||
// If the model has timestamp columns, | ||
// set them as attributes on the model, even | ||
// if the "patch" option is specified. | ||
if (this.hasTimestamps) _.extend(attrs, this.timestamp(options)); | ||
// If the object is being created, we merge any defaults here | ||
// rather than during object creation. | ||
if (method === 'insert' || options.defaults) { | ||
var defaults = _.result(this, 'defaults'); | ||
if (defaults) { | ||
vals = _.extend({}, defaults, this.attributes, vals); | ||
// Determine whether the model is new, based on whether the model has an `idAttribute` or not. | ||
var method = options.method || (options.method = isNew ? 'insert' : 'update'); | ||
var vals = attrs; | ||
// If the object is being created, we merge any defaults here | ||
// rather than during object creation. | ||
if (method === 'insert' || options.defaults) { | ||
var defaults = _.result(this, 'defaults'); | ||
if (defaults) { | ||
vals = _.extend({}, defaults, this.attributes, vals); | ||
} | ||
} | ||
} | ||
// Set the attributes on the model, and maintain a reference to use below. | ||
var model = this.set(vals, {silent: true}); | ||
// Set the attributes on the model. | ||
this.set(vals, {silent: true}); | ||
// If there are any save constraints, set them on the model. | ||
if (this.relatedData && this.relatedData.type !== 'morphTo') { | ||
Helpers.saveConstraints(this, this.relatedData); | ||
} | ||
// If there are any save constraints, set them on the model. | ||
if (this.relatedData && this.relatedData.type !== 'morphTo') { | ||
Helpers.saveConstraints(this, this.relatedData); | ||
} | ||
var sync = this.sync(options); | ||
// Gives access to the `query` object in the `options`, in case we need it | ||
// in any event handlers. | ||
var sync = this.sync(options); | ||
options.query = sync.query; | ||
// Gives access to the `query` object in the `options`, in case we need it. | ||
options.query = sync.query; | ||
return Promise.bind(this).all([ | ||
this.triggerThen((method === 'insert' ? 'creating' : 'updating'), this, attrs, options), | ||
this.triggerThen('saving', this, attrs, options) | ||
]) | ||
.then(function() { | ||
return sync[options.method](method === 'update' && options.patch ? attrs : this.attributes); | ||
}) | ||
.then(function(resp) { | ||
return when.all([ | ||
model.triggerThen((method === 'insert' ? 'creating' : 'updating'), model, attrs, options), | ||
model.triggerThen('saving', model, attrs, options) | ||
]) | ||
.then(function() { | ||
return sync[options.method](method === 'update' && options.patch ? attrs : model.attributes); | ||
}) | ||
.then(function(resp) { | ||
// After a successful database save, the id is updated if the model was created | ||
if (method === 'insert' && resp) { | ||
this.attributes[this.idAttribute] = this[this.idAttribute] = resp[0]; | ||
} | ||
// After a successful database save, the id is updated if the model was created | ||
if (method === 'insert' && resp) { | ||
model.attributes[model.idAttribute] = model[model.idAttribute] = resp[0]; | ||
} | ||
// In case we need to reference the `previousAttributes` for the this | ||
// in the following event handlers. | ||
options.previousAttributes = this._previousAttributes; | ||
// In case we need to reference the `previousAttributes` for the model | ||
// in the following event handlers. | ||
options.previousAttributes = model._previousAttributes; | ||
this._reset(); | ||
model._reset(); | ||
return Promise.all([ | ||
this.triggerThen((method === 'insert' ? 'created' : 'updated'), this, resp, options), | ||
this.triggerThen('saved', this, resp, options) | ||
]); | ||
return when.all([ | ||
model.triggerThen((method === 'insert' ? 'created' : 'updated'), model, resp, options), | ||
model.triggerThen('saved', model, resp, options) | ||
]); | ||
}); | ||
}).yield(this); | ||
}, | ||
}), | ||
@@ -238,5 +251,4 @@ // Reset the query builder, called internally | ||
_handleEager: function(options) { | ||
var model = this; | ||
return function(response) { | ||
return new EagerRelation([model], response, model).fetch(options); | ||
return new EagerRelation([this], response, this).fetch(options); | ||
}; | ||
@@ -243,0 +255,0 @@ } |
@@ -9,4 +9,3 @@ // Relation | ||
var _ = require('underscore'); | ||
var when = require('when'); | ||
var _ = require('lodash'); | ||
var inflection = require('inflection'); | ||
@@ -18,2 +17,3 @@ | ||
var RelationBase = require('../base/relation').RelationBase; | ||
var Promise = require('../base/promise').Promise; | ||
@@ -28,3 +28,3 @@ var push = [].push; | ||
init: function(parent) { | ||
this.parentId = parent.id; | ||
this.parentId = parent.id; | ||
this.parentTableName = _.result(parent, 'tableName'); | ||
@@ -372,6 +372,6 @@ this.parentIdAttribute = _.result(parent, 'idAttribute'); | ||
// the `belongsToMany` or `hasOne` / `hasMany` :through relationship. | ||
_handler: function(method, ids, options) { | ||
_handler: Promise.method(function(method, ids, options) { | ||
var pending = []; | ||
if (ids == void 0) { | ||
if (method === 'insert') return when.resolve(this); | ||
if (method === 'insert') return Promise.resolve(this); | ||
if (method === 'delete') pending.push(this._processPivot(method, null, options)); | ||
@@ -383,4 +383,4 @@ } | ||
} | ||
return when.all(pending).yield(this); | ||
}, | ||
return Promise.all(pending).yield(this); | ||
}), | ||
@@ -390,3 +390,3 @@ // Handles setting the appropriate constraints and shelling out | ||
// returning a promise. | ||
_processPivot: function(method, item, options) { | ||
_processPivot: Promise.method(function(method, item, options) { | ||
var data = {}; | ||
@@ -425,3 +425,3 @@ var relatedData = this.relatedData; | ||
}); | ||
} | ||
}) | ||
@@ -428,0 +428,0 @@ }; |
@@ -9,4 +9,4 @@ // Sync | ||
var _ = require('underscore'); | ||
var when = require('when'); | ||
var _ = require('lodash'); | ||
var Promise = require('../base/promise').Promise; | ||
@@ -23,4 +23,3 @@ // Sync is the dispatcher for any database queries, | ||
this.options = options; | ||
this._init(syncing, options); | ||
this.initialize(syncing, options); | ||
if (options.transacting) this.query.transacting(options.transacting); | ||
}; | ||
@@ -30,10 +29,9 @@ | ||
initialize: function() {}, | ||
// Select the first item from the database - only used by models. | ||
first: function() { | ||
var syncing = this.syncing; | ||
this.query.where(syncing.format(_.extend(Object.create(null), syncing.attributes))).limit(1); | ||
first: Promise.method(function() { | ||
this.query.where(this.syncing.format( | ||
_.extend(Object.create(null), this.syncing.attributes)) | ||
).limit(1); | ||
return this.select(); | ||
}, | ||
}), | ||
@@ -45,5 +43,5 @@ // Runs a `select` query on the database, adding any necessary relational | ||
// options will be called - used by both models & collections. | ||
select: function() { | ||
var columns, sync = this, syncing = this.syncing, | ||
options = this.options, relatedData = syncing.relatedData; | ||
select: Promise.method(function() { | ||
var columns, sync = this, | ||
options = this.options, relatedData = this.syncing.relatedData; | ||
@@ -56,3 +54,3 @@ // Inject all appropriate select costraints dealing with the relation | ||
columns = options.columns; | ||
if (!_.isArray(columns)) columns = columns ? [columns] : [_.result(syncing, 'tableName') + '.*']; | ||
if (!_.isArray(columns)) columns = columns ? [columns] : [_.result(this.syncing, 'tableName') + '.*']; | ||
} | ||
@@ -65,38 +63,36 @@ | ||
// Trigger a `fetching` event on the model, and then select the appropriate columns. | ||
return syncing.triggerThen('fetching', syncing, columns, options).then(function() { | ||
return sync.query.select(columns); | ||
return Promise.bind(this).then(function() { | ||
return this.syncing.triggerThen('fetching', this.syncing, columns, options); | ||
}).then(function() { | ||
return this.query.select(columns); | ||
}); | ||
}, | ||
}), | ||
// Issues an `insert` command on the query - only used by models. | ||
insert: function() { | ||
insert: Promise.method(function() { | ||
var syncing = this.syncing; | ||
return this.query | ||
.insert(syncing.format(_.extend(Object.create(null), syncing.attributes)), syncing.idAttribute); | ||
}, | ||
}), | ||
// Issues an `update` command on the query - only used by models. | ||
update: function(attrs) { | ||
update: Promise.method(function(attrs) { | ||
var syncing = this.syncing, query = this.query; | ||
if (syncing.id != null) query.where(syncing.idAttribute, syncing.id); | ||
if (query.wheres.length === 0) { | ||
return when.reject(new Error('A model cannot be updated without a "where" clause or an idAttribute.')); | ||
throw new Error('A model cannot be updated without a "where" clause or an idAttribute.'); | ||
} | ||
return query.update(syncing.format(_.extend(Object.create(null), attrs))); | ||
}, | ||
}), | ||
// Issues a `delete` command on the query. | ||
del: function() { | ||
del: Promise.method(function() { | ||
var query = this.query, syncing = this.syncing; | ||
if (syncing.id != null) query.where(syncing.idAttribute, syncing.id); | ||
if (query.wheres.length === 0) { | ||
return when.reject(new Error('A model cannot be destroyed without a "where" clause or an idAttribute.')); | ||
throw new Error('A model cannot be destroyed without a "where" clause or an idAttribute.'); | ||
} | ||
return this.query.del(); | ||
}, | ||
}) | ||
_init: function(syncing, options) { | ||
if (options.transacting) this.query.transacting(options.transacting); | ||
} | ||
}); | ||
@@ -103,0 +99,0 @@ |
{ | ||
"name": "bookshelf", | ||
"version": "0.5.8", | ||
"version": "0.6.0", | ||
"description": "A lightweight ORM for PostgreSQL, MySQL, and SQLite3, influenced by Backbone.js", | ||
@@ -24,13 +24,13 @@ "main": "bookshelf.js", | ||
"dependencies": { | ||
"backbone": "~1.0.0", | ||
"backbone": "~1.1.0", | ||
"inflection": "~1.2.x", | ||
"when": "~2.4.0", | ||
"trigger-then": "~0.1.1", | ||
"underscore": "~1.5.1", | ||
"knex": "~0.4.6" | ||
"knex": "~0.5.0", | ||
"bluebird": "~0.10.5-0", | ||
"lodash": "~2.3.0" | ||
}, | ||
"devDependencies": { | ||
"mocha": "~1.13.0", | ||
"mocha": "~1.14.0", | ||
"mysql": "~2.0.0-alpha7", | ||
"pg": "~2.6.2", | ||
"pg": "~2.8.2", | ||
"sqlite3": "~2.1.7", | ||
@@ -40,5 +40,5 @@ "objectdump": "~0.3.0", | ||
"grunt": "~0.4.1", | ||
"grunt-release": "~0.5.1", | ||
"grunt-release": "~0.6.0", | ||
"mocha-as-promised": "~1.4.0", | ||
"chai-as-promised": "~3.3.1", | ||
"chai-as-promised": "~4.1.0", | ||
"chai": "~1.8.0", | ||
@@ -45,0 +45,0 @@ "sinon-chai": "~2.4.0", |
@@ -12,3 +12,3 @@ // Exec plugin | ||
var _ = require('underscore'); | ||
var _ = require('lodash'); | ||
@@ -15,0 +15,0 @@ // Accept the instance of `Bookshelf` we'd like to add `exec` support to. |
@@ -1,5 +0,5 @@ | ||
var when = require('when'); | ||
var Promise = testPromise; | ||
var assert = require('assert'); | ||
var equal = assert.equal; | ||
var _ = require('underscore'); | ||
var _ = require('lodash'); | ||
@@ -24,3 +24,3 @@ module.exports = function() { | ||
invokedMethod: function() { | ||
return when(this.id); | ||
return Promise.resolve(this.id); | ||
} | ||
@@ -103,9 +103,5 @@ }) | ||
var spyIterator = sinon.spy(function(model) { | ||
return model.id; | ||
}); | ||
return collection.invokeThen('invokedMethod').then(function(resp) { | ||
expect(_.compact(resp)).to.eql([1]); | ||
}); | ||
}) | ||
@@ -112,0 +108,0 @@ }); |
@@ -0,4 +1,9 @@ | ||
var Promise = require('../dialects/base/promise').Promise; | ||
require('when/monitor/console'); | ||
global.testPromise = Promise; | ||
Promise.onPossiblyUnhandledRejection(function(e) { | ||
console.log(e.stack); | ||
}); | ||
var Bookshelf = require('../bookshelf'); | ||
@@ -18,3 +23,2 @@ var base = require('./base'); | ||
global.whenResolve = require('when').resolve; | ||
global.expect = chai.expect; | ||
@@ -21,0 +25,0 @@ global.AssertionError = chai.AssertionError; |
@@ -1,2 +0,2 @@ | ||
var _ = require('underscore'); | ||
var _ = require('lodash'); | ||
@@ -7,3 +7,3 @@ module.exports = function(Bookshelf) { | ||
var config = require(process.env.BOOKSHELF_TEST || './integration/helpers/config'); | ||
var nodefn = require('when/node/function'); | ||
var Promise = global.testPromise; | ||
@@ -15,3 +15,3 @@ var MySQL = Bookshelf.initialize({ | ||
afterCreate: function(connection, callback) { | ||
return nodefn.call(connection.query.bind(connection), "SET sql_mode='TRADITIONAL';", []).then(function() { | ||
return Promise.promisify(connection.query, connection)("SET sql_mode='TRADITIONAL';", []).then(function() { | ||
callback(null, connection); | ||
@@ -18,0 +18,0 @@ }); |
@@ -1,2 +0,2 @@ | ||
var when = require('when'); | ||
var Promise = global.testPromise; | ||
@@ -142,3 +142,3 @@ module.exports = function(Bookshelf) { | ||
expect(this.values[0]).to.eql([['first_name', 'Test'], ['last_name', 'User'], ['site_id', 1]]); | ||
return when.resolve(this.toString()).then(onFufilled, onRejected); | ||
return Promise.resolve(this.toString()).then(onFufilled, onRejected); | ||
}; | ||
@@ -145,0 +145,0 @@ |
@@ -1,3 +0,3 @@ | ||
var when = require('when'); | ||
var _ = require('underscore'); | ||
var Promise = global.testPromise; | ||
var _ = require('lodash'); | ||
@@ -8,3 +8,3 @@ module.exports = function(bookshelf) { | ||
return when.all([ | ||
return Promise.all([ | ||
@@ -11,0 +11,0 @@ knex('sites').insert([{ |
var cwd = process.cwd(); | ||
var isDev = parseInt(process.env.BOOKSHELF_DEV, 10); | ||
var _ = require('underscore'); | ||
var _ = require('lodash'); | ||
@@ -6,0 +6,0 @@ var Ctors = {}; |
@@ -1,3 +0,3 @@ | ||
var _ = require('underscore'); | ||
var when = require('when'); | ||
var _ = require('lodash'); | ||
var Promise = global.testPromise; | ||
@@ -16,3 +16,3 @@ var drops = [ | ||
return when.all(_.map(drops, function(val) { | ||
return Promise.all(_.map(drops, function(val) { | ||
return schema.dropTableIfExists(val); | ||
@@ -22,3 +22,3 @@ })) | ||
return when.all([ | ||
return Promise.all([ | ||
schema.createTable('sites', function(table) { | ||
@@ -25,0 +25,0 @@ table.increments('id'); |
@@ -1,5 +0,5 @@ | ||
var _ = require('underscore'); | ||
var _ = require('lodash'); | ||
_.str = require('underscore.string'); | ||
var when = require('when'); | ||
var Promise = global.testPromise; | ||
var equal = require('assert').equal; | ||
@@ -16,7 +16,7 @@ var deepEqual = require('assert').deepEqual; | ||
var stubSync = { | ||
first: function() { return when.resolve({}); }, | ||
select: function() { return when.resolve({}); }, | ||
insert: function() { return when.resolve({}); }, | ||
update: function() { return when.resolve({}); }, | ||
del: function() { return when.resolve({}); } | ||
first: function() { return Promise.resolve({}); }, | ||
select: function() { return Promise.resolve({}); }, | ||
insert: function() { return Promise.resolve({}); }, | ||
update: function() { return Promise.resolve({}); }, | ||
del: function() { return Promise.resolve({}); } | ||
}; | ||
@@ -325,3 +325,3 @@ | ||
equal(this.wheres.length, 1); | ||
return when.resolve({}); | ||
return Promise.resolve({}); | ||
}; | ||
@@ -351,3 +351,3 @@ | ||
equal(this.wheres.length, 1); | ||
return when.resolve(this.toString()).then(onFulfilled, onRejected); | ||
return Promise.resolve(this.toString()).then(onFulfilled, onRejected); | ||
}; | ||
@@ -354,0 +354,0 @@ |
@@ -1,2 +0,2 @@ | ||
var _ = require('underscore'); | ||
var _ = require('lodash'); | ||
@@ -3,0 +3,0 @@ module.exports = function(Bookshelf) { |
@@ -1,3 +0,3 @@ | ||
var _ = require('underscore'); | ||
var when = require('when'); | ||
var _ = require('lodash'); | ||
var Promise = global.testPromise; | ||
var equal = require('assert').equal; | ||
@@ -204,3 +204,3 @@ | ||
before(function() { | ||
return when.all([ | ||
return Promise.all([ | ||
new Site({id: 1}).admins().detach(), | ||
@@ -219,6 +219,6 @@ new Site({id: 2}).admins().detach() | ||
return when.all([admin1.save(), admin2.save()]) | ||
return Promise.all([admin1.save(), admin2.save()]) | ||
.then(function() { | ||
admin1_id = admin1.id; | ||
return when.all([ | ||
return Promise.all([ | ||
site1.related('admins').attach([admin1, admin2]), | ||
@@ -232,3 +232,3 @@ site2.related('admins').attach(admin2) | ||
}).then(function() { | ||
return when.all([ | ||
return Promise.all([ | ||
new Site({id: 1}).related('admins').fetch().then(function(c) { | ||
@@ -247,3 +247,3 @@ c.each(function(m) { | ||
.then(function(resp) { | ||
return when.all([ | ||
return Promise.all([ | ||
new Site({id: 1}).related('admins').fetch(), | ||
@@ -254,3 +254,3 @@ new Site({id: 2}).related('admins').fetch() | ||
.spread(function(admins1, admins2) { | ||
return when.all([ | ||
return Promise.all([ | ||
admins1.detach(admin1_id).then(function(c) { | ||
@@ -464,3 +464,3 @@ expect(admins1).to.have.length(1); | ||
it('correctly pairs eager-loaded models before parse()', function () { | ||
return when.all([ | ||
return Promise.all([ | ||
new Blog({id: 1}).related('parsedPosts').fetch(), | ||
@@ -467,0 +467,0 @@ new Blog({id: 1}).fetch({ withRelated: 'parsedPosts' }) |
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
256075
48
6988
+ Addedbluebird@~0.10.5-0
+ Addedlodash@~2.3.0
+ Addedbackbone@1.1.2(transitive)
+ Addedbluebird@0.10.5-01.2.4(transitive)
+ Addedcolors@0.6.2(transitive)
+ Addedknex@0.5.16(transitive)
+ Addedlodash@2.3.04.17.21(transitive)
+ Addedminimist@0.0.10(transitive)
+ Addedmkdirp@0.3.5(transitive)
+ Addedoptimist@0.6.1(transitive)
+ Addedunderscore@1.13.7(transitive)
+ Addedwordwrap@0.0.3(transitive)
- Removedunderscore@~1.5.1
- Removedwhen@~2.4.0
- Removedbackbone@1.0.0(transitive)
- Removedknex@0.4.13(transitive)
- Removedunderscore@1.5.2(transitive)
- Removedwhen@2.4.0(transitive)
Updatedbackbone@~1.1.0
Updatedknex@~0.5.0