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

backbone-sql

Package Overview
Dependencies
Maintainers
2
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

backbone-sql - npm Package Compare versions

Comparing version 0.5.8 to 0.6.0

lib/core.js

98

lib/cursor.js
// Generated by CoffeeScript 1.7.1
/*
backbone-sql.js 0.5.7
backbone-sql.js 0.6.0
Copyright (c) 2013 Vidigami - https://github.com/vidigami/backbone-sql

@@ -10,3 +10,3 @@ License: MIT (http://www.opensource.org/licenses/mit-license.php)

(function() {
var COMPARATORS, COMPARATOR_KEYS, Cursor, Knex, SqlCursor, util, _, _appendCondition, _appendConditionalWhere, _appendSort, _appendWhere, _columnName, _extractCount,
var COMPARATORS, COMPARATOR_KEYS, Knex, SqlCursor, sync, _, _appendCondition, _appendConditionalWhere, _appendSort, _appendWhere, _columnName, _extractCount, _ref,
__hasProp = {}.hasOwnProperty,

@@ -16,9 +16,5 @@ __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },

util = require('util');
_ = require('underscore');
Knex = require('knex');
Cursor = require('backbone-orm/lib/cursor');
_ref = require('backbone-orm'), _ = _ref._, sync = _ref.sync;

@@ -36,5 +32,5 @@ COMPARATORS = {

_appendCondition = function(conditions, key, value) {
var mongo_op, mongo_ops, operations, ops_length, parameter, _ref, _ref1;
var mongo_op, mongo_ops, operations, ops_length, parameter, _ref1, _ref2;
if (value != null ? value.$in : void 0) {
if ((_ref = value.$in) != null ? _ref.length : void 0) {
if ((_ref1 = value.$in) != null ? _ref1.length : void 0) {
conditions.where_ins.push({

@@ -49,3 +45,3 @@ key: key,

} else if (value != null ? value.$nin : void 0) {
if ((_ref1 = value.$nin) != null ? _ref1.length : void 0) {
if ((_ref2 = value.$nin) != null ? _ref2.length : void 0) {
conditions.where_nins.push({

@@ -112,6 +108,6 @@ key: key,

_appendWhere = function(query, conditions, table) {
var condition, _i, _j, _k, _l, _len, _len1, _len2, _len3, _ref, _ref1, _ref2, _ref3;
_ref = conditions.wheres;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
condition = _ref[_i];
var condition, _i, _j, _k, _l, _len, _len1, _len2, _len3, _ref1, _ref2, _ref3, _ref4;
_ref1 = conditions.wheres;
for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
condition = _ref1[_i];
if (_.isNull(condition.value)) {

@@ -123,15 +119,15 @@ query.whereNull(_columnName(condition.key, table));

}
_ref1 = conditions.where_conditionals;
for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
condition = _ref1[_j];
_ref2 = conditions.where_conditionals;
for (_j = 0, _len1 = _ref2.length; _j < _len1; _j++) {
condition = _ref2[_j];
if (condition.operations) {
query.where(function() {
var nested_query, operation, _k, _len2, _ref2, _results;
var nested_query, operation, _k, _len2, _ref3, _results;
operation = condition.operations.pop();
nested_query = this;
_appendConditionalWhere(nested_query, condition.key, operation, table, false);
_ref2 = condition.operations;
_ref3 = condition.operations;
_results = [];
for (_k = 0, _len2 = _ref2.length; _k < _len2; _k++) {
operation = _ref2[_k];
for (_k = 0, _len2 = _ref3.length; _k < _len2; _k++) {
operation = _ref3[_k];
_results.push(_appendConditionalWhere(nested_query, condition.key, operation, table, true));

@@ -147,10 +143,10 @@ }

}
_ref2 = conditions.where_ins;
for (_k = 0, _len2 = _ref2.length; _k < _len2; _k++) {
condition = _ref2[_k];
_ref3 = conditions.where_ins;
for (_k = 0, _len2 = _ref3.length; _k < _len2; _k++) {
condition = _ref3[_k];
query.whereIn(_columnName(condition.key, table), condition.value);
}
_ref3 = conditions.where_nins;
for (_l = 0, _len3 = _ref3.length; _l < _len3; _l++) {
condition = _ref3[_l];
_ref4 = conditions.where_nins;
for (_l = 0, _len3 = _ref4.length; _l < _len3; _l++) {
condition = _ref4[_l];
query.whereNotIn(_columnName(condition.key, table), condition.value);

@@ -195,3 +191,3 @@ }

SqlCursor.prototype._parseConditions = function(find, cursor) {
var conditions, key, related_conditions, related_wheres, relation, reverse_relation, value, _base, _name, _ref;
var conditions, key, related_conditions, related_wheres, relation, reverse_relation, value, _base, _name, _ref1;
conditions = {

@@ -212,3 +208,3 @@ wheres: [],

if (key.indexOf('.') > 0) {
_ref = key.split('.'), relation = _ref[0], key = _ref[1];
_ref1 = key.split('.'), relation = _ref1[0], key = _ref1[1];
related_wheres[relation] || (related_wheres[relation] = {});

@@ -247,3 +243,3 @@ related_wheres[relation][key] = value;

SqlCursor.prototype.queryToJSON = function(callback) {
var $columns, $fields, err, from_columns, key, query, related_model_type, related_wheres, relation, to_columns, _i, _len, _ref;
var $columns, $fields, err, from_columns, key, query, related_model_type, related_wheres, relation, to_columns, _i, _len, _ref1;
if (this.hasCursorQuery('$zero')) {

@@ -294,5 +290,5 @@ return callback(null, this.hasCursorQuery('$one') ? null : []);

to_columns = [];
_ref = this.include_keys;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
key = _ref[_i];
_ref1 = this.include_keys;
for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
key = _ref1[_i];
relation = this._getRelation(key);

@@ -392,9 +388,9 @@ related_model_type = relation.reverse_relation.model_type;

SqlCursor.prototype._appendCompleteRelations = function(json, callback) {
var key, new_query, related_model_type, relation, to_columns, _i, _len, _ref;
var key, new_query, related_model_type, relation, to_columns, _i, _len, _ref1;
new_query = this.connection(this.model_type.tableName());
new_query.whereIn(_columnName('id', this.model_type.tableName()), _.pluck(json, 'id'));
to_columns = [];
_ref = this.queued_queries;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
key = _ref[_i];
_ref1 = this.queued_queries;
for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
key = _ref1[_i];
relation = this._getRelation(key);

@@ -423,3 +419,3 @@ related_model_type = relation.reverse_relation.model_type;

SqlCursor.prototype._appendRelatedWheres = function(query) {
var key, related_wheres, relation, _ref, _results;
var key, related_wheres, relation, _ref1, _results;
if (_.isEmpty(this._conditions.related_wheres)) {

@@ -432,6 +428,6 @@ return;

}
_ref = this._conditions.related_wheres;
_ref1 = this._conditions.related_wheres;
_results = [];
for (key in _ref) {
related_wheres = _ref[key];
for (key in _ref1) {
related_wheres = _ref1[key];
relation = this._getRelation(key);

@@ -445,3 +441,3 @@ this._joinTo(query, relation);

SqlCursor.prototype._appendJoinedWheres = function(query) {
var from_key, joined_wheres, key, relation, to_key, _ref, _results;
var from_key, joined_wheres, key, relation, to_key, _ref1, _results;
if (_.isEmpty(this._conditions.joined_wheres)) {

@@ -451,6 +447,6 @@ return;

this.joined = true;
_ref = this._conditions.joined_wheres;
_ref1 = this._conditions.joined_wheres;
_results = [];
for (key in _ref) {
joined_wheres = _ref[key];
for (key in _ref1) {
joined_wheres = _ref1[key];
relation = this._getRelation(key);

@@ -491,3 +487,3 @@ if (!(__indexOf.call(_.keys(this._conditions.related_wheres), key) >= 0 || (this.include_keys && __indexOf.call(this.include_keys, key) >= 0))) {

SqlCursor.prototype._joinedResultsToJSON = function(raw_json) {
var found, include_key, json, key, match, model_json, related_json, related_model_type, reverse_relation_schema, row, row_relation_json, value, _i, _j, _len, _len1, _ref;
var found, include_key, json, key, match, model_json, related_json, related_model_type, reverse_relation_schema, row, row_relation_json, value, _i, _j, _len, _len1, _ref1;
if (!(raw_json && raw_json.length)) {

@@ -506,5 +502,5 @@ return raw_json;

} else if (this.include_keys) {
_ref = this.include_keys;
for (_j = 0, _len1 = _ref.length; _j < _len1; _j++) {
include_key = _ref[_j];
_ref1 = this.include_keys;
for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
include_key = _ref1[_j];
related_json = (row_relation_json[include_key] || (row_relation_json[include_key] = {}));

@@ -554,3 +550,3 @@ related_model_type = this.model_type.relation(include_key).reverse_relation.model_type;

var col, columns;
columns = fields ? _.clone(fields) : model_type.schema().allColumns();
columns = fields ? _.clone(fields) : model_type.schema().columns();
if (__indexOf.call(columns, 'id') < 0) {

@@ -588,4 +584,4 @@ columns.push('id');

})(Cursor);
})(sync.Cursor);
}).call(this);
// Generated by CoffeeScript 1.7.1
/*
backbone-sql.js 0.5.7
backbone-sql.js 0.6.0
Copyright (c) 2013 Vidigami - https://github.com/vidigami/backbone-sql

@@ -10,20 +10,30 @@ License: MIT (http://www.opensource.org/licenses/mit-license.php)

(function() {
var DatabaseTools, KNEX_COLUMN_OPERATORS, KNEX_COLUMN_OPTIONS, Knex, Queue, inflection, _,
var DatabaseTools, KNEX_COLUMN_OPTIONS, KNEX_SKIP, KNEX_TYPES, Knex, Queue, knex_helpers, _, _deprecate, _ref,
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
_ = require('underscore');
Knex = require('knex');
inflection = require('inflection');
_ref = require('backbone-orm'), _ = _ref._, Queue = _ref.Queue;
Knex = require('knex');
KNEX_COLUMN_OPTIONS = ['textType', 'length', 'precision', 'scale', 'value', 'values'];
Queue = require('backbone-orm/lib/queue');
knex_helpers = require('knex/lib/helpers');
KNEX_COLUMN_OPERATORS = ['indexed', 'nullable', 'unique'];
KNEX_SKIP = ['The five argument join'];
KNEX_COLUMN_OPTIONS = ['textType', 'length', 'precision', 'scale', 'value', 'values'];
_deprecate = knex_helpers.deprecate;
knex_helpers.deprecate = function(msg) {
if (msg.indexOf(KNEX_SKIP) !== 0) {
return _deprecate.apply(this, _.toArray(arguments));
}
};
KNEX_TYPES = {
datetime: 'dateTime',
biginteger: 'bigInteger'
};
module.exports = DatabaseTools = (function() {
function DatabaseTools(connection, table_name, schema, options) {
var _ref;
this.connection = connection;

@@ -40,205 +50,48 @@ this.table_name = table_name;

this.hasColumn = __bind(this.hasColumn, this);
this.ensureColumn = __bind(this.ensureColumn, this);
this.ensureField = __bind(this.ensureField, this);
this.ensureRelation = __bind(this.ensureRelation, this);
this.ensureSchemaForExistingTable = __bind(this.ensureSchemaForExistingTable, this);
this.updateColumn = __bind(this.updateColumn, this);
this.addColumn = __bind(this.addColumn, this);
this.createOrUpdateTable = __bind(this.createOrUpdateTable, this);
this.ensureSchema = __bind(this.ensureSchema, this);
this.resetSchema = __bind(this.resetSchema, this);
this.resetRelation = __bind(this.resetRelation, this);
this.addRelation = __bind(this.addRelation, this);
this.addColumn = __bind(this.addColumn, this);
this.addIDColumn = __bind(this.addIDColumn, this);
this.addField = __bind(this.addField, this);
this.editTable = __bind(this.editTable, this);
this.createTable = __bind(this.createTable, this);
this.end = __bind(this.end, this);
this.reset = __bind(this.reset, this);
this.strict = (_ref = options.strict) != null ? _ref : true;
this.join_table_operations = [];
this.reset();
}
DatabaseTools.prototype.reset = function() {
this.promise = this.table = null;
return this;
};
DatabaseTools.prototype.end = function(callback) {
if (!this.promise) {
if (this.strict) {
return callback(new Error('end() called with no operations in progress, call createTable or editTable first'));
}
DatabaseTools.prototype.resetSchema = function(options, callback) {
var queue, _ref1;
if (arguments.length === 1) {
_ref1 = [options, {}], callback = _ref1[0], options = _ref1[1];
}
if (this.resetting) {
return callback();
}
return this.promise.exec((function(_this) {
return function(err) {
var join_table_fn, queue, _fn, _i, _len, _ref;
_this.reset();
if (err) {
return callback(err);
}
if (_this.join_table_operations.length) {
queue = new Queue(1);
_ref = _this.join_table_operations;
_fn = function(join_table_fn) {
return queue.defer(function(callback) {
return join_table_fn(callback);
});
};
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
join_table_fn = _ref[_i];
_fn(join_table_fn);
}
return queue.await(function(err) {
_this.join_table_operations = [];
return callback(err);
this.resetting = true;
queue = new Queue(1);
queue.defer((function(_this) {
return function(callback) {
return _this.connection.knex().schema.dropTableIfExists(_this.table_name).exec(callback);
};
})(this));
queue.defer((function(_this) {
return function(callback) {
var join_queue, join_table, _fn, _i, _len, _ref2;
join_queue = new Queue(1);
_ref2 = _this.schema.joinTables();
_fn = function(join_table) {
return join_queue.defer(function(callback) {
return join_table.db().resetSchema(callback);
});
} else {
return callback();
};
for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
join_table = _ref2[_i];
_fn(join_table);
}
return join_queue.await(callback);
};
})(this));
};
DatabaseTools.prototype.createTable = function() {
if (this.promise && this.table) {
if (this.strict) {
throw Error("Table operation on " + this.table_name + " already in progress, call end() first");
}
return this;
}
this.promise = this.connection.knex().schema.createTable(this.table_name, (function(_this) {
return function(t) {
return _this.table = t;
};
})(this));
return this;
};
DatabaseTools.prototype.editTable = function() {
if (this.promise && this.table) {
if (this.strict) {
throw Error("Table operation on " + this.table_name + " already in progress, call end() first");
}
return this;
}
this.promise = this.connection.knex().schema.table(this.table_name, (function(_this) {
return function(t) {
return _this.table = t;
};
})(this));
return this;
};
DatabaseTools.prototype.addField = function(key, field) {
var type;
if (!this.table) {
this.editTable();
}
type = "" + (field.type[0].toLowerCase()) + (field.type.slice(1));
this.addColumn(key, type, field);
return this;
};
DatabaseTools.prototype.addIDColumn = function() {
return this.addColumn('id', 'increments', ['primary']);
};
DatabaseTools.prototype.addColumn = function(key, type, options) {
var column, column_args, constructor_options, knex_methods, method, _i, _len;
if (options == null) {
options = {};
}
if (!this.table) {
this.editTable();
}
column_args = [key];
constructor_options = _.pick(options, KNEX_COLUMN_OPTIONS);
if (!_.isEmpty(constructor_options)) {
if (type === 'float' || type === 'decimal') {
column_args[1] = constructor_options['precision'];
column_args[2] = constructor_options['scale'];
} else {
column_args[1] = _.values(constructor_options)[0];
}
}
column = this.table[type].apply(this.table, column_args);
knex_methods = [];
if (options.nullable === false) {
knex_methods.push['notNullable'];
}
if (options.indexed) {
knex_methods.push('index');
}
if (options.unique) {
knex_methods.push('unique');
}
for (_i = 0, _len = knex_methods.length; _i < _len; _i++) {
method = knex_methods[_i];
column[method]();
}
return this;
};
DatabaseTools.prototype.addRelation = function(key, relation) {
if (!this.table) {
this.editTable();
}
if (relation.isVirtual()) {
return;
}
if (relation.type === 'belongsTo') {
this.addColumn(relation.foreign_key, 'integer', ['nullable', 'index']);
} else if (relation.type === 'hasMany' && relation.reverse_relation.type === 'hasMany') {
this.join_table_operations.push(function(callback) {
return relation.findOrGenerateJoinTable().db().ensureSchema(callback);
});
}
return this;
};
DatabaseTools.prototype.resetRelation = function(key, relation) {
if (!this.table) {
this.editTable();
}
if (relation.isVirtual()) {
return;
}
if (relation.type === 'belongsTo') {
this.addColumn(relation.foreign_key, 'integer', ['nullable', 'index']);
} else if (relation.type === 'hasMany' && relation.reverse_relation.type === 'hasMany') {
this.join_table_operations.push(function(callback) {
return relation.findOrGenerateJoinTable().resetSchema(callback);
});
}
return this;
};
DatabaseTools.prototype.resetSchema = function(options, callback) {
if (arguments.length === 1) {
callback = options;
options = {};
}
return this.connection.knex().schema.dropTableIfExists(this.table_name).exec((function(_this) {
return queue.await((function(_this) {
return function(err) {
var field, key, relation, _ref, _ref1;
_this.resetting = false;
if (err) {
return callback(err);
}
_this.createTable();
if (options.verbose) {
console.log("Creating table: " + _this.table_name + " with fields: '" + (_.keys(_this.schema.fields).join(', ')) + "' and relations: '" + (_.keys(_this.schema.relations).join(', ')) + "'");
}
_this.addIDColumn();
_ref = _this.schema.fields;
for (key in _ref) {
field = _ref[key];
_this.addField(key, field);
}
_ref1 = _this.schema.relations;
for (key in _ref1) {
relation = _ref1[key];
_this.resetRelation(key, relation);
}
return _this.end(callback);
return _this.ensureSchema(options, callback);
};

@@ -249,5 +102,5 @@ })(this));

DatabaseTools.prototype.ensureSchema = function(options, callback) {
var queue, _ref1;
if (arguments.length === 1) {
callback = options;
options = {};
_ref1 = [options, {}], callback = _ref1[0], options = _ref1[1];
}

@@ -258,77 +111,29 @@ if (this.ensuring) {

this.ensuring = true;
return this.hasTable((function(_this) {
return function(err, table_exists) {
if (err) {
_this.ensuring = false;
return callback(err);
}
if (options.verbose) {
console.log("Ensuring table: " + _this.table_name + " with fields: '" + (_.keys(_this.schema.fields).join(', ')) + "' and relations: '" + (_.keys(_this.schema.relations).join(', ')) + "'");
}
if (!table_exists) {
_this.createTable();
_this.addIDColumn();
return _this.end(function(err) {
if (err) {
_this.ensuring = false;
return callback(err);
}
return _this.ensureSchemaForExistingTable(options, function(err) {
_this.ensuring = false;
return callback(err);
});
});
} else {
return _this.ensureSchemaForExistingTable(options, function(err) {
_this.ensuring = false;
return callback(err);
});
}
};
})(this));
};
DatabaseTools.prototype.ensureSchemaForExistingTable = function(options, callback) {
var field, key, queue, relation, _fn, _fn1, _ref, _ref1;
this.editTable();
queue = new Queue(1);
queue.defer((function(_this) {
return function(callback) {
return _this.ensureColumn('id', 'increments', ['primary'], callback);
return _this.createOrUpdateTable(options, callback);
};
})(this));
if (this.schema.fields) {
_ref = this.schema.fields;
_fn = (function(_this) {
return function(key, field) {
return queue.defer(function(callback) {
return _this.ensureField(key, field, callback);
queue.defer((function(_this) {
return function(callback) {
var join_queue, join_table, _fn, _i, _len, _ref2;
join_queue = new Queue(1);
_ref2 = _this.schema.joinTables();
_fn = function(join_table) {
return join_queue.defer(function(callback) {
return join_table.db().ensureSchema(callback);
});
};
})(this);
for (key in _ref) {
field = _ref[key];
_fn(key, field);
}
}
if (this.schema.relations) {
_ref1 = this.schema.relations;
_fn1 = (function(_this) {
return function(key, relation) {
return queue.defer(function(callback) {
return _this.ensureRelation(key, relation, callback);
});
};
})(this);
for (key in _ref1) {
relation = _ref1[key];
_fn1(key, relation);
}
}
for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
join_table = _ref2[_i];
_fn(join_table);
}
return join_queue.await(callback);
};
})(this));
return queue.await((function(_this) {
return function(err) {
if (err) {
return callback(err);
}
return _this.end(callback);
_this.ensuring = false;
return callback(err);
};

@@ -338,32 +143,104 @@ })(this));

DatabaseTools.prototype.ensureRelation = function(key, relation, callback) {
if (relation.type === 'belongsTo') {
return this.hasColumn(relation.foreign_key, (function(_this) {
return function(err, column_exists) {
if (err) {
return callback(err);
}
if (!column_exists) {
_this.addRelation(key, relation);
}
return callback();
};
})(this));
} else if (relation.type === 'hasMany' && relation.reverse_relation.type === 'hasMany') {
return relation.findOrGenerateJoinTable().db().ensureSchema(callback);
} else {
return callback();
}
};
DatabaseTools.prototype.ensureField = function(key, field, callback) {
return this.hasColumn(key, (function(_this) {
return function(err, column_exists) {
DatabaseTools.prototype.createOrUpdateTable = function(options, callback) {
this.hasTable((function(_this) {
return function(err, table_exists) {
var columns, field, group, key, override, relation, type, _i, _len, _ref1, _ref2;
if (err) {
return callback(err);
}
if (!column_exists) {
_this.addField(key, field);
if (options.verbose) {
console.log("Ensuring table: " + _this.table_name + " (exists: " + (!!table_exists) + ") with fields: '" + (_.keys(_this.schema.fields).join(', ')) + "' and relations: '" + (_.keys(_this.schema.relations).join(', ')) + "'");
}
return callback();
columns = [];
_ref1 = _this.schema.columns();
for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
key = _ref1[_i];
if (field = _this.schema.fields[key]) {
if (override = KNEX_TYPES[type = field.type.toLowerCase()]) {
type = override;
}
columns.push({
key: key,
type: type,
options: field
});
} else if (key === 'id') {
columns.push({
key: key,
type: 'increments',
options: {
indexed: true,
primary: true
}
});
}
}
_ref2 = _this.schema.relations;
for (key in _ref2) {
relation = _ref2[key];
if ((relation.type === 'belongsTo') && !relation.isVirtual() && !relation.isEmbedded()) {
(function(key, relation) {
return columns.push({
key: relation.foreign_key,
type: 'integer',
options: {
indexed: true,
nullable: true
}
});
})(key, relation);
}
}
group = function(columns, callback) {
var column_info, queue, result, _fn, _j, _len1;
if (!table_exists) {
return callback(null, {
add: columns,
update: []
});
}
result = {
add: [],
update: []
};
queue = new Queue();
_fn = function(column_info) {
return queue.defer(function(callback) {
return _this.hasColumn(column_info.key, function(err, exists) {
if (err) {
return callback(err);
}
(exists ? result.update : result.add).push(column_info);
return callback();
});
});
};
for (_j = 0, _len1 = columns.length; _j < _len1; _j++) {
column_info = columns[_j];
_fn(column_info);
}
return queue.await(function(err) {
return callback(err, result);
});
};
return group(columns, function(err, result) {
if (err) {
return callback(err);
}
return _this.connection.knex().schema[table_exists ? 'table' : 'createTable'](_this.table_name, function(table) {
var column_info, _j, _k, _len1, _len2, _ref3, _ref4, _results;
_ref3 = result.add;
for (_j = 0, _len1 = _ref3.length; _j < _len1; _j++) {
column_info = _ref3[_j];
_this.addColumn(table, column_info);
}
_ref4 = result.update;
_results = [];
for (_k = 0, _len2 = _ref4.length; _k < _len2; _k++) {
column_info = _ref4[_k];
_results.push(_this.updateColumn(table, column_info));
}
return _results;
}).exec(callback);
});
};

@@ -373,19 +250,31 @@ })(this));

DatabaseTools.prototype.ensureColumn = function(key, type, options, callback) {
if (!this.table) {
this.editTable();
DatabaseTools.prototype.addColumn = function(table, column_info) {
var column, column_args, constructor_options, _ref1;
column_args = [column_info.key];
constructor_options = _.pick(column_info.options, KNEX_COLUMN_OPTIONS);
if (!_.isEmpty(constructor_options)) {
if ((_ref1 = column_info.type) === 'float' || _ref1 === 'decimal') {
column_args[1] = constructor_options['precision'];
column_args[2] = constructor_options['scale'];
} else {
column_args[1] = _.values(constructor_options)[0];
}
}
return this.hasColumn(key, (function(_this) {
return function(err, column_exists) {
if (err) {
return callback(err);
}
if (!column_exists) {
_this.addColumn(key, type, options);
}
return callback();
};
})(this));
column = table[column_info.type].apply(table, column_args);
if (!!column_info.options.nullable) {
column.nullable();
}
if (!!column_info.options.primary) {
column.primary();
}
if (!!column_info.options.indexed) {
column.index();
}
if (!!column_info.options.unique) {
column.unique();
}
};
DatabaseTools.prototype.updateColumn = function(table, column_info) {};
DatabaseTools.prototype.hasColumn = function(column, callback) {

@@ -392,0 +281,0 @@ return this.connection.knex().schema.hasColumn(this.table_name, column).exec(callback);

// Generated by CoffeeScript 1.7.1
/*
backbone-sql.js 0.5.7
backbone-sql.js 0.6.0
Copyright (c) 2013 Vidigami - https://github.com/vidigami/backbone-sql

@@ -10,6 +10,27 @@ License: MIT (http://www.opensource.org/licenses/mit-license.php)

(function() {
module.exports = {
sync: require('./sync')
var Backbone, BackboneORM, BackboneSQL, key, publish, value, _, _ref, _ref1;
_ref = BackboneORM = require('backbone-orm'), _ = _ref._, Backbone = _ref.Backbone;
module.exports = BackboneSQL = require('./core');
publish = {
configure: require('./lib/configure'),
sync: require('./sync'),
_: _,
Backbone: Backbone
};
publish._.extend(BackboneSQL, publish);
BackboneSQL.modules = {
'backbone-orm': BackboneORM
};
_ref1 = BackboneORM.modules;
for (key in _ref1) {
value = _ref1[key];
BackboneSQL.modules[key] = value;
}
}).call(this);
// Generated by CoffeeScript 1.7.1
/*
backbone-sql.js 0.5.7
backbone-sql.js 0.6.0
Copyright (c) 2013 Vidigami - https://github.com/vidigami/backbone-sql

@@ -10,37 +10,22 @@ License: MIT (http://www.opensource.org/licenses/mit-license.php)

(function() {
var Backbone, Connection, DESTROY_BATCH_LIMIT, DatabaseTools, DatabaseURL, ModelCache, ModelTypeID, QueryCache, Queue, Schema, SqlCursor, SqlSync, Utils, inflection, modelExtensions, util, _,
var Backbone, BackboneORM, CAPABILITIES, Connection, DESTROY_BATCH_LIMIT, DatabaseTools, DatabaseURL, JSONUtils, Queue, SQLUtils, Schema, SqlCursor, SqlSync, Utils, _, _ref,
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
util = require('util');
_ref = BackboneORM = require('backbone-orm'), _ = _ref._, Backbone = _ref.Backbone, Queue = _ref.Queue, Schema = _ref.Schema, Utils = _ref.Utils, JSONUtils = _ref.JSONUtils, DatabaseURL = _ref.DatabaseURL;
_ = require('underscore');
SqlCursor = require('./cursor');
Backbone = require('backbone');
DatabaseTools = require('./database_tools');
inflection = require('inflection');
Connection = require('./lib/connection');
Queue = require('backbone-orm/lib/queue');
SQLUtils = require('./lib/utils');
DatabaseURL = require('backbone-orm/lib/database_url');
DESTROY_BATCH_LIMIT = 1000;
Schema = require('backbone-orm/lib/schema');
CAPABILITIES = {
self_reference: false,
embed: false
};
Utils = require('backbone-orm/lib/utils');
ModelCache = require('backbone-orm/lib/cache/singletons').ModelCache;
QueryCache = require('backbone-orm/lib/cache/singletons').QueryCache;
ModelTypeID = require('backbone-orm/lib/cache/singletons').ModelTypeID;
modelExtensions = require('backbone-orm/lib/extensions/model');
Connection = require('./connection');
SqlCursor = require('./cursor');
DatabaseTools = require('./database_tools');
DESTROY_BATCH_LIMIT = 1000;
SqlSync = (function() {

@@ -64,7 +49,23 @@ function SqlSync(model_type, options) {

this.model_type.model_name = Utils.findOrGenerateModelName(this.model_type);
this.model_type.model_id = ModelTypeID.generate(this.model_type);
this.schema = new Schema(this.model_type);
this.backbone_adapter = require('./backbone_adapter');
this.schema = new Schema(this.model_type, {
id: {
type: 'Integer'
}
});
this.backbone_adapter = require('./lib/backbone_adapter');
}
SqlSync.prototype.initialize = function() {
var url;
if (this.is_initialized) {
return;
}
this.is_initialized = true;
this.schema.initialize();
if (!(url = _.result(new this.model_type(), 'url'))) {
throw new Error("Missing url for model");
}
return this.connect(url);
};
SqlSync.prototype.read = function(model, options) {

@@ -107,11 +108,6 @@ if (model.models) {

if (!(res != null ? res.length : void 0)) {
return options.error(new Error("Failed to create model with attributes: " + (util.inspect(model.attributes))));
return options.error(new Error("Failed to create model with attributes: " + (JSONUtils.stringify(model.attributes))));
}
return QueryCache.reset(_this.model_type, function(err) {
if (err) {
return typeof options.error === "function" ? options.error(err) : void 0;
}
json.id = res[0];
return options.success(json);
});
json.id = res[0];
return options.success(json);
};

@@ -129,8 +125,3 @@ })(this));

}
return QueryCache.reset(_this.model_type, function(err) {
if (err) {
return typeof options.error === "function" ? options.error(err) : void 0;
}
return options.success(json);
});
return options.success(json);
};

@@ -146,8 +137,3 @@ })(this));

}
return QueryCache.reset(_this.model_type, function(err) {
if (err) {
return typeof options.error === "function" ? options.error(err) : void 0;
}
return options.success();
});
return options.success();
};

@@ -172,2 +158,6 @@ })(this));

SqlSync.prototype.destroy = function(query, callback) {
var _ref1;
if (arguments.length === 1) {
_ref1 = [{}, query], query = _ref1[0], callback = _ref1[1];
}
return this.model_type.each(_.extend({

@@ -184,8 +174,3 @@ $each: {

}
return _this.getTable('master').where('id', model_json.id).del().exec(function(err) {
if (err) {
return callback(err);
}
return QueryCache.reset(_this.model_type, callback);
});
return _this.getTable('master').where('id', model_json.id).del().exec(callback);
});

@@ -196,17 +181,4 @@ };

SqlSync.prototype.initialize = function() {
var url;
if (this.is_initialized) {
return;
}
this.is_initialized = true;
this.schema.initialize();
if (!(url = _.result(new this.model_type, 'url'))) {
throw new Error("Missing url for model");
}
return this.connect(url);
};
SqlSync.prototype.connect = function(url) {
var connection, slave_url, _i, _len, _ref, _ref1;
var slave_url, _i, _len, _ref1, _ref2;
this.table = (new DatabaseURL(url)).table;

@@ -218,7 +190,7 @@ this.connections || (this.connections = {

});
if ((_ref = this.slaves) != null ? _ref.length : void 0) {
_ref1 = this.slaves;
for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
slave_url = _ref1[_i];
this.connections.slaves.push(connection = new Connection("" + slave_url + "/" + this.table));
if ((_ref1 = this.slaves) != null ? _ref1.length : void 0) {
_ref2 = this.slaves;
for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
slave_url = _ref2[_i];
this.connections.slaves.push(new Connection("" + slave_url + "/" + this.table));
}

@@ -285,6 +257,12 @@ }

};
modelExtensions(type);
return ModelCache.configureSync(type, sync_fn);
Utils.configureModelType(type);
return BackboneORM.model_cache.configureSync(type, sync_fn);
};
module.exports.capabilities = function(url) {
return _.extend({
json: SQLUtils.protocolType(url) === 'postgres'
}, CAPABILITIES);
};
}).call(this);
{
"name": "backbone-sql",
"version": "0.5.8",
"version": "0.6.0",
"description": "PostgreSQL, MySQL, and SQLite3 storage for BackboneORM",

@@ -8,8 +8,29 @@ "main": "./lib/index.js",

"author": "Gwilym Humphreys (https://github.com/gwilymhumphreys)",
"contributors": ["Kevin Malakoff (https://github.com/kmalakoff)"],
"repository": {"type": "git", "url": "https://github.com/vidigami/backbone-sql.git"},
"keywords": ["backbone", "orm", "backbone-orm", "sql", "mysql", "postgres", "pg", "sqlite", "sqlite3"],
"engines": {"node": "*"},
"contributors": [
"Kevin Malakoff (https://github.com/kmalakoff)"
],
"repository": {
"type": "git",
"url": "https://github.com/vidigami/backbone-sql.git"
},
"keywords": [
"backbone",
"orm",
"backbone-orm",
"sql",
"mysql",
"postgres",
"pg",
"sqlite",
"sqlite3"
],
"engines": {
"node": "*"
},
"scripts": {
"test": "npm run build; NODE_ENV=test mocha test/suite.coffee --compilers coffee:coffee-script/register --reporter spec --timeout 10000",
"test": "npm run build; npm run test-postgres",
"test-quick": "npm run build; NODE_ENV=test mocha --require test/parameters_postgres 'node_modules/backbone-orm/test/spec/**/*.tests.coffee' --grep '@no_cache @no_embed'",
"test-postgres": "NODE_ENV=test mocha --require test/parameters_postgres --require backbone-rest/test/parameters_express4 '{node_modules/backbone-{orm,rest}/,}test/{issues,spec}/**/*.tests.coffee' --grep ''",
"test-mysql": "NODE_ENV=test mocha --require test/parameters_mysql --require backbone-rest/test/parameters_express4 '{node_modules/backbone-{orm,rest}/,}test/{issues,spec}/**/*.tests.coffee' --grep ''",
"test-sqlite3": "NODE_ENV=test mocha --require test/parameters_sqlite3 --require backbone-rest/test/parameters_express4 '{node_modules/backbone-{orm,rest}/,}test/{issues,spec}/**/*.tests.coffee' --grep ''",
"build": "coffee -o lib -c src",

