columnbuilder.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. 'use strict';
  2. exports.__esModule = true;
  3. var _toArray2 = require('lodash/toArray');
  4. var _toArray3 = _interopRequireDefault(_toArray2);
  5. var _each2 = require('lodash/each');
  6. var _each3 = _interopRequireDefault(_each2);
  7. var _extend2 = require('lodash/extend');
  8. var _extend3 = _interopRequireDefault(_extend2);
  9. exports.default = ColumnBuilder;
  10. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  11. // The chainable interface off the original "column" method.
  12. function ColumnBuilder(client, tableBuilder, type, args) {
  13. this.client = client;
  14. this._method = 'add';
  15. this._single = {};
  16. this._modifiers = {};
  17. this._statements = [];
  18. this._type = columnAlias[type] || type;
  19. this._args = args;
  20. this._tableBuilder = tableBuilder;
  21. // If we're altering the table, extend the object
  22. // with the available "alter" methods.
  23. if (tableBuilder._method === 'alter') {
  24. (0, _extend3.default)(this, AlterMethods);
  25. }
  26. }
  27. // All of the modifier methods that can be used to modify the current query.
  28. var modifiers = ['default', 'defaultsTo', 'defaultTo', 'unsigned', 'nullable', 'first', 'after', 'comment', 'collate'];
  29. // Aliases for convenience.
  30. var aliasMethod = {
  31. default: 'defaultTo',
  32. defaultsTo: 'defaultTo'
  33. };
  34. // If we call any of the modifiers (index or otherwise) on the chainable, we pretend
  35. // as though we're calling `table.method(column)` directly.
  36. (0, _each3.default)(modifiers, function (method) {
  37. var key = aliasMethod[method] || method;
  38. ColumnBuilder.prototype[method] = function () {
  39. this._modifiers[key] = (0, _toArray3.default)(arguments);
  40. return this;
  41. };
  42. });
  43. ColumnBuilder.prototype.notNull = ColumnBuilder.prototype.notNullable = function notNullable() {
  44. return this.nullable(false);
  45. };
  46. (0, _each3.default)(['index', 'primary', 'unique'], function (method) {
  47. ColumnBuilder.prototype[method] = function () {
  48. if (this._type.toLowerCase().indexOf('increments') === -1) {
  49. this._tableBuilder[method].apply(this._tableBuilder, [this._args[0]].concat((0, _toArray3.default)(arguments)));
  50. }
  51. return this;
  52. };
  53. });
  54. // Specify that the current column "references" a column,
  55. // which may be tableName.column or just "column"
  56. ColumnBuilder.prototype.references = function (value) {
  57. return this._tableBuilder.foreign.call(this._tableBuilder, this._args[0], undefined, this)._columnBuilder(this).references(value);
  58. };
  59. var AlterMethods = {};
  60. // Specify that the column is to be dropped. This takes precedence
  61. // over all other rules for the column.
  62. AlterMethods.drop = function () {
  63. this._single.drop = true;
  64. return this;
  65. };
  66. // Specify the "type" that we're looking to set the
  67. // Knex takes no responsibility for any data-loss that may
  68. // occur when changing data types.
  69. AlterMethods.alterType = function (type) {
  70. this._statements.push({
  71. grouping: 'alterType',
  72. value: type
  73. });
  74. return this;
  75. };
  76. // Set column method to alter (default is add).
  77. AlterMethods.alter = function () {
  78. this._method = 'alter';
  79. return this;
  80. };
  81. // Alias a few methods for clarity when processing.
  82. var columnAlias = {
  83. 'float': 'floating',
  84. 'enum': 'enu',
  85. 'boolean': 'bool',
  86. 'string': 'varchar',
  87. 'bigint': 'bigInteger'
  88. };
  89. module.exports = exports['default'];