columncompiler.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. 'use strict';
  2. exports.__esModule = true;
  3. var _assign2 = require('lodash/assign');
  4. var _assign3 = _interopRequireDefault(_assign2);
  5. var _inherits = require('inherits');
  6. var _inherits2 = _interopRequireDefault(_inherits);
  7. var _columncompiler = require('../../../schema/columncompiler');
  8. var _columncompiler2 = _interopRequireDefault(_columncompiler);
  9. var _helpers = require('../../../helpers');
  10. var helpers = _interopRequireWildcard(_helpers);
  11. function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
  12. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  13. function ColumnCompiler_PG() {
  14. _columncompiler2.default.apply(this, arguments);
  15. this.modifiers = ['nullable', 'defaultTo', 'comment'];
  16. }
  17. // PostgreSQL Column Compiler
  18. // -------
  19. (0, _inherits2.default)(ColumnCompiler_PG, _columncompiler2.default);
  20. (0, _assign3.default)(ColumnCompiler_PG.prototype, {
  21. // Types
  22. // ------
  23. bigincrements: 'bigserial primary key',
  24. bigint: 'bigint',
  25. binary: 'bytea',
  26. bit: function bit(column) {
  27. return column.length !== false ? 'bit(' + column.length + ')' : 'bit';
  28. },
  29. bool: 'boolean',
  30. // Create the column definition for an enum type.
  31. // Using method "2" here: http://stackoverflow.com/a/10984951/525714
  32. enu: function enu(allowed) {
  33. return 'text check (' + this.formatter.wrap(this.args[0]) + ' in (\'' + allowed.join("', '") + '\'))';
  34. },
  35. double: 'double precision',
  36. decimal: function decimal(precision, scale) {
  37. if (precision === null) return 'decimal';
  38. return 'decimal(' + this._num(precision, 8) + ', ' + this._num(scale, 2) + ')';
  39. },
  40. floating: 'real',
  41. increments: 'serial primary key',
  42. json: function json(jsonb) {
  43. if (jsonb) helpers.deprecate('json(true)', 'jsonb()');
  44. return jsonColumn(this.client, jsonb);
  45. },
  46. jsonb: function jsonb() {
  47. return jsonColumn(this.client, true);
  48. },
  49. smallint: 'smallint',
  50. tinyint: 'smallint',
  51. datetime: function datetime(without) {
  52. return without ? 'timestamp' : 'timestamptz';
  53. },
  54. timestamp: function timestamp(without) {
  55. return without ? 'timestamp' : 'timestamptz';
  56. },
  57. uuid: 'uuid',
  58. // Modifiers:
  59. // ------
  60. comment: function comment(_comment) {
  61. var columnName = this.args[0] || this.defaults('columnName');
  62. this.pushAdditional(function () {
  63. this.pushQuery('comment on column ' + this.tableCompiler.tableName() + '.' + this.formatter.wrap(columnName) + " is " + (_comment ? '\'' + _comment + '\'' : 'NULL'));
  64. }, _comment);
  65. }
  66. });
  67. function jsonColumn(client, jsonb) {
  68. if (!client.version || parseFloat(client.version) >= 9.2) return jsonb ? 'jsonb' : 'json';
  69. return 'text';
  70. }
  71. exports.default = ColumnCompiler_PG;
  72. module.exports = exports['default'];