Comparing version 0.5.10 to 0.5.11
@@ -1,2 +0,2 @@ | ||
// Knex.js 0.5.10 | ||
// Knex.js 0.5.11 | ||
// -------------- | ||
@@ -91,3 +91,3 @@ | ||
// Keep in sync with package.json | ||
knex.VERSION = '0.5.10'; | ||
knex.VERSION = '0.5.11'; | ||
@@ -94,0 +94,0 @@ // Runs a new transaction, taking a container and returning a promise |
@@ -10,4 +10,7 @@ // Builder | ||
var array = []; | ||
var push = array.push; | ||
var concat = function(dst, src) { | ||
for (var i = 0, len = src.length; i < len; i++) { | ||
dst.push(src[i]); | ||
} | ||
}; | ||
@@ -29,2 +32,4 @@ // Constructor for the builder instance, typically called from | ||
var nullOperators = ['is', 'is not']; | ||
// Valid values for the `order by` clause generation. | ||
@@ -55,3 +60,3 @@ var orderBys = ['asc', 'desc']; | ||
if (columns) { | ||
push.apply(this.columns, _.isArray(columns) ? columns : _.toArray(arguments)); | ||
concat(this.columns, _.isArray(columns) ? columns : arguments); | ||
} | ||
@@ -148,3 +153,19 @@ return this; | ||
operator = '='; | ||
} else if (!_.contains(operators, operator)) { | ||
} | ||
operator = operator.toLowerCase(); | ||
if (!_.contains(operators, operator)) { | ||
if (_.contains(nullOperators, operator)) { | ||
if (value === null || _.isString(value) && value.toLowerCase() === 'null') { | ||
return operator === 'is' ? this.whereNull(column, bool) : this.whereNull(column, bool, 'NotNull'); | ||
} | ||
throw new Error('Invalid where in clause'); | ||
} | ||
throw new Error('Invalid operator: ' + operator); | ||
@@ -191,3 +212,3 @@ } | ||
this.wheres.push({type: 'Raw', sql: sql, bool: bool || 'and'}); | ||
push.apply(this.bindings, bindings); | ||
concat(this.bindings, bindings); | ||
return this; | ||
@@ -210,3 +231,3 @@ }, | ||
}); | ||
push.apply(this.bindings, query.bindings); | ||
concat(this.bindings, query.bindings); | ||
return this; | ||
@@ -242,3 +263,3 @@ }, | ||
}); | ||
push.apply(this.bindings, values); | ||
concat(this.bindings, values); | ||
return this; | ||
@@ -286,3 +307,3 @@ }, | ||
this.wheres.push({column: column, type: 'Between', bool: 'and'}); | ||
push.apply(this.bindings, values); | ||
concat(this.bindings, values); | ||
return this; | ||
@@ -294,3 +315,3 @@ }, | ||
this.wheres.push({column: column, type: 'Between', bool: 'or'}); | ||
push.apply(this.bindings, values); | ||
concat(this.bindings, values); | ||
return this; | ||
@@ -345,3 +366,3 @@ }, | ||
this.havings.push({type: 'Raw', sql: sql, bool: bool || 'and'}); | ||
push.apply(this.bindings, bindings); | ||
concat(this.bindings, bindings); | ||
return this; | ||
@@ -394,3 +415,3 @@ }, | ||
increment: function(column, amount) { | ||
return this._counter(column, amount); | ||
return this._counter(column, amount, false); | ||
}, | ||
@@ -400,3 +421,3 @@ | ||
decrement: function(column, amount) { | ||
return this._counter(column, amount, '-'); | ||
return this._counter(column, amount, true); | ||
}, | ||
@@ -407,3 +428,3 @@ | ||
if (columns) { | ||
push.apply(this.columns, _.isArray(columns) ? columns : _.toArray(arguments)); | ||
concat(this.columns, _.isArray(columns) ? columns : arguments); | ||
} | ||
@@ -478,3 +499,3 @@ return this._setType('select'); | ||
this.wheres.push({type: condition, column: column, query: query, bool: bool}); | ||
push.apply(this.bindings, query.bindings); | ||
concat(this.bindings, query.bindings); | ||
return this; | ||
@@ -488,3 +509,3 @@ }, | ||
this.wheres.push({type: 'Nested', query: query, bool: bool}); | ||
push.apply(this.bindings, query.bindings); | ||
concat(this.bindings, query.bindings); | ||
return this; | ||
@@ -504,3 +525,3 @@ }, | ||
}); | ||
push.apply(this.bindings, query.bindings); | ||
concat(this.bindings, query.bindings); | ||
return this; | ||
@@ -517,7 +538,46 @@ }, | ||
// Helper for the incrementing/decrementing queries. | ||
_counter: function(column, amount, symbol) { | ||
amount = parseInt(amount, 10); | ||
if (isNaN(amount)) amount = 1; | ||
_counter: function(column, amount, negative) { | ||
var symbol = '+'; | ||
var defaultAmount = '1'; | ||
if (_.isNumber(amount)) { | ||
if (!isFinite(amount)) { | ||
amount = defaultAmount; | ||
} | ||
else if (amount < 0) { | ||
negative = !negative; | ||
amount = -amount; | ||
} | ||
} | ||
else if (_.isString(amount)) { | ||
amount = amount.trim(); | ||
if (!amount) { | ||
amount = defaultAmount; | ||
} | ||
else { | ||
if (amount.charAt(0) === '-') { | ||
negative = !negative; | ||
amount = amount.substr(1); | ||
} | ||
if (!amount || !/^\d*\.?\d*$/.test(amount)) { | ||
throw new Error('Invalid amount string: ' + amount + '.'); | ||
} | ||
} | ||
} | ||
else if (amount == null) { | ||
// Null or undefined. | ||
amount = defaultAmount; | ||
} | ||
else { | ||
throw new Error('Invalid amount type: ' + (typeof amount) + '.'); | ||
} | ||
if (negative) { | ||
symbol = '-'; | ||
} | ||
var toUpdate = {}; | ||
toUpdate[column] = this.knex.raw(this.grammar.wrap(column) + ' ' + (symbol || '+') + ' ' + amount); | ||
toUpdate[column] = this.knex.raw(this.grammar.wrap(column) + ' ' + symbol + ' ' + amount); | ||
return this.update(toUpdate); | ||
@@ -531,3 +591,3 @@ }, | ||
this.unions.push({query: query, all: bool}); | ||
push.apply(this.bindings, query.bindings); | ||
concat(this.bindings, query.bindings); | ||
}, | ||
@@ -543,6 +603,5 @@ | ||
}); | ||
}, | ||
} | ||
}); | ||
exports.Builder = Builder; |
{ | ||
"name": "knex", | ||
"version": "0.5.10", | ||
"version": "0.5.11", | ||
"description": "A query builder for Postgres, MySQL and SQLite3, designed to be flexible, portable, and fun to use.", | ||
@@ -5,0 +5,0 @@ "main": "knex.js", |
@@ -17,40 +17,42 @@ module.exports = function(knex) { | ||
it('should increment a value', function() { | ||
return knex('accounts').select('logins').where('id', 1).tap(function() { | ||
return knex('accounts').where('id', 1).increment('logins'); | ||
}).then(function(attrs1) { | ||
return knex('accounts').select('logins').where('id', 1).then(function(attrs2) { | ||
expect(attrs1[0].logins + 1).to.equal(attrs2[0].logins); | ||
}); | ||
}); | ||
}); | ||
it('should increment a negative value', function() { | ||
return knex('accounts').select('logins').where('id', 1).tap(function() { | ||
return knex('accounts').where('id', 1).increment('logins', -2); | ||
}).then(function(attrs1) { | ||
return knex('accounts').select('logins').where('id', 1).then(function(attrs2) { | ||
expect(attrs1[0].logins).to.equal(attrs2[0].logins - 1); | ||
expect(attrs1[0].logins - 2).to.equal(attrs2[0].logins); | ||
}); | ||
}); | ||
}); | ||
it('should decrement a value', function() { | ||
return knex('accounts').select('logins').where('id', 1).tap(function() { | ||
return knex('accounts').where('id', 1).decrement('logins'); | ||
}).then(function(attrs1) { | ||
return knex('accounts').select('logins').where('id', 1).then(function(attrs2) { | ||
expect(attrs1[0].logins - 1).to.equal(attrs2[0].logins); | ||
}); | ||
}); | ||
}); | ||
it('should decrement a negative value', function() { | ||
return knex('accounts').select('logins').where('id', 1).tap(function() { | ||
return knex('accounts').where('id', 1).decrement('logins', -2); | ||
}).then(function(attrs1) { | ||
return knex('accounts').select('logins').where('id', 1).then(function(attrs2) { | ||
expect(attrs1[0].logins).to.equal(attrs2[0].logins + 1); | ||
expect(attrs1[0].logins + 2).to.equal(attrs2[0].logins); | ||
}); | ||
}); | ||
}); | ||
it('should allow returning for updates in postgresql', function() { | ||
return knex('accounts').logMe().where('id', 1).update({ | ||
@@ -61,3 +63,2 @@ first_name: 'UpdatedUser', | ||
}, '*'); | ||
}); | ||
@@ -67,2 +68,2 @@ | ||
}; | ||
}; |
@@ -239,2 +239,13 @@ var _ = require('lodash'); | ||
it('should allow case insensitive operators', function() { | ||
var query = builder.where('foo', 'like', 'bar').where('foo', 'LIKE', 'bar'); | ||
expect(query.toString()).to.equal("select * where `foo` like 'bar' and `foo` like 'bar'"); | ||
}); | ||
it('should allow where is null or where is not null', function() { | ||
var query = builder.where('foo', 'is', null).orWhere('foo', 'is not', null) | ||
.orWhere('foo', 'is', 'null').orWhere('foo', 'is not', 'null'); | ||
expect(query.toString()).to.equal("select * where `foo` is null or `foo` is not null or `foo` is null or `foo` is not null"); | ||
}); | ||
}); | ||
@@ -241,0 +252,0 @@ |
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
255249
6584