knex/src/schema/columnbuilder.js

104 lines
2.7 KiB
JavaScript
Raw Normal View History

2015-05-09 13:58:18 -04:00
import { extend, each, toArray } from 'lodash'
2015-05-09 13:58:18 -04:00
// The chainable interface off the original "column" method.
export default function ColumnBuilder(client, tableBuilder, type, args) {
this.client = client;
this._method = 'add';
this._single = {};
this._modifiers = {};
this._statements = [];
this._type = columnAlias[type] || type;
this._args = args;
2015-05-09 13:58:18 -04:00
this._tableBuilder = tableBuilder;
// If we're altering the table, extend the object
// with the available "alter" methods.
if (tableBuilder._method === 'alter') {
2016-03-02 16:52:32 +01:00
extend(this, AlterMethods);
2015-05-09 13:58:18 -04:00
}
}
// All of the modifier methods that can be used to modify the current query.
const modifiers = [
2015-05-09 13:58:18 -04:00
'default', 'defaultsTo', 'defaultTo', 'unsigned',
2016-10-09 17:42:47 -04:00
'nullable', 'first', 'after', 'comment', 'collate'
2015-05-09 13:58:18 -04:00
];
2016-10-09 17:42:47 -04:00
// Aliases for convenience.
const aliasMethod = {
default: 'defaultTo',
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.
2016-03-02 16:52:32 +01:00
each(modifiers, function(method) {
2016-10-09 17:36:41 -04:00
const key = aliasMethod[method] || method
2015-05-09 13:58:18 -04: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;
};
});
2016-10-09 17:42:47 -04:00
ColumnBuilder.prototype.notNull =
2016-10-09 17:36:41 -04:00
ColumnBuilder.prototype.notNullable = function notNullable() {
return this.nullable(false)
}
2016-03-02 16:52:32 +01:00
each(['index', 'primary', 'unique'], function(method) {
2015-05-09 13:58:18 -04:00
ColumnBuilder.prototype[method] = function() {
if (this._type.toLowerCase().indexOf('increments') === -1) {
this._tableBuilder[method].apply(this._tableBuilder,
2016-03-02 16:52:32 +01:00
[this._args[0]].concat(toArray(arguments)));
2015-05-09 13:58:18 -04:00
}
return this;
};
});
// Specify that the current column "references" a column,
// which may be tableName.column or just "column"
ColumnBuilder.prototype.references = function(value) {
return this._tableBuilder.foreign.call(this._tableBuilder, this._args[0], undefined, this)
2015-05-09 13:58:18 -04:00
._columnBuilder(this)
.references(value);
};
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.
AlterMethods.drop = function() {
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.
AlterMethods.alterType = function(type) {
this._statements.push({
grouping: 'alterType',
value: type
});
2015-05-09 13:58:18 -04:00
return this;
};
// Set column method to alter (default is add).
AlterMethods.alter = function() {
this._method = 'alter';
return this;
};
2015-05-09 13:58:18 -04:00
// Alias a few methods for clarity when processing.
const columnAlias = {
2015-05-09 13:58:18 -04:00
'float' : 'floating',
'enum' : 'enu',
'boolean': 'bool',
'string' : 'varchar',
'bigint' : 'bigInteger'
};