@dbml/core 3.13.7-alpha.0 → 3.13.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -6,9 +6,13 @@ Object.defineProperty(exports, "__esModule", {
6
6
  exports["default"] = void 0;
7
7
  var _lodash = _interopRequireDefault(require("lodash"));
8
8
  var _utils = require("./utils");
9
- var _config = require("../model_structure/config");
9
+ var _utils2 = require("../model_structure/utils");
10
10
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
11
11
  function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
12
+ function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); }
13
+ function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
14
+ function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
15
+ function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
12
16
  function _toConsumableArray(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread(); }
13
17
  function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
14
18
  function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
@@ -20,6 +24,43 @@ function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o =
20
24
  function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
21
25
  function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
22
26
  function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
27
+ // PostgreSQL built-in data types
28
+ // Generated from PostgreSQLParser.g4 and PostgreSQLLexer.g4
29
+
30
+ var POSTGRES_BUILTIN_TYPES = [
31
+ // Numeric types
32
+ 'SMALLINT', 'INTEGER', 'INT', 'BIGINT', 'DECIMAL', 'NUMERIC', 'REAL', 'DOUBLE PRECISION', 'SMALLSERIAL', 'SERIAL', 'BIGSERIAL', 'FLOAT',
33
+ // Monetary types
34
+ 'MONEY',
35
+ // Character types
36
+ 'CHARACTER', 'CHAR', 'CHARACTER VARYING', 'VARCHAR', 'TEXT', 'NAME', 'BPCHAR',
37
+ // Binary data types
38
+ 'BYTEA',
39
+ // Date/time types
40
+ 'TIMESTAMP', 'TIMESTAMP WITH TIME ZONE', 'TIMESTAMP WITHOUT TIME ZONE', 'DATE', 'TIME', 'TIME WITH TIME ZONE', 'TIME WITHOUT TIME ZONE', 'INTERVAL',
41
+ // Boolean type
42
+ 'BOOLEAN', 'BOOL',
43
+ // Geometric types
44
+ 'POINT', 'LINE', 'LSEG', 'BOX', 'PATH', 'POLYGON', 'CIRCLE',
45
+ // Network address types
46
+ 'CIDR', 'INET', 'MACADDR', 'MACADDR8',
47
+ // Bit string types
48
+ 'BIT', 'BIT VARYING', 'VARBIT',
49
+ // UUID type
50
+ 'UUID',
51
+ // XML type
52
+ 'XML',
53
+ // JSON types
54
+ 'JSON', 'JSONB',
55
+ // Range types
56
+ 'INT4RANGE', 'INT8RANGE', 'NUMRANGE', 'TSRANGE', 'TSTZRANGE', 'DATERANGE',
57
+ // Object identifier types
58
+ 'OID', 'REGCLASS', 'REGCOLLATION', 'REGCONFIG', 'REGDICTIONARY', 'REGNAMESPACE', 'REGOPER', 'REGOPERATOR', 'REGPROC', 'REGPROCEDURE', 'REGROLE', 'REGTYPE',
59
+ // pg_lsn type
60
+ 'PG_LSN',
61
+ // Special types
62
+ 'VOID', 'RECORD', 'TRIGGER', 'EVENT_TRIGGER', 'PG_DDL_COMMAND', 'UNKNOWN', 'ANYELEMENT', 'ANYARRAY', 'ANYNONARRAY', 'ANYENUM', 'ANYRANGE', 'ANY', 'CSTRING', 'INTERNAL', 'LANGUAGE_HANDLER', 'FDW_HANDLER', 'INDEX_AM_HANDLER', 'TSM_HANDLER', 'OPAQUE'];
63
+ var POSTGRES_RESERVED_KEYWORDS = ['USER'];
23
64
  var PostgresExporter = /*#__PURE__*/function () {
24
65
  function PostgresExporter() {
25
66
  _classCallCheck(this, PostgresExporter);
@@ -27,22 +68,22 @@ var PostgresExporter = /*#__PURE__*/function () {
27
68
  return _createClass(PostgresExporter, null, [{
28
69
  key: "exportEnums",
29
70
  value: function exportEnums(enumIds, model) {
30
- var enumArr = enumIds.map(function (enumId) {
71
+ return enumIds.map(function (enumId) {
31
72
  var _enum = model.enums[enumId];
32
73
  var schema = model.schemas[_enum.schemaId];
74
+ var enumName = "".concat((0, _utils.shouldPrintSchema)(schema, model) ? "\"".concat(schema.name, "\".") : '', "\"").concat(_enum.name, "\"");
33
75
  var enumValueArr = _enum.valueIds.map(function (valueId) {
34
76
  var value = model.enumValues[valueId];
35
77
  return " '".concat(value.name, "'");
36
78
  });
37
79
  var enumValueStr = enumValueArr.join(',\n');
38
- var line = "CREATE TYPE ".concat((0, _utils.shouldPrintSchema)(schema, model) ? "\"".concat(schema.name, "\".") : '', "\"").concat(_enum.name, "\" AS ENUM (\n").concat(enumValueStr, "\n);\n");
39
- return line;
80
+ var enumLine = "CREATE TYPE ".concat(enumName, " AS ENUM (\n").concat(enumValueStr, "\n);\n");
81
+ return [enumName, enumLine];
40
82
  });
41
- return enumArr;
42
83
  }
43
84
  }, {
44
85
  key: "getFieldLines",
45
- value: function getFieldLines(tableId, model) {
86
+ value: function getFieldLines(tableId, model, enumSet) {
46
87
  var table = model.tables[tableId];
47
88
  var lines = table.fieldIds.map(function (fieldId) {
48
89
  var field = model.fields[fieldId];
@@ -54,13 +95,23 @@ var PostgresExporter = /*#__PURE__*/function () {
54
95
  var type = '';
55
96
  if (typicalIntergers.has(typeRaw)) type = "".concat(typeRaw, " GENERATED BY DEFAULT AS IDENTITY");else if (incrementIntergers.has(typeRaw)) type = typeRaw;else type = 'SERIAL';
56
97
  line = "\"".concat(field.name, "\" ").concat(type);
98
+ } else if (!field.type.schemaName || !(0, _utils2.shouldPrintSchemaName)(field.type.schemaName)) {
99
+ // The result type will only has the type part, no schema part (e.g. `int` or `dollars`)
100
+ var originalTypeName = field.type.type_name;
101
+ var upperCaseTypeName = originalTypeName.toUpperCase();
102
+ var shouldDoubleQuote = !POSTGRES_BUILTIN_TYPES.includes(upperCaseTypeName) && ((0, _utils.hasWhiteSpaceOrUpperCase)(originalTypeName) || POSTGRES_RESERVED_KEYWORDS.includes(upperCaseTypeName));
103
+ var typeName = shouldDoubleQuote ? "\"".concat(originalTypeName, "\"") : originalTypeName;
104
+ line = "\"".concat(field.name, "\" ").concat(typeName);
105
+ } else if (field.type.originalTypeName) {
106
+ // A custom Postgres type that is not defined as enum in DBML content
107
+ line = "\"".concat(field.name, "\" \"").concat(field.type.schemaName, "\".\"").concat(field.type.originalTypeName, "\"");
57
108
  } else {
58
- var schemaName = '';
59
- if (field.type.schemaName && field.type.schemaName !== _config.DEFAULT_SCHEMA_NAME) {
60
- schemaName = (0, _utils.hasWhiteSpaceOrUpperCase)(field.type.schemaName) ? "\"".concat(field.type.schemaName, "\".") : "".concat(field.type.schemaName, ".");
61
- }
62
- var typeName = (0, _utils.hasWhiteSpaceOrUpperCase)(field.type.type_name) ? "\"".concat(field.type.type_name, "\"") : field.type.type_name;
63
- line = "\"".concat(field.name, "\" ").concat(schemaName).concat(typeName);
109
+ var schemaName = (0, _utils.hasWhiteSpaceOrUpperCase)(field.type.schemaName) ? "\"".concat(field.type.schemaName, "\".") : "".concat(field.type.schemaName, ".");
110
+ var _typeName = (0, _utils.hasWhiteSpaceOrUpperCase)(field.type.type_name) ? "\"".concat(field.type.type_name, "\"") : field.type.type_name;
111
+ var typeWithSchema = "".concat(schemaName).concat(_typeName);
112
+ var typeAsEnum = "\"".concat(field.type.schemaName, "\".\"").concat(field.type.type_name, "\"");
113
+ if (!enumSet.has(typeAsEnum) && !(0, _utils.hasWhiteSpace)(typeAsEnum)) typeWithSchema = typeWithSchema.replaceAll('"', '');
114
+ line = "\"".concat(field.name, "\" ").concat(typeWithSchema);
64
115
  }
65
116
  if (field.unique) {
66
117
  line += ' UNIQUE';
@@ -112,9 +163,9 @@ var PostgresExporter = /*#__PURE__*/function () {
112
163
  }
113
164
  }, {
114
165
  key: "getTableContentArr",
115
- value: function getTableContentArr(tableIds, model) {
166
+ value: function getTableContentArr(tableIds, model, enumSet) {
116
167
  var tableContentArr = tableIds.map(function (tableId) {
117
- var fieldContents = PostgresExporter.getFieldLines(tableId, model);
168
+ var fieldContents = PostgresExporter.getFieldLines(tableId, model, enumSet);
118
169
  var compositePKs = PostgresExporter.getCompositePKs(tableId, model);
119
170
  return {
120
171
  tableId: tableId,
@@ -126,8 +177,8 @@ var PostgresExporter = /*#__PURE__*/function () {
126
177
  }
127
178
  }, {
128
179
  key: "exportTables",
129
- value: function exportTables(tableIds, model) {
130
- var tableContentArr = PostgresExporter.getTableContentArr(tableIds, model);
180
+ value: function exportTables(tableIds, model, enumSet) {
181
+ var tableContentArr = PostgresExporter.getTableContentArr(tableIds, model, enumSet);
131
182
  var tableStrs = tableContentArr.map(function (tableContent) {
132
183
  var content = [].concat(_toConsumableArray(tableContent.fieldContents), _toConsumableArray(tableContent.compositePKs));
133
184
  var table = model.tables[tableContent.tableId];
@@ -293,21 +344,42 @@ var PostgresExporter = /*#__PURE__*/function () {
293
344
  var usedTableNames = new Set(Object.values(model.tables).map(function (table) {
294
345
  return table.name;
295
346
  }));
296
- var statements = database.schemaIds.reduce(function (prevStatements, schemaId) {
347
+
348
+ // Pre-collect all user-defined enum names to distinguish them from built-in PostgreSQL types
349
+ // This prevents built-in types like VARCHAR, INTEGER from being quoted unnecessarily
350
+ var enumSet = new Set();
351
+ var schemaEnumStatements = database.schemaIds.reduce(function (prevStatements, schemaId) {
297
352
  var schema = model.schemas[schemaId];
298
- var tableIds = schema.tableIds,
299
- enumIds = schema.enumIds,
300
- refIds = schema.refIds;
353
+ var enumIds = schema.enumIds;
301
354
  if ((0, _utils.shouldPrintSchema)(schema, model)) {
302
355
  prevStatements.schemas.push("CREATE SCHEMA \"".concat(schema.name, "\";\n"));
303
356
  }
304
357
  if (!_lodash["default"].isEmpty(enumIds)) {
305
- var _prevStatements$enums;
306
- (_prevStatements$enums = prevStatements.enums).push.apply(_prevStatements$enums, _toConsumableArray(PostgresExporter.exportEnums(enumIds, model)));
358
+ var enumPairs = PostgresExporter.exportEnums(enumIds, model);
359
+ enumPairs.forEach(function (enumPair) {
360
+ var _enumPair = _slicedToArray(enumPair, 2),
361
+ enumName = _enumPair[0],
362
+ enumLine = _enumPair[1];
363
+ prevStatements.enums.push(enumLine);
364
+ enumSet.add(enumName);
365
+ });
307
366
  }
367
+ return prevStatements;
368
+ }, {
369
+ schemas: [],
370
+ enums: [],
371
+ tables: [],
372
+ indexes: [],
373
+ comments: [],
374
+ refs: []
375
+ });
376
+ var statements = database.schemaIds.reduce(function (prevStatements, schemaId) {
377
+ var schema = model.schemas[schemaId];
378
+ var tableIds = schema.tableIds,
379
+ refIds = schema.refIds;
308
380
  if (!_lodash["default"].isEmpty(tableIds)) {
309
381
  var _prevStatements$table;
310
- (_prevStatements$table = prevStatements.tables).push.apply(_prevStatements$table, _toConsumableArray(PostgresExporter.exportTables(tableIds, model)));
382
+ (_prevStatements$table = prevStatements.tables).push.apply(_prevStatements$table, _toConsumableArray(PostgresExporter.exportTables(tableIds, model, enumSet)));
311
383
  }
312
384
  var indexIds = _lodash["default"].flatten(tableIds.map(function (tableId) {
313
385
  return model.tables[tableId].indexIds;
@@ -343,14 +415,7 @@ var PostgresExporter = /*#__PURE__*/function () {
343
415
  (_prevStatements$refs = prevStatements.refs).push.apply(_prevStatements$refs, _toConsumableArray(PostgresExporter.exportRefs(refIds, model, usedTableNames)));
344
416
  }
345
417
  return prevStatements;
346
- }, {
347
- schemas: [],
348
- enums: [],
349
- tables: [],
350
- indexes: [],
351
- comments: [],
352
- refs: []
353
- });
418
+ }, schemaEnumStatements);
354
419
  var res = _lodash["default"].concat(statements.schemas, statements.enums, statements.tables, statements.indexes, statements.comments, statements.refs).join('\n');
355
420
  return res;
356
421
  }
@@ -89,6 +89,9 @@ var Field = /*#__PURE__*/function (_Element) {
89
89
  // SQL allow definition of non-enum type to be used as column type, which we don't have equivalent dbml counterpart.
90
90
  // So instead of throwing errors on those type, we can view the type as plain text for the purpose of importing to dbml.
91
91
  this.type.type_name = "".concat(typeSchemaName, ".").concat(typeName);
92
+ // We set this field to avoid doubling schema name when exporting this type to SQL
93
+ // e.g. to avoid `schema.schema.type`
94
+ this.type.originalTypeName = typeName;
92
95
  return;
93
96
  }
94
97
  this._enum = _enum;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dbml/core",
3
- "version": "3.13.7-alpha.0",
3
+ "version": "3.13.7",
4
4
  "description": "> TODO: description",
5
5
  "author": "Holistics <dev@holistics.io>",
6
6
  "license": "Apache-2.0",
@@ -32,7 +32,7 @@
32
32
  "prepublish": "npm run build"
33
33
  },
34
34
  "dependencies": {
35
- "@dbml/parse": "^3.13.7-alpha.0",
35
+ "@dbml/parse": "^3.13.7",
36
36
  "antlr4": "^4.13.1",
37
37
  "lodash": "^4.17.15",
38
38
  "parsimmon": "^1.13.0",
@@ -57,12 +57,9 @@
57
57
  "transform": {
58
58
  "^.+\\.js$": "babel-jest",
59
59
  "\\.(?!json$)[^.]*$": "@glen/jest-raw-loader"
60
- },
61
- "moduleNameMapper": {
62
- "^lodash-es$": "lodash"
63
60
  }
64
61
  },
65
- "gitHead": "b75ba3ddb06e870cf797392398c6037c03904284",
62
+ "gitHead": "a49ead2bd55f99082954af631e386423f1fe8506",
66
63
  "engines": {
67
64
  "node": ">=16"
68
65
  }