backbone-sql
Advanced tools
Comparing version 0.6.0 to 0.6.2
// Generated by CoffeeScript 1.7.1 | ||
/* | ||
backbone-sql.js 0.6.0 | ||
backbone-sql.js 0.6.2 | ||
Copyright (c) 2013 Vidigami - https://github.com/vidigami/backbone-sql | ||
@@ -10,3 +10,4 @@ License: MIT (http://www.opensource.org/licenses/mit-license.php) | ||
(function() { | ||
var COMPARATORS, COMPARATOR_KEYS, Knex, SqlCursor, sync, _, _appendCondition, _appendConditionalWhere, _appendSort, _appendWhere, _columnName, _extractCount, _ref, | ||
var COMPARATORS, COMPARATOR_KEYS, Knex, SqlCursor, sync, _, _appendCondition, _appendConditionalWhere, _appendWhere, _columnName, _extractCount, _ref, | ||
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }, | ||
__hasProp = {}.hasOwnProperty, | ||
@@ -34,3 +35,4 @@ __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; }, | ||
if ((_ref1 = value.$in) != null ? _ref1.length : void 0) { | ||
conditions.where_ins.push({ | ||
conditions.wheres.push({ | ||
method: 'whereIn', | ||
key: key, | ||
@@ -45,3 +47,4 @@ value: value.$in | ||
if ((_ref2 = value.$nin) != null ? _ref2.length : void 0) { | ||
conditions.where_nins.push({ | ||
conditions.wheres.push({ | ||
method: 'whereNotIn', | ||
key: key, | ||
@@ -107,7 +110,9 @@ value: value.$nin | ||
_appendWhere = function(query, conditions, table) { | ||
var condition, _i, _j, _k, _l, _len, _len1, _len2, _len3, _ref1, _ref2, _ref3, _ref4; | ||
var condition, _i, _j, _len, _len1, _ref1, _ref2; | ||
_ref1 = conditions.wheres; | ||
for (_i = 0, _len = _ref1.length; _i < _len; _i++) { | ||
condition = _ref1[_i]; | ||
if (_.isNull(condition.value)) { | ||
if (condition.method) { | ||
query[condition.method](_columnName(condition.key, table), condition.value); | ||
} else if (_.isNull(condition.value)) { | ||
query.whereNull(_columnName(condition.key, table)); | ||
@@ -141,32 +146,5 @@ } else { | ||
} | ||
_ref3 = conditions.where_ins; | ||
for (_k = 0, _len2 = _ref3.length; _k < _len2; _k++) { | ||
condition = _ref3[_k]; | ||
query.whereIn(_columnName(condition.key, table), condition.value); | ||
} | ||
_ref4 = conditions.where_nins; | ||
for (_l = 0, _len3 = _ref4.length; _l < _len3; _l++) { | ||
condition = _ref4[_l]; | ||
query.whereNotIn(_columnName(condition.key, table), condition.value); | ||
} | ||
return query; | ||
}; | ||
_appendSort = function(query, sorts) { | ||
var col, dir, sort, _i, _len; | ||
sorts = _.isArray(sorts) ? sorts : [sorts]; | ||
for (_i = 0, _len = sorts.length; _i < _len; _i++) { | ||
sort = sorts[_i]; | ||
if (sort[0] === '-') { | ||
dir = 'desc'; | ||
col = sort.substr(1); | ||
} else { | ||
dir = 'asc'; | ||
col = sort; | ||
} | ||
query.orderBy(col, dir); | ||
} | ||
return query; | ||
}; | ||
_extractCount = function(count_json) { | ||
@@ -185,5 +163,8 @@ var count_info; | ||
function SqlCursor() { | ||
this._exec = __bind(this._exec, this); | ||
return SqlCursor.__super__.constructor.apply(this, arguments); | ||
} | ||
SqlCursor.prototype.verbose = false; | ||
SqlCursor.prototype._parseConditions = function(find, cursor) { | ||
@@ -194,4 +175,2 @@ var conditions, key, related_conditions, related_wheres, relation, reverse_relation, value, _base, _name, _ref1; | ||
where_conditionals: [], | ||
where_ins: [], | ||
where_nins: [], | ||
related_wheres: {}, | ||
@@ -214,5 +193,3 @@ joined_wheres: {} | ||
wheres: [], | ||
where_conditionals: [], | ||
where_ins: [], | ||
where_nins: [] | ||
where_conditionals: [] | ||
}); | ||
@@ -233,3 +210,4 @@ _appendCondition(conditions.joined_wheres[relation.key], key, value); | ||
} | ||
conditions.where_ins.push({ | ||
conditions.wheres.push({ | ||
method: 'whereIn', | ||
key: 'id', | ||
@@ -243,3 +221,3 @@ value: cursor.$ids | ||
SqlCursor.prototype.queryToJSON = function(callback) { | ||
var $columns, $fields, err, from_columns, key, query, related_model_type, related_wheres, relation, to_columns, _i, _len, _ref1; | ||
var $columns, $fields, err, from_columns, key, query, rank_field, related_model_type, related_wheres, relation, sort_dir, sort_field, subquery, to_columns, _base, _i, _len, _ref1, _ref2, _ref3; | ||
if (this.hasCursorQuery('$zero')) { | ||
@@ -259,2 +237,36 @@ return callback(null, this.hasCursorQuery('$one') ? null : []); | ||
} | ||
if (this._cursor.$sort) { | ||
this._cursor.$sort = _.isArray(this._cursor.$sort) ? this._cursor.$sort : [this._cursor.$sort]; | ||
} | ||
if (this._cursor.$values) { | ||
$fields = this._cursor.$white_list ? _.intersection(this._cursor.$values, this._cursor.$white_list) : this._cursor.$values; | ||
} else if (this._cursor.$select) { | ||
$fields = this._cursor.$white_list ? _.intersection(this._cursor.$select, this._cursor.$white_list) : this._cursor.$select; | ||
} else if (this._cursor.$white_list) { | ||
$fields = this._cursor.$white_list; | ||
} | ||
if (this._cursor.$unique) { | ||
$fields || ($fields = this._columns(this.model_type, $fields)); | ||
if (this.hasCursorQuery('$count')) { | ||
query.count().from(this.connection.distinct(this._cursor.$unique).from(this.model_type.tableName()).as('count_query')); | ||
return query.exec((function(_this) { | ||
return function(err, count_json) { | ||
return callback(err, _extractCount(count_json)); | ||
}; | ||
})(this)); | ||
} | ||
if (_.difference($fields, this._cursor.$unique).length === 0) { | ||
query.distinct($fields); | ||
} else { | ||
(_base = this._cursor).$sort || (_base.$sort = []); | ||
rank_field = this._cursor.$unique[0]; | ||
_ref2 = this._parseSortField(((_ref1 = this._cursor.$sort) != null ? _ref1[0] : void 0) || 'id'), sort_field = _ref2[0], sort_dir = _ref2[1]; | ||
subquery = this.connection.select(this.connection.raw("" + ($fields.join(', ')) + ", rank() over (partition by " + rank_field + " order by " + sort_field + " " + sort_dir + ")")); | ||
subquery.from(this.model_type.tableName()).as('subquery'); | ||
query.select($fields).from(subquery).where('rank', 1); | ||
} | ||
this._appendSort(query); | ||
this._appendLimits(query); | ||
return this._exec(query, callback); | ||
} | ||
if (this.hasCursorQuery('$count') || this.hasCursorQuery('$exists')) { | ||
@@ -277,9 +289,2 @@ this._appendRelatedWheres(query); | ||
} | ||
if (this._cursor.$values) { | ||
$fields = this._cursor.$white_list ? _.intersection(this._cursor.$values, this._cursor.$white_list) : this._cursor.$values; | ||
} else if (this._cursor.$select) { | ||
$fields = this._cursor.$white_list ? _.intersection(this._cursor.$select, this._cursor.$white_list) : this._cursor.$select; | ||
} else if (this._cursor.$white_list) { | ||
$fields = this._cursor.$white_list; | ||
} | ||
if (this._cursor.$include) { | ||
@@ -292,5 +297,5 @@ this.include_keys = _.isArray(this._cursor.$include) ? this._cursor.$include : [this._cursor.$include]; | ||
to_columns = []; | ||
_ref1 = this.include_keys; | ||
for (_i = 0, _len = _ref1.length; _i < _len; _i++) { | ||
key = _ref1[_i]; | ||
_ref3 = this.include_keys; | ||
for (_i = 0, _len = _ref3.length; _i < _len; _i++) { | ||
key = _ref3[_i]; | ||
relation = this._getRelation(key); | ||
@@ -308,19 +313,13 @@ related_model_type = relation.reverse_relation.model_type; | ||
} else { | ||
if (this._cursor.$one) { | ||
query.limit(1); | ||
} | ||
if (this._cursor.$limit) { | ||
query.limit(this._cursor.$limit); | ||
} | ||
if (this._cursor.$offset) { | ||
query.offset(this._cursor.$offset); | ||
} | ||
this._appendLimits(query); | ||
} | ||
this._appendRelatedWheres(query); | ||
this._appendJoinedWheres(query); | ||
$columns || ($columns = this.joined ? this._prefixColumns(this.model_type, $fields) : $fields); | ||
$columns || ($columns = this.joined ? this._prefixColumns(this.model_type, $fields) : this._columns(this.model_type, $fields)); | ||
query.select($columns); | ||
if (this._cursor.$sort) { | ||
_appendSort(query, this._cursor.$sort); | ||
} | ||
this._appendSort(query); | ||
return this._exec(query, callback); | ||
}; | ||
SqlCursor.prototype._exec = function(query, callback) { | ||
if (this.verbose) { | ||
@@ -390,2 +389,41 @@ console.log('\n----------'); | ||
SqlCursor.prototype._appendLimits = function(query) { | ||
if (this._cursor.$one) { | ||
query.limit(1); | ||
} | ||
if (this._cursor.$limit) { | ||
query.limit(this._cursor.$limit); | ||
} | ||
if (this._cursor.$offset) { | ||
query.offset(this._cursor.$offset); | ||
} | ||
return query; | ||
}; | ||
SqlCursor.prototype._parseSortField = function(sort) { | ||
var col, dir; | ||
if (sort[0] === '-') { | ||
dir = 'desc'; | ||
col = sort.substr(1); | ||
} else { | ||
dir = 'asc'; | ||
col = sort; | ||
} | ||
return [col, dir]; | ||
}; | ||
SqlCursor.prototype._appendSort = function(query) { | ||
var col, dir, sort, _i, _len, _ref1, _ref2; | ||
if (!this._cursor.$sort) { | ||
return query; | ||
} | ||
_ref1 = this._cursor.$sort; | ||
for (_i = 0, _len = _ref1.length; _i < _len; _i++) { | ||
sort = _ref1[_i]; | ||
_ref2 = this._parseSortField(sort), col = _ref2[0], dir = _ref2[1]; | ||
query.orderBy(col, dir); | ||
} | ||
return query; | ||
}; | ||
SqlCursor.prototype._appendCompleteRelations = function(json, callback) { | ||
@@ -545,2 +583,11 @@ var key, new_query, related_model_type, relation, to_columns, _i, _len, _ref1; | ||
SqlCursor.prototype._columns = function(model_type, fields) { | ||
var columns; | ||
columns = fields ? _.clone(fields) : model_type.schema().columns(); | ||
if (__indexOf.call(columns, 'id') < 0) { | ||
columns.push('id'); | ||
} | ||
return columns; | ||
}; | ||
SqlCursor.prototype._prefixColumns = function(model_type, fields) { | ||
@@ -547,0 +594,0 @@ var col, columns; |
// Generated by CoffeeScript 1.7.1 | ||
/* | ||
backbone-sql.js 0.6.0 | ||
backbone-sql.js 0.6.2 | ||
Copyright (c) 2013 Vidigami - https://github.com/vidigami/backbone-sql | ||
@@ -6,0 +6,0 @@ License: MIT (http://www.opensource.org/licenses/mit-license.php) |
// Generated by CoffeeScript 1.7.1 | ||
/* | ||
backbone-sql.js 0.6.0 | ||
backbone-sql.js 0.6.2 | ||
Copyright (c) 2013 Vidigami - https://github.com/vidigami/backbone-sql | ||
@@ -6,0 +6,0 @@ License: MIT (http://www.opensource.org/licenses/mit-license.php) |
// Generated by CoffeeScript 1.7.1 | ||
/* | ||
backbone-sql.js 0.6.0 | ||
backbone-sql.js 0.6.2 | ||
Copyright (c) 2013 Vidigami - https://github.com/vidigami/backbone-sql | ||
@@ -6,0 +6,0 @@ License: MIT (http://www.opensource.org/licenses/mit-license.php) |
// Generated by CoffeeScript 1.7.1 | ||
(function() { | ||
var configure; | ||
module.exports = require('backbone-orm').configure; | ||
module.exports = configure = require('backbone-orm').configure; | ||
}).call(this); |
// Generated by CoffeeScript 1.7.1 | ||
/* | ||
backbone-sql.js 0.6.0 | ||
backbone-sql.js 0.6.2 | ||
Copyright (c) 2013 Vidigami - https://github.com/vidigami/backbone-sql | ||
@@ -6,0 +6,0 @@ License: MIT (http://www.opensource.org/licenses/mit-license.php) |
// Generated by CoffeeScript 1.7.1 | ||
/* | ||
backbone-sql.js 0.6.0 | ||
backbone-sql.js 0.6.2 | ||
Copyright (c) 2013 Vidigami - https://github.com/vidigami/backbone-sql | ||
@@ -26,4 +26,26 @@ License: MIT (http://www.opensource.org/licenses/mit-license.php) | ||
CAPABILITIES = { | ||
self_reference: false, | ||
embed: false | ||
mysql: { | ||
embed: false, | ||
json: false, | ||
unique: false, | ||
manual_ids: false, | ||
dynamic: false, | ||
self_reference: false | ||
}, | ||
postgres: { | ||
embed: false, | ||
json: true, | ||
unique: true, | ||
manual_ids: false, | ||
dynamic: false, | ||
self_reference: false | ||
}, | ||
sqlite: { | ||
embed: false, | ||
json: false, | ||
unique: false, | ||
manual_ids: false, | ||
dynamic: false, | ||
self_reference: false | ||
} | ||
}; | ||
@@ -254,7 +276,5 @@ | ||
module.exports.capabilities = function(url) { | ||
return _.extend({ | ||
json: SQLUtils.protocolType(url) === 'postgres' | ||
}, CAPABILITIES); | ||
return CAPABILITIES[SQLUtils.protocolType(url)] || {}; | ||
}; | ||
}).call(this); |
{ | ||
"name": "backbone-sql", | ||
"version": "0.6.0", | ||
"version": "0.6.2", | ||
"description": "PostgreSQL, MySQL, and SQLite3 storage for BackboneORM", | ||
@@ -30,15 +30,7 @@ "main": "./lib/index.js", | ||
"scripts": { | ||
"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", | ||
"watch": "coffee -o lib -w src", | ||
"clean": "rm -rf lib/*", | ||
"prepublish": "npm run build", | ||
"build_dep": "npm run-script backbone-orm build; npm run-script backbone-rest build", | ||
"watch_dep": "npm run-script backbone-orm watch; npm run-script backbone-rest watch" | ||
"test": "gulp test-postgres", | ||
"prepublish": "npm run build" | ||
}, | ||
"devDependencies": { | ||
"async": "*", | ||
"backbone-rest": "0.6.x", | ||
@@ -48,4 +40,9 @@ "body-parser": "1.x", | ||
"coffee-script": "*", | ||
"event-stream": "*", | ||
"express": "3.x", | ||
"mocha": "^1.20.1", | ||
"gulp": "*", | ||
"gulp-coffee": "*", | ||
"gulp-spawn-mocha": "*", | ||
"gulp-util": "*", | ||
"mocha": "*", | ||
"mysql": "2.x", | ||
@@ -52,0 +49,0 @@ "pg": "3.x", |
@@ -78,12 +78,28 @@ [![Build Status](https://travis-ci.org/vidigami/backbone-sql.svg?branch=develop)](https://travis-ci.org/vidigami/backbone-sql) | ||
To build the library for Node.js: | ||
To build the library for Node.js and browsers: | ||
``` | ||
$ npm run | ||
$ gulp build | ||
``` | ||
Please run tests before submitting a pull request. | ||
Please run tests before submitting a pull request: | ||
``` | ||
$ gulp test --quick | ||
``` | ||
and eventually all tests: | ||
``` | ||
$ npm test | ||
``` | ||
# Test Variants | ||
You can run the following fine-grained tests to resolve problems with specific application frameworks | ||
``` | ||
$ gulp test-postgres | ||
$ gulp test-mysql | ||
$ gulp test-sqlite3 | ||
``` |
Please refer to the following release notes when upgrading your version of BackboneSQL. | ||
### 0.6.2 | ||
* Added dynamic and manual_ids capabilities | ||
### 0.6.1 | ||
* Added unique capability | ||
### 0.6.0 | ||
@@ -4,0 +10,0 @@ * Upgraded to BackboneORM 0.6.x |
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
122164
33
1241
105
16