builder.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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 _inherits = require('inherits');
  8. var _inherits2 = _interopRequireDefault(_inherits);
  9. var _events = require('events');
  10. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  11. // Constructor for the builder instance, typically called from
  12. // `knex.builder`, accepting the current `knex` instance,
  13. // and pulling out the `client` and `grammar` from the current
  14. // knex instance.
  15. function SchemaBuilder(client) {
  16. this.client = client;
  17. this._sequence = [];
  18. this._debug = client.config && client.config.debug;
  19. }
  20. (0, _inherits2.default)(SchemaBuilder, _events.EventEmitter);
  21. // Each of the schema builder methods just add to the
  22. // "_sequence" array for consistency.
  23. (0, _each3.default)(['createTable', 'createTableIfNotExists', 'createSchema', 'createSchemaIfNotExists', 'dropSchema', 'dropSchemaIfExists', 'createExtension', 'createExtensionIfNotExists', 'dropExtension', 'dropExtensionIfExists', 'table', 'alterTable', 'hasTable', 'hasColumn', 'dropTable', 'renameTable', 'dropTableIfExists', 'raw'], function (method) {
  24. SchemaBuilder.prototype[method] = function () {
  25. if (method === 'table') method = 'alterTable';
  26. this._sequence.push({
  27. method: method,
  28. args: (0, _toArray3.default)(arguments)
  29. });
  30. return this;
  31. };
  32. });
  33. require('../interface')(SchemaBuilder);
  34. SchemaBuilder.prototype.withSchema = function (schemaName) {
  35. this._schema = schemaName;
  36. return this;
  37. };
  38. SchemaBuilder.prototype.toString = function () {
  39. return this.toQuery();
  40. };
  41. SchemaBuilder.prototype.toSQL = function () {
  42. return this.client.schemaCompiler(this).toSQL();
  43. };
  44. exports.default = SchemaBuilder;
  45. module.exports = exports['default'];