backbone-sql
Advanced tools
Comparing version 0.5.4 to 0.5.5
@@ -1,9 +0,9 @@ | ||
// Generated by CoffeeScript 1.6.3 | ||
// Generated by CoffeeScript 1.7.1 | ||
/* | ||
backbone-sql.js 0.5.2 | ||
backbone-sql.js 0.5.5 | ||
Copyright (c) 2013 Vidigami - https://github.com/vidigami/backbone-sql | ||
License: MIT (http://www.opensource.org/licenses/mit-license.php) | ||
*/ | ||
*/ | ||
(function() { | ||
@@ -10,0 +10,0 @@ var SqlBackboneAdapter, inflection, util, _; |
@@ -1,9 +0,9 @@ | ||
// Generated by CoffeeScript 1.6.3 | ||
// Generated by CoffeeScript 1.7.1 | ||
/* | ||
backbone-sql.js 0.5.2 | ||
backbone-sql.js 0.5.5 | ||
Copyright (c) 2013 Vidigami - https://github.com/vidigami/backbone-sql | ||
License: MIT (http://www.opensource.org/licenses/mit-license.php) | ||
*/ | ||
*/ | ||
(function() { | ||
@@ -10,0 +10,0 @@ var Connection, ConnectionPool, DatabaseUrl, Knex, KnexConnection, PROTOCOLS, _; |
@@ -1,11 +0,11 @@ | ||
// Generated by CoffeeScript 1.6.3 | ||
// Generated by CoffeeScript 1.7.1 | ||
/* | ||
backbone-sql.js 0.5.2 | ||
backbone-sql.js 0.5.5 | ||
Copyright (c) 2013 Vidigami - https://github.com/vidigami/backbone-sql | ||
License: MIT (http://www.opensource.org/licenses/mit-license.php) | ||
*/ | ||
*/ | ||
(function() { | ||
var COMPARATORS, COMPARATOR_KEYS, Cursor, Knex, SqlCursor, util, _, _appendCondition, _appendConditionalWhere, _appendSort, _appendWhere, _columnName, _ref, | ||
var COMPARATORS, COMPARATOR_KEYS, Cursor, Knex, SqlCursor, util, _, _appendCondition, _appendConditionalWhere, _appendSort, _appendWhere, _columnName, _extractCount, | ||
__hasProp = {}.hasOwnProperty, | ||
@@ -165,2 +165,11 @@ __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; }, | ||
_extractCount = function(count_json) { | ||
var count_info; | ||
if (!count_json.length) { | ||
return 0; | ||
} | ||
count_info = count_json[0]; | ||
return +count_info[count_info.hasOwnProperty('count(*)') ? 'count(*)' : 'count']; | ||
}; | ||
module.exports = SqlCursor = (function(_super) { | ||
@@ -170,8 +179,7 @@ __extends(SqlCursor, _super); | ||
function SqlCursor() { | ||
_ref = SqlCursor.__super__.constructor.apply(this, arguments); | ||
return _ref; | ||
return SqlCursor.__super__.constructor.apply(this, arguments); | ||
} | ||
SqlCursor.prototype._parseConditions = function(find, cursor) { | ||
var conditions, key, related_conditions, related_wheres, relation, reverse_relation, value, _base, _name, _ref1; | ||
var conditions, key, related_conditions, related_wheres, relation, reverse_relation, value, _base, _name, _ref; | ||
conditions = { | ||
@@ -192,3 +200,3 @@ wheres: [], | ||
if (key.indexOf('.') > 0) { | ||
_ref1 = key.split('.'), relation = _ref1[0], key = _ref1[1]; | ||
_ref = key.split('.'), relation = _ref[0], key = _ref[1]; | ||
related_wheres[relation] || (related_wheres[relation] = {}); | ||
@@ -227,4 +235,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, _ref1, | ||
_this = this; | ||
var $columns, $fields, err, from_columns, key, query, related_model_type, related_wheres, relation, to_columns, _i, _len, _ref; | ||
if (this.hasCursorQuery('$zero')) { | ||
@@ -248,9 +255,13 @@ return callback(null, this.hasCursorQuery('$one') ? null : []); | ||
if (this.hasCursorQuery('$count')) { | ||
return query.count('*').exec(function(err, json) { | ||
return callback(null, json.length ? +json[0].aggregate : 0); | ||
}); | ||
return query.count('*').exec((function(_this) { | ||
return function(err, count_json) { | ||
return callback(null, _extractCount(count_json)); | ||
}; | ||
})(this)); | ||
} else { | ||
return query.count(1).exec(function(err, json) { | ||
return callback(null, json.length ? !!+json[0].aggregate : false); | ||
}); | ||
return query.count('*').limit(1).exec((function(_this) { | ||
return function(err, count_json) { | ||
return callback(null, _extractCount(count_json) > 0); | ||
}; | ||
})(this)); | ||
} | ||
@@ -272,5 +283,5 @@ } | ||
to_columns = []; | ||
_ref1 = this.include_keys; | ||
for (_i = 0, _len = _ref1.length; _i < _len; _i++) { | ||
key = _ref1[_i]; | ||
_ref = this.include_keys; | ||
for (_i = 0, _len = _ref.length; _i < _len; _i++) { | ||
key = _ref[_i]; | ||
relation = this._getRelation(key); | ||
@@ -310,20 +321,21 @@ related_model_type = relation.reverse_relation.model_type; | ||
} | ||
return query.exec(function(err, json) { | ||
if (err) { | ||
return callback(new Error("Query failed for model: " + _this.model_type.model_name + " with error: " + err)); | ||
} | ||
if (_this.joined) { | ||
json = _this._joinedResultsToJSON(json); | ||
} | ||
if (_this.queued_queries) { | ||
return _this._appendCompleteRelations(json, callback); | ||
} else { | ||
return _this._processResponse(json, callback); | ||
} | ||
}); | ||
return query.exec((function(_this) { | ||
return function(err, json) { | ||
if (err) { | ||
return callback(new Error("Query failed for model: " + _this.model_type.model_name + " with error: " + err)); | ||
} | ||
if (_this.joined) { | ||
json = _this._joinedResultsToJSON(json); | ||
} | ||
if (_this.queued_queries) { | ||
return _this._appendCompleteRelations(json, callback); | ||
} else { | ||
return _this._processResponse(json, callback); | ||
} | ||
}; | ||
})(this)); | ||
}; | ||
SqlCursor.prototype._processResponse = function(json, callback) { | ||
var model_json, number, query, schema, _i, _len, | ||
_this = this; | ||
var model_json, number, query, schema, _i, _len; | ||
schema = this.model_type.schema(); | ||
@@ -352,9 +364,14 @@ for (_i = 0, _len = json.length; _i < _len; _i++) { | ||
this._appendJoinedWheres(query); | ||
return query.count('*').exec(function(err, count_json) { | ||
return callback(null, { | ||
offset: _this._cursor.$offset || 0, | ||
total_rows: count_json.length ? +count_json[0].aggregate : 0, | ||
rows: json | ||
}); | ||
}); | ||
return query.count('*').exec((function(_this) { | ||
return function(err, count_json) { | ||
if (err) { | ||
return callback(err); | ||
} | ||
return callback(null, { | ||
offset: _this._cursor.$offset || 0, | ||
total_rows: _extractCount(count_json), | ||
rows: json | ||
}); | ||
}; | ||
})(this)); | ||
} else { | ||
@@ -366,10 +383,9 @@ return callback(null, json); | ||
SqlCursor.prototype._appendCompleteRelations = function(json, callback) { | ||
var key, new_query, related_model_type, relation, to_columns, _i, _len, _ref1, | ||
_this = this; | ||
var key, new_query, related_model_type, relation, to_columns, _i, _len, _ref; | ||
new_query = this.connection(this.model_type.tableName()); | ||
new_query.whereIn(_columnName('id', this.model_type.tableName()), _.pluck(json, 'id')); | ||
to_columns = []; | ||
_ref1 = this.queued_queries; | ||
for (_i = 0, _len = _ref1.length; _i < _len; _i++) { | ||
key = _ref1[_i]; | ||
_ref = this.queued_queries; | ||
for (_i = 0, _len = _ref.length; _i < _len; _i++) { | ||
key = _ref[_i]; | ||
relation = this._getRelation(key); | ||
@@ -381,18 +397,20 @@ related_model_type = relation.reverse_relation.model_type; | ||
new_query.select((this._prefixColumns(this.model_type, ['id'])).concat(to_columns)); | ||
return new_query.exec(function(err, new_json) { | ||
var model, placeholder, relation_json, _j, _len1; | ||
relation_json = _this._joinedResultsToJSON(new_json); | ||
for (_j = 0, _len1 = relation_json.length; _j < _len1; _j++) { | ||
placeholder = relation_json[_j]; | ||
model = _.find(json, function(test) { | ||
return test.id === placeholder.id; | ||
}); | ||
_.extend(model, placeholder); | ||
} | ||
return _this._processResponse(json, callback); | ||
}); | ||
return new_query.exec((function(_this) { | ||
return function(err, new_json) { | ||
var model, placeholder, relation_json, _j, _len1; | ||
relation_json = _this._joinedResultsToJSON(new_json); | ||
for (_j = 0, _len1 = relation_json.length; _j < _len1; _j++) { | ||
placeholder = relation_json[_j]; | ||
model = _.find(json, function(test) { | ||
return test.id === placeholder.id; | ||
}); | ||
_.extend(model, placeholder); | ||
} | ||
return _this._processResponse(json, callback); | ||
}; | ||
})(this)); | ||
}; | ||
SqlCursor.prototype._appendRelatedWheres = function(query) { | ||
var key, related_wheres, relation, _ref1, _results; | ||
var key, related_wheres, relation, _ref, _results; | ||
if (_.isEmpty(this._conditions.related_wheres)) { | ||
@@ -405,6 +423,6 @@ return; | ||
} | ||
_ref1 = this._conditions.related_wheres; | ||
_ref = this._conditions.related_wheres; | ||
_results = []; | ||
for (key in _ref1) { | ||
related_wheres = _ref1[key]; | ||
for (key in _ref) { | ||
related_wheres = _ref[key]; | ||
relation = this._getRelation(key); | ||
@@ -418,3 +436,3 @@ this._joinTo(query, relation); | ||
SqlCursor.prototype._appendJoinedWheres = function(query) { | ||
var from_key, joined_wheres, key, relation, to_key, _ref1, _results; | ||
var from_key, joined_wheres, key, relation, to_key, _ref, _results; | ||
if (_.isEmpty(this._conditions.joined_wheres)) { | ||
@@ -424,6 +442,6 @@ return; | ||
this.joined = true; | ||
_ref1 = this._conditions.joined_wheres; | ||
_ref = this._conditions.joined_wheres; | ||
_results = []; | ||
for (key in _ref1) { | ||
joined_wheres = _ref1[key]; | ||
for (key in _ref) { | ||
joined_wheres = _ref[key]; | ||
relation = this._getRelation(key); | ||
@@ -464,3 +482,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, _ref1; | ||
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; | ||
if (!(raw_json && raw_json.length)) { | ||
@@ -479,5 +497,5 @@ return raw_json; | ||
} else if (this.include_keys) { | ||
_ref1 = this.include_keys; | ||
for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) { | ||
include_key = _ref1[_j]; | ||
_ref = this.include_keys; | ||
for (_j = 0, _len1 = _ref.length; _j < _len1; _j++) { | ||
include_key = _ref[_j]; | ||
related_json = (row_relation_json[include_key] || (row_relation_json[include_key] = {})); | ||
@@ -484,0 +502,0 @@ related_model_type = this.model_type.relation(include_key).reverse_relation.model_type; |
@@ -1,9 +0,9 @@ | ||
// Generated by CoffeeScript 1.6.3 | ||
// Generated by CoffeeScript 1.7.1 | ||
/* | ||
backbone-sql.js 0.5.2 | ||
backbone-sql.js 0.5.5 | ||
Copyright (c) 2013 Vidigami - https://github.com/vidigami/backbone-sql | ||
License: MIT (http://www.opensource.org/licenses/mit-license.php) | ||
*/ | ||
*/ | ||
(function() { | ||
@@ -65,3 +65,2 @@ var DatabaseTools, KNEX_COLUMN_OPERATORS, KNEX_COLUMN_OPTIONS, Knex, Queue, inflection, _, | ||
DatabaseTools.prototype.end = function(callback) { | ||
var _this = this; | ||
if (!this.promise) { | ||
@@ -73,32 +72,33 @@ if (this.strict) { | ||
} | ||
return this.promise.exec(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); | ||
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); | ||
}); | ||
}; | ||
for (_i = 0, _len = _ref.length; _i < _len; _i++) { | ||
join_table_fn = _ref[_i]; | ||
_fn(join_table_fn); | ||
} else { | ||
return callback(); | ||
} | ||
return queue.await(function(err) { | ||
_this.join_table_operations = []; | ||
return callback(err); | ||
}); | ||
} else { | ||
return callback(); | ||
} | ||
}); | ||
}; | ||
})(this)); | ||
}; | ||
DatabaseTools.prototype.createTable = function() { | ||
var _this = this; | ||
if (this.promise && this.table) { | ||
@@ -110,5 +110,7 @@ if (this.strict) { | ||
} | ||
this.promise = this.connection.knex().schema.createTable(this.table_name, function(t) { | ||
return _this.table = t; | ||
}); | ||
this.promise = this.connection.knex().schema.createTable(this.table_name, (function(_this) { | ||
return function(t) { | ||
return _this.table = t; | ||
}; | ||
})(this)); | ||
return this; | ||
@@ -118,3 +120,2 @@ }; | ||
DatabaseTools.prototype.editTable = function() { | ||
var _this = this; | ||
if (this.promise && this.table) { | ||
@@ -126,5 +127,7 @@ if (this.strict) { | ||
} | ||
this.promise = this.connection.knex().schema.table(this.table_name, function(t) { | ||
return _this.table = t; | ||
}); | ||
this.promise = this.connection.knex().schema.table(this.table_name, (function(_this) { | ||
return function(t) { | ||
return _this.table = t; | ||
}; | ||
})(this)); | ||
return this; | ||
@@ -218,3 +221,2 @@ }; | ||
DatabaseTools.prototype.resetSchema = function(options, callback) { | ||
var _this = this; | ||
if (arguments.length === 1) { | ||
@@ -224,28 +226,29 @@ callback = options; | ||
} | ||
return this.connection.knex().schema.dropTableIfExists(this.table_name).exec(function(err) { | ||
var field, key, relation, _ref, _ref1; | ||
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.connection.knex().schema.dropTableIfExists(this.table_name).exec((function(_this) { | ||
return function(err) { | ||
var field, key, relation, _ref, _ref1; | ||
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); | ||
}; | ||
})(this)); | ||
}; | ||
DatabaseTools.prototype.ensureSchema = function(options, callback) { | ||
var _this = this; | ||
if (arguments.length === 1) { | ||
@@ -259,18 +262,25 @@ callback = options; | ||
this.ensuring = true; | ||
return this.hasTable(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.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) { | ||
@@ -280,27 +290,25 @@ _this.ensuring = false; | ||
}); | ||
}); | ||
} 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 = this; | ||
var field, key, queue, relation, _fn, _fn1, _ref, _ref1; | ||
this.editTable(); | ||
queue = new Queue(1); | ||
queue.defer(function(callback) { | ||
return _this.ensureColumn('id', 'increments', ['primary'], callback); | ||
}); | ||
queue.defer((function(_this) { | ||
return function(callback) { | ||
return _this.ensureColumn('id', 'increments', ['primary'], callback); | ||
}; | ||
})(this)); | ||
if (this.schema.fields) { | ||
_ref = this.schema.fields; | ||
_fn = function(key, field) { | ||
return queue.defer(function(callback) { | ||
return _this.ensureField(key, field, callback); | ||
}); | ||
}; | ||
_fn = (function(_this) { | ||
return function(key, field) { | ||
return queue.defer(function(callback) { | ||
return _this.ensureField(key, field, callback); | ||
}); | ||
}; | ||
})(this); | ||
for (key in _ref) { | ||
@@ -313,7 +321,9 @@ field = _ref[key]; | ||
_ref1 = this.schema.relations; | ||
_fn1 = function(key, relation) { | ||
return queue.defer(function(callback) { | ||
return _this.ensureRelation(key, relation, callback); | ||
}); | ||
}; | ||
_fn1 = (function(_this) { | ||
return function(key, relation) { | ||
return queue.defer(function(callback) { | ||
return _this.ensureRelation(key, relation, callback); | ||
}); | ||
}; | ||
})(this); | ||
for (key in _ref1) { | ||
@@ -324,22 +334,25 @@ relation = _ref1[key]; | ||
} | ||
return queue.await(function(err) { | ||
if (err) { | ||
return callback(err); | ||
} | ||
return _this.end(callback); | ||
}); | ||
return queue.await((function(_this) { | ||
return function(err) { | ||
if (err) { | ||
return callback(err); | ||
} | ||
return _this.end(callback); | ||
}; | ||
})(this)); | ||
}; | ||
DatabaseTools.prototype.ensureRelation = function(key, relation, callback) { | ||
var _this = this; | ||
if (relation.type === 'belongsTo') { | ||
return this.hasColumn(relation.foreign_key, function(err, column_exists) { | ||
if (err) { | ||
return callback(err); | ||
} | ||
if (!column_exists) { | ||
_this.addRelation(key, relation); | ||
} | ||
return callback(); | ||
}); | ||
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') { | ||
@@ -353,28 +366,30 @@ return relation.findOrGenerateJoinTable().db().ensureSchema(callback); | ||
DatabaseTools.prototype.ensureField = function(key, field, callback) { | ||
var _this = this; | ||
return this.hasColumn(key, function(err, column_exists) { | ||
if (err) { | ||
return callback(err); | ||
} | ||
if (!column_exists) { | ||
_this.addField(key, field); | ||
} | ||
return callback(); | ||
}); | ||
return this.hasColumn(key, (function(_this) { | ||
return function(err, column_exists) { | ||
if (err) { | ||
return callback(err); | ||
} | ||
if (!column_exists) { | ||
_this.addField(key, field); | ||
} | ||
return callback(); | ||
}; | ||
})(this)); | ||
}; | ||
DatabaseTools.prototype.ensureColumn = function(key, type, options, callback) { | ||
var _this = this; | ||
if (!this.table) { | ||
this.editTable(); | ||
} | ||
return this.hasColumn(key, function(err, column_exists) { | ||
if (err) { | ||
return callback(err); | ||
} | ||
if (!column_exists) { | ||
_this.addColumn(key, type, options); | ||
} | ||
return callback(); | ||
}); | ||
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)); | ||
}; | ||
@@ -381,0 +396,0 @@ |
@@ -1,9 +0,9 @@ | ||
// Generated by CoffeeScript 1.6.3 | ||
// Generated by CoffeeScript 1.7.1 | ||
/* | ||
backbone-sql.js 0.5.2 | ||
backbone-sql.js 0.5.5 | ||
Copyright (c) 2013 Vidigami - https://github.com/vidigami/backbone-sql | ||
License: MIT (http://www.opensource.org/licenses/mit-license.php) | ||
*/ | ||
*/ | ||
(function() { | ||
@@ -10,0 +10,0 @@ module.exports = { |
145
lib/sync.js
@@ -1,9 +0,9 @@ | ||
// Generated by CoffeeScript 1.6.3 | ||
// Generated by CoffeeScript 1.7.1 | ||
/* | ||
backbone-sql.js 0.5.2 | ||
backbone-sql.js 0.5.5 | ||
Copyright (c) 2013 Vidigami - https://github.com/vidigami/backbone-sql | ||
License: MIT (http://www.opensource.org/licenses/mit-license.php) | ||
*/ | ||
*/ | ||
(function() { | ||
@@ -69,23 +69,26 @@ var Backbone, Connection, DESTROY_BATCH_LIMIT, DatabaseTools, DatabaseURL, ModelCache, ModelTypeID, QueryCache, Queue, Schema, SqlCursor, SqlSync, Utils, inflection, modelExtensions, util, _, | ||
SqlSync.prototype.read = function(model, options) { | ||
var _this = this; | ||
if (model.models) { | ||
return this.cursor().toJSON(function(err, json) { | ||
if (err) { | ||
return options.error(err); | ||
} | ||
if (!json) { | ||
return options.error(new Error('Collection not fetched')); | ||
} | ||
return typeof options.success === "function" ? options.success(json) : void 0; | ||
}); | ||
return this.cursor().toJSON((function(_this) { | ||
return function(err, json) { | ||
if (err) { | ||
return options.error(err); | ||
} | ||
if (!json) { | ||
return options.error(new Error('Collection not fetched')); | ||
} | ||
return typeof options.success === "function" ? options.success(json) : void 0; | ||
}; | ||
})(this)); | ||
} else { | ||
return this.cursor(model.id).toJSON(function(err, json) { | ||
if (err) { | ||
return options.error(err); | ||
} | ||
if (!json) { | ||
return options.error(new Error("Model not found. Id " + model.id)); | ||
} | ||
return options.success(json); | ||
}); | ||
return this.cursor(model.id).toJSON((function(_this) { | ||
return function(err, json) { | ||
if (err) { | ||
return options.error(err); | ||
} | ||
if (!json) { | ||
return options.error(new Error("Model not found. Id " + model.id)); | ||
} | ||
return options.success(json); | ||
}; | ||
})(this)); | ||
} | ||
@@ -95,52 +98,55 @@ }; | ||
SqlSync.prototype.create = function(model, options) { | ||
var json, | ||
_this = this; | ||
var json; | ||
json = model.toJSON(); | ||
return this.getTable('master').insert(json, 'id').exec(function(err, res) { | ||
if (err) { | ||
return options.error(err); | ||
} | ||
if (!(res != null ? res.length : void 0)) { | ||
return options.error(new Error("Failed to create model with attributes: " + (util.inspect(model.attributes)))); | ||
} | ||
return QueryCache.reset(_this.model_type, function(err) { | ||
return this.getTable('master').insert(json, 'id').exec((function(_this) { | ||
return function(err, res) { | ||
if (err) { | ||
return typeof options.error === "function" ? options.error(err) : void 0; | ||
return options.error(err); | ||
} | ||
json.id = res[0]; | ||
return options.success(json); | ||
}); | ||
}); | ||
if (!(res != null ? res.length : void 0)) { | ||
return options.error(new Error("Failed to create model with attributes: " + (util.inspect(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); | ||
}); | ||
}; | ||
})(this)); | ||
}; | ||
SqlSync.prototype.update = function(model, options) { | ||
var json, | ||
_this = this; | ||
var json; | ||
json = model.toJSON(); | ||
return this.getTable('master').where('id', model.id).update(json).exec(function(err, res) { | ||
if (err) { | ||
return options.error(err); | ||
} | ||
return QueryCache.reset(_this.model_type, function(err) { | ||
return this.getTable('master').where('id', model.id).update(json).exec((function(_this) { | ||
return function(err, res) { | ||
if (err) { | ||
return typeof options.error === "function" ? options.error(err) : void 0; | ||
return options.error(err); | ||
} | ||
return options.success(json); | ||
}); | ||
}); | ||
return QueryCache.reset(_this.model_type, function(err) { | ||
if (err) { | ||
return typeof options.error === "function" ? options.error(err) : void 0; | ||
} | ||
return options.success(json); | ||
}); | ||
}; | ||
})(this)); | ||
}; | ||
SqlSync.prototype["delete"] = function(model, options) { | ||
var _this = this; | ||
return this.getTable('master').where('id', model.id).del().exec(function(err, res) { | ||
if (err) { | ||
return options.error(err); | ||
} | ||
return QueryCache.reset(_this.model_type, function(err) { | ||
return this.getTable('master').where('id', model.id).del().exec((function(_this) { | ||
return function(err, res) { | ||
if (err) { | ||
return typeof options.error === "function" ? options.error(err) : void 0; | ||
return options.error(err); | ||
} | ||
return options.success(); | ||
}); | ||
}); | ||
return QueryCache.reset(_this.model_type, function(err) { | ||
if (err) { | ||
return typeof options.error === "function" ? options.error(err) : void 0; | ||
} | ||
return options.success(); | ||
}); | ||
}; | ||
})(this)); | ||
}; | ||
@@ -163,3 +169,2 @@ | ||
SqlSync.prototype.destroy = function(query, callback) { | ||
var _this = this; | ||
return this.model_type.each(_.extend({ | ||
@@ -170,15 +175,17 @@ $each: { | ||
} | ||
}, query), (function(model_json, callback) { | ||
return Utils.patchRemoveByJSON(_this.model_type, model_json, function(err) { | ||
if (err) { | ||
return callback(err); | ||
} | ||
return _this.getTable('master').where('id', model_json.id).del().exec(function(err) { | ||
}, query), ((function(_this) { | ||
return function(model_json, callback) { | ||
return Utils.patchRemoveByJSON(_this.model_type, model_json, 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(function(err) { | ||
if (err) { | ||
return callback(err); | ||
} | ||
return QueryCache.reset(_this.model_type, callback); | ||
}); | ||
}); | ||
}); | ||
}), callback); | ||
}; | ||
})(this)), callback); | ||
}; | ||
@@ -185,0 +192,0 @@ |
{ | ||
"name": "backbone-sql", | ||
"version": "0.5.4", | ||
"version": "0.5.5", | ||
"description": "PostgreSQL, MySQL, and SQLite3 storage for BackboneORM", | ||
@@ -13,3 +13,3 @@ "main": "./lib/index.js", | ||
"scripts": { | ||
"test": "npm run build; NODE_ENV=test mocha test/suite.coffee --compilers coffee:coffee-script --reporter spec --timeout 10000", | ||
"test": "npm run build; NODE_ENV=test mocha test/suite.coffee --compilers coffee:coffee-script/register --reporter spec --timeout 10000", | ||
"build": "coffee -o lib -c src", | ||
@@ -31,3 +31,3 @@ "watch": "coffee -o lib -w src", | ||
"express": "3.2.x", | ||
"pg": "2.3.x", | ||
"pg": "2.11.x", | ||
"mysql": "2.0.x", | ||
@@ -40,6 +40,6 @@ "sqlite3": "2.1.x" | ||
"backbone-orm": "0.5.x", | ||
"moment": "2.4.x", | ||
"inflection": "1.2.x", | ||
"knex": "0.4.x" | ||
"moment": "2.x.x", | ||
"inflection": "1.x.x", | ||
"knex": "0.5.x" | ||
} | ||
} |
Please refer to the following release notes when upgrading your version of BackboneSQL. | ||
### 0.5.5 | ||
* Updated to latest Knex (still outstanding problems with consistent Date support in Knex - not all mysql sqlite tests passing for dates) | ||
### 0.5.4 | ||
* $nin bug fix | ||
### 0.5.3 | ||
* $nin support | ||
### 0.5.2 | ||
@@ -4,0 +13,0 @@ * Handle null hasMany relations in _joinedResultsToJSON |
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
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
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
121223
1257
26
+ Addedbluebird@1.2.4(transitive)
+ Addedcolors@0.6.2(transitive)
+ Addedinflection@1.13.4(transitive)
+ Addedknex@0.5.16(transitive)
+ Addedlodash@4.17.21(transitive)
+ Addedminimist@0.0.10(transitive)
+ Addedmkdirp@0.3.5(transitive)
+ Addedmoment@2.30.1(transitive)
+ Addedoptimist@0.6.1(transitive)
+ Addedwordwrap@0.0.3(transitive)
- Removedinflection@1.2.7(transitive)
- Removedknex@0.4.13(transitive)
- Removedmoment@2.4.0(transitive)
- Removedwhen@2.4.0(transitive)
Updatedinflection@1.x.x
Updatedknex@0.5.x
Updatedmoment@2.x.x