Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

bookshelf

Package Overview
Dependencies
Maintainers
1
Versions
87
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

bookshelf - npm Package Compare versions

Comparing version 0.1.1 to 0.1.2

110

bookshelf.js

@@ -1,2 +0,2 @@

// Bookshelf.js 0.1.1
// Bookshelf.js 0.1.2

@@ -20,8 +20,11 @@ // (c) 2013 Tim Griesser

var When = require('when');
var WhenFn = require('when/function');
var Knex = require('knex');
var Inflection = require('inflection');
// Mixin the `triggerThen` function into all relevant Backbone objects,
// so we can have event driven async validations, functions, etc.
require('trigger-then')(Backbone, When);
// Keep in sync with `package.json`.
Bookshelf.VERSION = '0.1.1';
Bookshelf.VERSION = '0.1.2';

@@ -33,2 +36,3 @@ // We're using `Backbone.Events` rather than `EventEmitter`,

Events.emit = function() { this.trigger.apply(this, arguments); };
Events.emitThen = function() { return this.triggerThen.apply(this, arguments); };
Events.removeAllListeners = function(event) { this.off(event, null, null); };

@@ -114,3 +118,2 @@

}
return When.resolve();
}

@@ -238,3 +241,3 @@

}
// Set the attributes on the model, and maintain a reference to use below.

@@ -245,9 +248,9 @@ var model = this.set(vals);

return WhenFn.call(function() {
model.trigger((method === 'insert' ? 'creating' : 'updating'), model, attrs, options);
model.trigger('saving', model, attrs, options);
})
return When.all([
model.triggerThen((method === 'insert' ? 'creating' : 'updating'), model, attrs, options),
model.triggerThen('saving', model, attrs, options)
])
.then(function() { return sync[method](attrs, options); })
.then(function(resp) {
// After a successful database save, the id is updated if the model was created

@@ -269,5 +272,3 @@ if (method === 'insert' && resp) model.set(model.idAttribute, resp[0]);

var model = this;
return WhenFn.call(function() {
model.trigger('destroying', model, options);
})
return model.triggerThen('destroying', model, options)
.then(function() { return model.sync(model, options).del(options); })

@@ -296,3 +297,3 @@ .then(function(resp) {

for (var key in relations) {
attrs[key] = relations[key];
attrs[key] = relations[key].toJSON();
}

@@ -478,3 +479,3 @@ return attrs;

if (handled[name]) continue;
// Internal flag to determine whether to set the ctor(s) on the _relation hash.

@@ -635,3 +636,3 @@ target._isEager = true;

return this._addConstraints(relation.parentResponse).then(function() {
return When(this._addConstraints(relation.parentResponse)).then(function() {
return current.query().select(relation.columns);

@@ -712,3 +713,3 @@ })

return model._addConstraints().then(function() {
return When(model._addConstraints()).then(function() {
var columns = options.columns;

@@ -785,3 +786,3 @@

.where(this.model.idAttribute, this.model.id)
.update(this.model.format(_.extend({}, this.model.attributes)));
.update(this.model.format(_.extend({}, attrs)));
},

@@ -811,32 +812,2 @@

_handler: function(method, ids, options) {
if (ids == void 0 && method === 'insert') return When.resolve();
if (!_.isArray(ids)) ids = ids ? [ids] : [];
var pivot = this._relation;
var context = this;
return When.all(_.map(ids, function(item) {
var data = {};
data[pivot.otherKey] = pivot.fkValue;
// If the item is an object, it's either a model
// that we're looking to attach to this model, or
// a hash of attributes to set in the relation.
if (_.isObject(item)) {
if (item instanceof Model) {
data[pivot.foreignKey] = item.id;
} else {
_.extend(data, item);
}
} else if (item) {
data[pivot.foreignKey] = item;
}
var builder = context.builder(pivot.joinTableName);
if (options && options.transacting) {
builder.transacting(options.transacting);
}
if (method === 'delete') return builder.where(data).del();
return builder.insert(data);
}));
},
// Attach one or more "ids" from a foreign

@@ -878,3 +849,44 @@ // table to the current. Creates & saves a new model

return this;
},
// Helper for handling either the `attach` or `detach` call on
// the `belongsToMany` relationship.
_handler: function(method, ids, options) {
if (ids == void 0 && method === 'insert') return When.resolve();
if (!_.isArray(ids)) ids = ids ? [ids] : [];
var pending = [];
for (var i = 0, l = ids.length; i < l; i++) {
pending.push(this._processPivot(method, ids[i], options));
}
return When.all(pending);
},
// Handles setting the appropriate constraints and shelling out
// to either the `insert` or `delete` call for the current model,
// returning a promise.
_processPivot: function(method, item, options) {
var data = {};
var pivot = this._relation;
data[pivot.otherKey] = pivot.fkValue;
// If the item is an object, it's either a model
// that we're looking to attach to this model, or
// a hash of attributes to set in the relation.
if (_.isObject(item)) {
if (item instanceof Model) {
data[pivot.foreignKey] = item.id;
} else {
_.extend(data, item);
}
} else if (item) {
data[pivot.foreignKey] = item;
}
var builder = this.builder(pivot.joinTableName);
if (options && options.transacting) {
builder.transacting(options.transacting);
}
if (method === 'delete') return builder.where(data).del();
return builder.insert(data);
}
};

@@ -922,3 +934,3 @@

Target = Bookshelf.Instances[name] = {};
// Create a new `Bookshelf` instance for this database connection.

@@ -925,0 +937,0 @@ _.extend(Target, _.omit(Bookshelf, 'Instances', 'Initialize', 'Knex', 'Transaction', 'VERSION'), {

{
"name": "bookshelf",
"author": "Tim Griesser",
"version": "0.1.1",
"version": "0.1.2",
"description": "An ORM with some Backbone",

@@ -26,3 +26,4 @@ "main": "bookshelf.js",

"inflection": "~1.2.x",
"when": "~2.1.0"
"when": "~2.1.0",
"trigger-then": ">=0.1.1"
},

@@ -34,3 +35,4 @@ "devDependencies": {

"sqlite3": "~2.1.7",
"objectdump": "~0.3.0"
"objectdump": "~0.3.0",
"underscore.string": "~2.3.1"
},

@@ -37,0 +39,0 @@ "license": "MIT",

var _ = require('underscore');
_.str = require('underscore.string');
var When = require('when');

@@ -182,2 +184,31 @@

describe('format', function() {
// TODO: better way to test this.
it('calls format when saving', function(ok) {
var M = Backbone.Model.extend({
tableName: 'test',
format: function(attrs) {
return _.reduce(attrs, function(memo, val, key) {
memo[_.str.underscored(key)] = val;
return memo;
}, {});
}
});
var m = new M({firstName: 'Tim', lastName: 'G'});
m.sync = function() {
var data = this.format(_.extend({}, this.attributes));
equal(data.first_name, 'Tim');
equal(data.last_name, 'G');
ok();
return stubSync;
};
m.save();
});
});
describe('fetch', function() {

@@ -184,0 +215,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc