columncompiler.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. floating: 'real',
  37. increments: 'serial primary key',
  38. json: function json(jsonb) {
  39. if (jsonb) helpers.deprecate('json(true)', 'jsonb()');
  40. return jsonColumn(this.client, jsonb);
  41. },
  42. jsonb: function jsonb() {
  43. return jsonColumn(this.client, true);
  44. },
  45. smallint: 'smallint',
  46. tinyint: 'smallint',
  47. datetime: function datetime(without) {
  48. return without ? 'timestamp' : 'timestamptz';
  49. },
  50. timestamp: function timestamp(without) {
  51. return without ? 'timestamp' : 'timestamptz';
  52. },
  53. uuid: 'uuid',
  54. // Modifiers:
  55. // ------
  56. comment: function comment(_comment) {
  57. this.pushAdditional(function () {
  58. this.pushQuery('comment on column ' + this.tableCompiler.tableName() + '.' + this.formatter.wrap(this.args[0]) + " is " + (_comment ? '\'' + _comment + '\'' : 'NULL'));
  59. }, _comment);
  60. }
  61. });
  62. function jsonColumn(client, jsonb) {
  63. if (!client.version || parseFloat(client.version) >= 9.2) return jsonb ? 'jsonb' : 'json';
  64. return 'text';
  65. }
  66. exports.default = ColumnCompiler_PG;
  67. module.exports = exports['default'];