knex/src/schema/tablebuilder.js

279 lines
6.8 KiB
JavaScript
Raw Normal View History

2015-05-09 13:58:18 -04:00
// TableBuilder
// Takes the function passed to the "createTable" or "table/editTable"
// functions and calls it with the "TableBuilder" as both the context and
// the first argument. Inside this function we can specify what happens to the
// method, pushing everything we want to do onto the "allStatements" array,
// which is then compiled into sql.
// ------
import { extend, each, toArray, isString, isFunction } from 'lodash'
import * as helpers from '../helpers';
2015-05-09 13:58:18 -04:00
function TableBuilder(client, method, tableName, fn) {
this.client = client
this._fn = fn;
this._method = method;
2015-08-09 22:24:55 -03:00
this._schemaName = undefined;
this._tableName = tableName;
2015-05-09 13:58:18 -04:00
this._statements = [];
this._single = {};
2016-03-15 18:15:27 +01:00
if(!isFunction(this._fn)) {
throw new TypeError(
'A callback function must be supplied to calls against `.createTable` ' +
'and `.table`'
);
}
2015-05-09 13:58:18 -04:00
}
2015-08-09 22:24:55 -03:00
TableBuilder.prototype.setSchema = function(schemaName) {
this._schemaName = schemaName;
};
2015-05-09 13:58:18 -04:00
// Convert the current tableBuilder object "toSQL"
// giving us additional methods if we're altering
// rather than creating the table.
TableBuilder.prototype.toSQL = function() {
if (this._method === 'alter') {
2016-03-02 16:52:32 +01:00
extend(this, AlterMethods);
2015-05-09 13:58:18 -04:00
}
this._fn.call(this, this);
return this.client.tableCompiler(this).toSQL();
};
2016-03-02 16:52:32 +01:00
each([
2015-05-09 13:58:18 -04:00
// Each of the index methods can be called individually, with the
// column name to be used, e.g. table.unique('column').
'index', 'primary', 'unique',
// Key specific
'dropPrimary', 'dropUnique', 'dropIndex', 'dropForeign'
], function(method) {
TableBuilder.prototype[method] = function() {
this._statements.push({
grouping: 'alterTable',
method,
2016-03-02 16:52:32 +01:00
args: toArray(arguments)
2015-05-09 13:58:18 -04:00
});
return this;
};
});
// Warn for dialect-specific table methods, since that's the
// only time these are supported.
const specialMethods = {
mysql: ['engine', 'charset', 'collate'],
postgresql: ['inherits']
};
each(specialMethods, function(methods, dialect) {
each(methods, function(method) {
TableBuilder.prototype[method] = function(value) {
if (this.client.dialect !== dialect) {
helpers.warn(`Knex only supports ${method} statement with ${dialect}.`);
}
if (this._method === 'alter') {
helpers.warn(
`Knex does not support altering the ${method} outside of create ` +
`table, please use knex.raw statement.`
);
}
this._single[method] = value;
};
});
2015-05-09 13:58:18 -04:00
});
// Each of the column types that we can add, we create a new ColumnBuilder
// instance and push it onto the statements array.
const columnTypes = [
2015-05-09 13:58:18 -04:00
// Numeric
'tinyint',
'smallint',
'mediumint',
'int',
'bigint',
'decimal',
'float',
'double',
'real',
'bit',
'boolean',
'serial',
// Date / Time
'date',
'datetime',
'timestamp',
'time',
'year',
// String
'char',
'varchar',
'tinytext',
'tinyText',
'text',
'mediumtext',
'mediumText',
'longtext',
'longText',
'binary',
'varbinary',
'tinyblob',
'tinyBlob',
'mediumblob',
'mediumBlob',
'blob',
'longblob',
'longBlob',
'enum',
'set',
// Increments, Aliases, and Additional
'bool',
'dateTime',
'increments',
'bigincrements',
'bigIncrements',
'integer',
'biginteger',
'bigInteger',
'string',
'json',
2015-11-04 13:34:18 +02:00
'jsonb',
2015-05-09 13:58:18 -04:00
'uuid',
'enu',
'specificType'
];
// For each of the column methods, create a new "ColumnBuilder" interface,
// push it onto the "allStatements" stack, and then return the interface,
// with which we can add indexes, etc.
2016-03-02 16:52:32 +01:00
each(columnTypes, function(type) {
2015-05-09 13:58:18 -04:00
TableBuilder.prototype[type] = function() {
const args = toArray(arguments);
const builder = this.client.columnBuilder(this, type, args);
2015-05-09 13:58:18 -04:00
this._statements.push({
grouping: 'columns',
builder
2015-05-09 13:58:18 -04:00
});
return builder;
};
});
2016-10-09 17:36:41 -04:00
// The "timestamps" call is really just sets the `created_at` and `updated_at` columns.
TableBuilder.prototype.timestamps = function timestamps() {
const method = (arguments[0] === true) ? 'timestamp' : 'datetime';
const createdAt = this[method]('created_at');
const updatedAt = this[method]('updated_at');
if (arguments[1] === true) {
const now = this.client.raw('CURRENT_TIMESTAMP');
createdAt.notNullable().defaultTo(now);
updatedAt.notNullable().defaultTo(now);
}
return;
}
2015-05-09 13:58:18 -04:00
// Set the comment value for a table, they're only allowed to be called
// once per table.
TableBuilder.prototype.comment = function(value) {
if (typeof value !== 'string') {
throw new TypeError('Table comment must be string');
}
2015-05-09 13:58:18 -04:00
this._single.comment = value;
};
// Set a foreign key on the table, calling
// `table.foreign('column_name').references('column').on('table').onDelete()...
// Also called from the ColumnBuilder context when chaining.
TableBuilder.prototype.foreign = function(column, keyName) {
const foreignData = {column: column, keyName: keyName};
2015-05-09 13:58:18 -04:00
this._statements.push({
grouping: 'alterTable',
method: 'foreign',
args: [foreignData]
});
let returnObj = {
references(tableColumn) {
let pieces;
2016-03-02 16:52:32 +01:00
if (isString(tableColumn)) {
2015-05-09 13:58:18 -04:00
pieces = tableColumn.split('.');
}
if (!pieces || pieces.length === 1) {
foreignData.references = pieces ? pieces[0] : tableColumn;
return {
on(tableName) {
if (typeof tableName !== 'string') {
throw new TypeError(`Expected tableName to be a string, got: ${typeof tableName}`);
}
2015-05-09 13:58:18 -04:00
foreignData.inTable = tableName;
return returnObj;
},
inTable() {
2015-05-09 13:58:18 -04:00
return this.on.apply(this, arguments);
}
};
}
foreignData.inTable = pieces[0];
foreignData.references = pieces[1];
return returnObj;
},
withKeyName(keyName) {
foreignData.keyName = keyName;
return returnObj;
},
onUpdate(statement) {
2015-05-09 13:58:18 -04:00
foreignData.onUpdate = statement;
return returnObj;
},
onDelete(statement) {
2015-05-09 13:58:18 -04:00
foreignData.onDelete = statement;
return returnObj;
},
_columnBuilder(builder) {
2016-03-02 16:52:32 +01:00
extend(builder, returnObj);
2015-05-09 13:58:18 -04:00
returnObj = builder;
return builder;
}
};
return returnObj;
}
const AlterMethods = {
2015-05-09 13:58:18 -04:00
// Renames the current column `from` the current
// TODO: this.column(from).rename(to)
renameColumn(from, to) {
2015-05-09 13:58:18 -04:00
this._statements.push({
grouping: 'alterTable',
method: 'renameColumn',
args: [from, to]
});
return this;
},
dropTimestamps() {
2015-05-09 13:58:18 -04:00
return this.dropColumns(['created_at', 'updated_at']);
}
// TODO: changeType
};
// Drop a column from the current table.
// TODO: Enable this.column(columnName).drop();
AlterMethods.dropColumn =
AlterMethods.dropColumns = function() {
this._statements.push({
grouping: 'alterTable',
method: 'dropColumn',
2016-03-02 16:52:32 +01:00
args: toArray(arguments)
2015-05-09 13:58:18 -04:00
});
return this;
};
export default TableBuilder;