renaming utils to helpers, helper to utils

This commit is contained in:
Tim Griesser 2014-04-16 04:29:20 -04:00
parent 1a8be88edc
commit 17c41f1b20
13 changed files with 99 additions and 110 deletions

View File

@ -112,10 +112,10 @@ Knex.initialize = function(config) {
};
});
// Add a few additional misc utils.
knex.utils = _.extend({}, require('./lib/utils'));
return knex;
};
// Convenience for checking whether `obj instanceof Knex.Client`.
Knex.Client = require('./lib/client');
module.exports = Knex;
module.exports = Knex;

View File

@ -7,7 +7,7 @@ var inherits = require('inherits');
var Promise = require('../../promise');
var Runner = require('../../runner');
var utils = require('../../utils');
var helpers = require('../../helpers');
// Inherit from the `Runner` constructor's prototype,
// so we can add the correct `then` method.
@ -68,7 +68,7 @@ Runner_MySQL.prototype.processResponse = function(obj) {
if (obj.output) {
return obj.output.call(this, rows, fields);
} else if (method === 'select') {
resp = utils.skim(rows);
resp = helpers.skim(rows);
} else if (method === 'insert') {
resp = [rows.insertId];
} else if (method === 'del' || method === 'update') {

View File

@ -4,7 +4,7 @@ module.exports = function(client) {
var inherits = require('inherits');
var Schema = require('../../../schema');
var utils = require('../../../utils');
var helpers = require('../../../helpers');
// Column Builder
// -------
@ -96,7 +96,7 @@ ColumnCompiler_MySQL.prototype.after = function(column) {
ColumnCompiler_MySQL.prototype.comment = function(comment) {
if (comment) {
if (comment.length > 255) {
utils.warn('Your comment is longer than the max comment length for MySQL');
helpers.warn('Your comment is longer than the max comment length for MySQL');
}
return "comment '" + comment + "'";
}

View File

@ -44,7 +44,7 @@ TableCompiler_MySQL.prototype.createQuery = function(columns) {
var hasComment = this.single.comment != void 0;
if (hasComment) {
var comment = (this.single.comment || '');
if (comment.length > 60) utils.warn('The max length for a table comment is 60 characters');
if (comment.length > 60) helpers.warn('The max length for a table comment is 60 characters');
sql += " comment = '" + comment + "'";
}

View File

@ -5,7 +5,7 @@ var inherits = require('inherits');
var Promise = require('../../promise');
var Runner = require('../../runner');
var helper = require('../../helper');
var utils = require('../../utils');
// Inherit from the `Runner` constructor's prototype,
// so we can add the correct `then` method.
@ -43,7 +43,7 @@ Runner_PG.prototype._stream = function(stream, options) {
// and any other necessary prep work.
Runner_PG.prototype._query = Promise.method(function(obj) {
var connection = this.connection;
var sql = obj.sql = helper.pgBindings(obj.sql);
var sql = obj.sql = utils.pgBindings(obj.sql);
if (this.isDebugging()) this.debug(obj);
if (obj.options) sql = _.extend({text: sql}, obj.options);
return new Promise(function(resolver, rejecter) {

View File

@ -3,7 +3,7 @@
module.exports = function(client) {
var Promise = require('../../promise');
var Runner = require('../../runner');
var utils = require('../../utils');
var helpers = require('../../helpers');
var inherits = require('inherits');
// Inherit from the `Runner` constructor's prototype,
@ -44,7 +44,7 @@ Runner_SQLite3.prototype.processResponse = function(obj) {
var response = obj.response;
if (obj.output) return obj.output.call(this, response);
if (obj.method === 'select') {
response = utils.skim(response);
response = helpers.skim(response);
} else if (obj.method === 'insert') {
response = [ctx.lastID];
} else if (obj.method === 'del' || obj.method === 'update') {

View File

@ -1,29 +0,0 @@
module.exports = {
// If we are running an insert with variable object keys, we need to normalize
// for the missing keys, presumably setting the values to undefined.
prepInsert: function(data) {
var defaults = _.reduce(_.union.apply(_, _.map(data, function(val) {
return _.keys(val);
})), function(memo, key) {
memo[key] = void 0;
return memo;
}, {});
for (var i = 0, l = vals.length; i<l; i++) {
var obj = vals[i] = helpers.sortObject(_.defaults(vals[i], defaults));
for (var i2 = 0, l2 = obj.length; i2 < l2; i2++) {
obj[i2][1] = f.parameter(obj[i2][1]);
}
}
return vals;
},
pgBindings: function(sql) {
var questionCount = 0;
return sql.replace(/\?/g, function() {
questionCount++;
return '$' + questionCount;
});
}
};

51
lib/helpers.js Normal file
View File

@ -0,0 +1,51 @@
// Util.js
// -------
// Just some common functions needed in multiple places within the library.
var _ = require('lodash');
var helpers = {
// Pick off the attributes from only the current layer of the object.
skim: function(data) {
return _.map(data, function(obj) {
return _.pick(obj, _.keys(obj));
});
},
// Check if the first argument is an array, otherwise
// uses all arguments as an array.
normalizeArr: function() {
var args = new Array(arguments.length);
for (var i = 0; i < args.length; i++) {
args[i] = arguments[i];
}
if (_.isArray(args[0])) {
return args[0];
}
return args;
},
// Used to signify deprecated functionality.
deprecate: function(msg) {
this.warn(msg);
},
// Used to warn about incorrect use, without error'ing
warn: function(msg) {
if (typeof console !== "undefined" && console !== null &&
typeof console.warn === "function") {
console.warn("Knex: " + msg);
}
},
// Sort the keys for the insert
sortObject: function(obj) {
return _.sortBy(_.pairs(obj), function(a) {
return a[0];
});
}
};
module.exports = helpers;

View File

@ -1,12 +1,8 @@
var Promise = require('bluebird/js/main/promise')();
Promise.prototype.yield = Promise.prototype.thenReturn;
Promise.prototype.tap = Promise.prototype.tap;
Promise.prototype.ensure = Promise.prototype.lastly;
Promise.prototype.yield = Promise.prototype.thenReturn;
Promise.prototype.ensure = Promise.prototype.lastly;
Promise.prototype.otherwise = Promise.prototype.caught;
Promise.prototype.exec = Promise.prototype.nodeify;
Promise.prototype.exec = Promise.prototype.nodeify;
Promise.resolve = Promise.fulfilled;
Promise.reject = Promise.rejected;
module.exports = Promise;
module.exports = Promise;

View File

@ -3,7 +3,7 @@
var _ = require('lodash');
var Raw = require('../raw');
var utils = require('../utils');
var helpers = require('../helpers');
var JoinClause = require('./joinclause');
@ -31,7 +31,7 @@ var orderBys = ['asc', 'desc'];
// In the next minor release, this will become:
// return '[object Knex:QueryBuilder]';
QueryBuilder.prototype.toString = function() {
utils.deprecate('The toString method is deprecated, please use `toQuery` instead.');
helpers.deprecate('The toString method is deprecated, please use `toQuery` instead.');
return this.toQuery();
};
@ -68,7 +68,7 @@ QueryBuilder.prototype.columns =
QueryBuilder.prototype.column = function() {
this._statements.push({
grouping: 'columns',
value: utils.normalizeArr.apply(null, arguments)
value: helpers.normalizeArr.apply(null, arguments)
});
return this;
};
@ -87,7 +87,7 @@ QueryBuilder.prototype.into = QueryBuilder.prototype.table;
QueryBuilder.prototype.distinct = function() {
this._statements.push({
grouping: 'columns',
value: utils.normalizeArr.apply(null, arguments),
value: helpers.normalizeArr.apply(null, arguments),
distinct: true
});
return this;
@ -101,14 +101,14 @@ QueryBuilder.prototype.join = function(table, first, operator, second) {
args[i] = arguments[i];
}
if (args.length === 5) {
utils.deprecate('The five argument join syntax is now deprecated, ' +
helpers.deprecate('The five argument join syntax is now deprecated, ' +
'please check the docs and update your code.');
return this._joinType(args[4]).join(table, first, operator, second);
}
var join;
if (_.isFunction(first)) {
if (args.length > 2) {
utils.deprecate('The [table, fn, type] join syntax is deprecated, ' +
helpers.deprecate('The [table, fn, type] join syntax is deprecated, ' +
'please check the docs and update your code.');
return this._joinType(args[2]).join(table, first);
}
@ -367,7 +367,7 @@ QueryBuilder.prototype.orWhereNotBetween = function(column, values) {
QueryBuilder.prototype.groupBy = function() {
this._statements.push({
grouping: 'group',
value: utils.normalizeArr.apply(null, arguments)
value: helpers.normalizeArr.apply(null, arguments)
});
return this;
};

View File

@ -1,9 +1,9 @@
// Query Compiler
// -------
var _ = require('lodash');
var utils = require('../utils');
var Raw = require('../raw');
var _ = require('lodash');
var helpers = require('../helpers');
var Raw = require('../raw');
// The "QueryCompiler" takes all of the query statements which have been
// gathered in the "QueryBuilder" and turns them into a properly formatted / bound
@ -71,7 +71,7 @@ QueryCompiler.prototype.insert = function() {
// Compiles the "update" query.
QueryCompiler.prototype.update = function() {
obj = utils.sortObject(obj);
obj = helpers.sortObject(obj);
var vals = [];
for (var i = 0; i < obj.length; i++) {
var value = obj[i];
@ -325,7 +325,7 @@ QueryCompiler.prototype._prepInsert = function(data) {
var columns;
if (!_.isArray(data)) data = data ? [data] : [];
for (var i = 0, l = data.length; i<l; i++) {
var sorted = utils.sortObject(data[i]);
var sorted = helpers.sortObject(data[i]);
if (i === 0) columns = _.pluck(sorted, 0);
values.push(_.pluck(sorted, 1));
}
@ -338,7 +338,7 @@ QueryCompiler.prototype._prepInsert = function(data) {
// "Preps" the update.
QueryCompiler.prototype._prepUpdate = function(data) {
var vals = [];
var sorted = utils.sortObject(data);
var sorted = helpers.sortObject(data);
for (var i = 0, l = sorted.length; i < l; i++) {
vals.push(this.formatter.wrap(sorted[i][0]) + ' = ' + this.formatter.parameter(sorted[i][1]));
}

View File

@ -2,7 +2,7 @@
// -------
var _ = require('lodash');
var utils = require('../utils');
var helpers = require('../helpers');
function TableCompiler(tableBuilder) {
this.method = tableBuilder._method;
@ -131,7 +131,7 @@ TableCompiler.prototype.dropForeign = function() {
TableCompiler.prototype.dropColumnPrefix = 'drop column ';
TableCompiler.prototype.dropColumn = function() {
var columns = utils.normalizeArr.apply(null, arguments);
var columns = helpers.normalizeArr.apply(null, arguments);
var drops = _.map(_.isArray(columns) ? columns : [columns], function(column) {
return this.dropColumnPrefix + this.formatter.wrap(column);
}, this);

View File

@ -1,51 +1,22 @@
// Util.js
// -------
module.exports = {
// Just some common functions needed in multiple places within the library.
var _ = require('lodash');
var utils = {
// Pick off the attributes from only the current layer of the object.
skim: function(data) {
return _.map(data, function(obj) {
return _.pick(obj, _.keys(obj));
});
// If we are running an insert with variable object keys, we need to normalize
// for the missing keys, presumably setting the values to undefined.
prepInsert: function(data) {
return _.reduce(_.union.apply(_, _.map(data, function(val) {
return _.keys(val);
})), function(memo, key) {
memo[key] = void 0;
return memo;
}, {});
},
// Check if the first argument is an array, otherwise
// uses all arguments as an array.
normalizeArr: function() {
var args = new Array(arguments.length);
for (var i = 0; i < args.length; i++) {
args[i] = arguments[i];
}
if (_.isArray(args[0])) {
return args[0];
}
return args;
},
// Used to signify deprecated functionality.
deprecate: function(msg) {
this.warn(msg);
},
// Used to warn about incorrect use, without error'ing
warn: function(msg) {
if (typeof console !== "undefined" && console !== null &&
typeof console.warn === "function") {
console.warn("Knex: " + msg);
}
},
// Sort the keys for the insert
sortObject: function(obj) {
return _.sortBy(_.pairs(obj), function(a) {
return a[0];
pgBindings: function(sql) {
var questionCount = 0;
return sql.replace(/\?/g, function() {
questionCount++;
return '$' + questionCount;
});
}
};
module.exports = utils;
};