knex/lib/schema/helpers.js
Bogdan Chadkin 6f817a3b6e
Avoid lodash typecheks (#4056)
* Avoid lodash typecheks

Lodash is quite big project. Even with direct imports it loads [tons](https://github.com/knex/knex/pull/3804) of
code and still bloats node_modules. Especially since lodash mostly used
as a polyfill for modern features.

In this diff I attempted to reduce lodash usage by replacing type checks
with `typeof` operator which might be sufficient.

Also replaced lodash/isObject with custom simplified utility which does not
consider functions as objects and allows to simplify code in one place.
2020-10-05 21:29:39 +03:00

52 lines
1.3 KiB
JavaScript

const tail = require('lodash/tail');
const { isString } = require('../util/is');
// Push a new query onto the compiled "sequence" stack,
// creating a new formatter, returning the compiler.
function pushQuery(query) {
if (!query) return;
if (isString(query)) {
query = { sql: query };
}
if (!query.bindings) {
query.bindings = this.formatter.bindings;
}
this.sequence.push(query);
this.formatter = this.client.formatter(this._commonBuilder);
}
// Used in cases where we need to push some additional column specific statements.
function pushAdditional(fn) {
const child = new this.constructor(
this.client,
this.tableCompiler,
this.columnBuilder
);
fn.call(child, tail(arguments));
this.sequence.additional = (this.sequence.additional || []).concat(
child.sequence
);
}
// Unshift a new query onto the compiled "sequence" stack,
// creating a new formatter, returning the compiler.
function unshiftQuery(query) {
if (!query) return;
if (isString(query)) {
query = { sql: query };
}
if (!query.bindings) {
query.bindings = this.formatter.bindings;
}
this.sequence.unshift(query);
this.formatter = this.client.formatter(this._commonBuilder);
}
module.exports = {
pushAdditional,
pushQuery,
unshiftQuery,
};