knex/src/helpers.js

88 lines
2.2 KiB
JavaScript
Raw Normal View History

/* eslint no-console:0 */
2015-05-09 13:58:18 -04:00
import { map, pick, keys, isFunction, isUndefined, isObject, isArray, isTypedArray } from 'lodash'
import chalk from 'chalk';
// Pick off the attributes from only the current layer of the object.
export function skim(data) {
return map(data, (obj) => pick(obj, keys(obj)));
}
// Check if the first argument is an array, otherwise uses all arguments as an
// array.
export function normalizeArr() {
const args = new Array(arguments.length);
for (let i = 0; i < args.length; i++) {
args[i] = arguments[i];
}
if (Array.isArray(args[0])) {
return args[0];
}
return args;
}
2015-05-09 13:58:18 -04:00
export function debugLog(msg) {
console.log(msg);
}
2015-05-09 13:58:18 -04:00
export function error(msg) {
console.log(chalk.red(`Knex:Error ${msg}`))
}
2015-05-09 13:58:18 -04:00
// Used to signify deprecated functionality.
export function deprecate(method, alternate) {
warn(`${method} is deprecated, please use ${alternate}`);
}
2015-05-09 13:58:18 -04:00
// Used to warn about incorrect use, without error'ing
export function warn(msg) {
console.log(chalk.yellow(`Knex:warning - ${msg}`))
}
export function exit(msg) {
console.log(chalk.red(msg))
process.exit(1)
}
export function containsUndefined(mixed) {
let argContainsUndefined = false;
if (isTypedArray(mixed))
return false;
if(mixed && isFunction(mixed.toSQL)) {
//Any QueryBuilder or Raw will automatically be validated during compile.
return argContainsUndefined;
}
if(isArray(mixed)) {
for(let i = 0; i < mixed.length; i++) {
if(argContainsUndefined) break;
argContainsUndefined = this.containsUndefined(mixed[i]);
}
} else if(isObject(mixed)) {
for(const key in mixed) {
if (mixed.hasOwnProperty(key)) {
if(argContainsUndefined) break;
argContainsUndefined = this.containsUndefined(mixed[key]);
}
}
} else {
argContainsUndefined = isUndefined(mixed);
}
return argContainsUndefined;
Add queryContext to schema and query builders (#2314) * feat(query-builder): add hookContext for wrapIdentifier * refactor: use isUndefined * test(transaction): test passing of hookContext * feat(runnner): pass context to postProcessResponse * test(runner): test postProcessResponse for raw responses * test(raw): test passing of hookContext * feat: add hookContext to Raw and SchemaBuilder * test(transaction): fix test for hookContext * chore: fix lint error * fix: check for hookContext before calling it * test(transaction): fix hookContext test * chore: remove whitespace * test(hookContext): test cloning of context object * refactor: hookContext -> queryContext * minor: use more descriptive variable name i.e. refactor: `context` => `queryContext` * fix: remove unnecessary checks for query builder * fix(Raw): pass query builder to formatter * fix(SchemaCompiler): pass schema builder to formatter * refactor: add addQueryContext helper * feat: add queryContext to TableBuilder and ColumnBuilder * fix(TableCompiler): pass table builder to formatter * fix(ColumnCompiler): pass column builder to formatter * fix(pushQuery): fix passing builder to formatter * test(Schema|Table|ColumnCompiler): test passing queryContext * fix(SchemaCompiler): pass queryContext to TableCompiler * fix(TableCompiler): pass queryContext to ColumnCompiler * test: add queryContext tests for all schema dialects * test(TableCompiler): test overwriting queryContext from SchemaCompiler * test(Raw): test passing queryContext to wrapIdentifier * tests: run all the tests
2018-02-01 23:41:01 +01:00
}
export function addQueryContext(Target) {
// Stores or returns (if called with no arguments) context passed to
// wrapIdentifier and postProcessResponse hooks
Target.prototype.queryContext = function(context) {
if (isUndefined(context)) {
return this._queryContext;
}
this._queryContext = context;
return this;
}
}