knex/lib/schema/columnbuilder.js

147 lines
3.5 KiB
JavaScript
Raw Permalink Normal View History

const extend = require('lodash/extend');
const assign = require('lodash/assign');
const toArray = require('lodash/toArray');
const { addQueryContext } = require('../util/helpers');
2015-05-09 13:58:18 -04:00
// The chainable interface off the original "column" method.
class ColumnBuilder {
constructor(client, tableBuilder, type, args) {
this.client = client;
this._method = 'add';
this._single = {};
this._modifiers = {};
this._statements = [];
this._type = columnAlias[type] || type;
this._args = args;
this._tableBuilder = tableBuilder;
// If we're altering the table, extend the object
// with the available "alter" methods.
if (tableBuilder._method === 'alter') {
extend(this, AlterMethods);
}
}
// Specify that the current column "references" a column,
// which may be tableName.column or just "column"
references(value) {
return this._tableBuilder.foreign
.call(this._tableBuilder, this._args[0], undefined, this)
._columnBuilder(this)
.references(value);
2015-05-09 13:58:18 -04:00
}
}
// All of the modifier methods that can be used to modify the current query.
const modifiers = [
'default',
'defaultsTo',
'defaultTo',
'unsigned',
'nullable',
'first',
'after',
'comment',
'collate',
'check',
'checkPositive',
'checkNegative',
'checkIn',
'checkNotIn',
'checkBetween',
'checkLength',
'checkRegex',
2015-05-09 13:58:18 -04:00
];
2016-10-09 17:42:47 -04:00
// Aliases for convenience.
const aliasMethod = {
default: 'defaultTo',
2016-10-09 17:42:47 -04:00
defaultsTo: 'defaultTo',
};
2015-05-09 13:58:18 -04:00
// If we call any of the modifiers (index or otherwise) on the chainable, we pretend
// as though we're calling `table.method(column)` directly.
2020-04-19 00:40:23 +02:00
modifiers.forEach(function (method) {
const key = aliasMethod[method] || method;
2020-04-19 00:40:23 +02:00
ColumnBuilder.prototype[method] = function () {
2016-10-09 17:36:41 -04:00
this._modifiers[key] = toArray(arguments);
2015-05-09 13:58:18 -04:00
return this;
};
});
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
addQueryContext(ColumnBuilder);
ColumnBuilder.prototype.notNull = ColumnBuilder.prototype.notNullable =
function notNullable() {
return this.nullable(false);
};
2016-10-09 17:36:41 -04:00
2020-04-19 00:40:23 +02:00
['index', 'primary', 'unique'].forEach(function (method) {
ColumnBuilder.prototype[method] = function () {
2015-05-09 13:58:18 -04:00
if (this._type.toLowerCase().indexOf('increments') === -1) {
this._tableBuilder[method].apply(
this._tableBuilder,
[this._args[0]].concat(toArray(arguments))
);
2015-05-09 13:58:18 -04:00
}
return this;
};
});
ColumnBuilder.extend = (methodName, fn) => {
if (
Object.prototype.hasOwnProperty.call(ColumnBuilder.prototype, methodName)
) {
throw new Error(
`Can't extend ColumnBuilder with existing method ('${methodName}').`
);
}
assign(ColumnBuilder.prototype, { [methodName]: fn });
};
const AlterMethods = {};
2015-05-09 13:58:18 -04:00
// Specify that the column is to be dropped. This takes precedence
// over all other rules for the column.
2020-04-19 00:40:23 +02:00
AlterMethods.drop = function () {
2015-05-09 13:58:18 -04:00
this._single.drop = true;
2015-05-09 13:58:18 -04:00
return this;
};
// Specify the "type" that we're looking to set the
// Knex takes no responsibility for any data-loss that may
// occur when changing data types.
2020-04-19 00:40:23 +02:00
AlterMethods.alterType = function (type) {
2015-05-09 13:58:18 -04:00
this._statements.push({
grouping: 'alterType',
value: type,
2015-05-09 13:58:18 -04:00
});
2015-05-09 13:58:18 -04:00
return this;
};
// Set column method to alter (default is add).
AlterMethods.alter = function ({
alterNullable = true,
alterType = true,
} = {}) {
this._method = 'alter';
this.alterNullable = alterNullable;
this.alterType = alterType;
return this;
};
2015-05-09 13:58:18 -04:00
// Alias a few methods for clarity when processing.
const columnAlias = {
float: 'floating',
enum: 'enu',
boolean: 'bool',
string: 'varchar',
bigint: 'bigInteger',
2015-05-09 13:58:18 -04:00
};
module.exports = ColumnBuilder;