@@ -19,3 +40,2 @@ "watch": "coffee -o lib -w src",

"prepublish": "npm run build",
"build_dep": "npm run-script backbone-orm build; npm run-script backbone-rest build",

@@ -25,21 +45,19 @@ "watch_dep": "npm run-script backbone-orm watch; npm run-script backbone-rest watch"

"devDependencies": {
"backbone-rest": "0.6.x",
"body-parser": "1.x",
"chai": "*",
"coffee-script": "*",
"codo": "1.7.x",
"mocha": "*",
"powerset": "0.0.x",
"backbone-rest": "0.5.x",
"supertest": "0.7.x",
"express": "3.2.x",
"pg": "2.11.x",
"mysql": "2.0.x",
"sqlite3": "2.1.x"
"express": "3.x",
"mocha": "^1.20.1",
"mysql": "2.x",
"pg": "3.x",
"sqlite3": "2.x",
"supertest": "*"
},
"dependencies": {
"underscore": "1.5.x",
"backbone": ">=1.0.0",
"backbone-orm": "0.5.x",
"moment": "2.x.x",
"inflection": "1.x.x",
"knex": "0.5.x"
"underscore": "1.6.x",
"backbone": "1.1.x",
"backbone-orm": "0.6.x",
"knex": "0.6.x"
}
}

@@ -0,1 +1,3 @@

[![Build Status](https://travis-ci.org/vidigami/backbone-sql.svg?branch=develop)](https://travis-ci.org/vidigami/backbone-sql)
![logo](https://github.com/vidigami/backbone-sql/raw/master/media/logo.png)

@@ -2,0 +4,0 @@

Please refer to the following release notes when upgrading your version of BackboneSQL.
### 0.6.0
* Upgraded to BackboneORM 0.6.x
### 0.5.10
* Simplified database_tools and made compatible with the latest knex.
### 0.5.9
* Update knex due to bluebird dependency breaking.
### 0.5.8

@@ -4,0 +13,0 @@ * Fix for $ne: null in find queries

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

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