@dbml/core 2.2.0 → 2.4.0

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.
Files changed (93) hide show
  1. package/lib/export/DbmlExporter.js +15 -4
  2. package/lib/export/JsonExporter.js +1 -3
  3. package/lib/export/ModelExporter.js +1 -3
  4. package/lib/export/MysqlExporter.js +50 -42
  5. package/lib/export/PostgresExporter.js +64 -49
  6. package/lib/export/SqlServerExporter.js +52 -46
  7. package/lib/model_structure/database.js +73 -28
  8. package/lib/model_structure/dbState.js +1 -3
  9. package/lib/model_structure/element.js +13 -13
  10. package/lib/model_structure/endpoint.js +18 -14
  11. package/lib/model_structure/enum.js +18 -14
  12. package/lib/model_structure/enumValue.js +16 -12
  13. package/lib/model_structure/field.js +47 -13
  14. package/lib/model_structure/indexColumn.js +16 -12
  15. package/lib/model_structure/indexes.js +18 -14
  16. package/lib/model_structure/ref.js +19 -16
  17. package/lib/model_structure/schema.js +24 -36
  18. package/lib/model_structure/table.js +19 -15
  19. package/lib/model_structure/tableGroup.js +18 -14
  20. package/lib/model_structure/utils.js +5 -0
  21. package/lib/parse/Parser.js +2 -4
  22. package/lib/parse/buildParser.js +1 -3
  23. package/lib/parse/dbml/parser.pegjs +64 -20
  24. package/lib/parse/dbmlParser.js +1401 -899
  25. package/lib/parse/mssql/constraint_definition/actions.js +2 -2
  26. package/lib/parse/mssql/fk_definition/actions.js +10 -3
  27. package/lib/parse/mssql/keyword_parsers.js +0 -1
  28. package/lib/parse/mssql/statements/actions.js +9 -6
  29. package/lib/parse/mssql/statements/statement_types/alter_table/actions.js +11 -5
  30. package/lib/parse/mssql/statements/statement_types/create_index/actions.js +6 -1
  31. package/lib/parse/mssql/statements/statement_types/create_table/actions.js +12 -10
  32. package/lib/parse/mssql/utils.js +16 -1
  33. package/lib/parse/mysql/parser.pegjs +57 -34
  34. package/lib/parse/mysqlParser.js +270 -218
  35. package/lib/parse/postgresParser.js +12 -10
  36. package/lib/parse/postgresql/Base_rules.pegjs +45 -10
  37. package/lib/parse/postgresql/Commands/Alter_table/Alter_table.pegjs +2 -1
  38. package/lib/parse/postgresql/Commands/Create_table/Create_table_normal.pegjs +5 -3
  39. package/lib/parse/postgresql/Commands/Create_table/Create_table_of.pegjs +1 -1
  40. package/lib/parse/postgresql/Commands/Create_table/Create_table_partition_of.pegjs +1 -1
  41. package/lib/parse/postgresql/Commands/Create_type/Create_type_enum.pegjs +2 -2
  42. package/lib/parse/postgresql/InitializerUtils.pegjs +10 -0
  43. package/lib/parse/postgresql/parser.pegjs +2 -1
  44. package/lib/parse/schemarbParser.js +3 -3
  45. package/package.json +5 -3
  46. package/types/export/ModelExporter.d.ts +5 -0
  47. package/types/export/index.d.ts +5 -0
  48. package/types/import/index.d.ts +5 -0
  49. package/types/index.d.ts +5 -0
  50. package/types/model_structure/database.d.ts +195 -0
  51. package/types/model_structure/dbState.d.ts +14 -0
  52. package/types/model_structure/element.d.ts +21 -0
  53. package/types/model_structure/endpoint.d.ts +55 -0
  54. package/types/model_structure/enum.d.ts +67 -0
  55. package/types/model_structure/enumValue.d.ts +39 -0
  56. package/types/model_structure/field.d.ts +77 -0
  57. package/types/model_structure/indexColumn.d.ts +37 -0
  58. package/types/model_structure/indexes.d.ts +74 -0
  59. package/types/model_structure/ref.d.ts +66 -0
  60. package/types/model_structure/schema.d.ts +188 -0
  61. package/types/model_structure/table.d.ts +118 -0
  62. package/types/model_structure/tableGroup.d.ts +54 -0
  63. package/types/parse/Parser.d.ts +11 -0
  64. package/lib/export/Exporter.js +0 -80
  65. package/lib/export/SchemaExporter.js +0 -99
  66. package/lib/parse/dbml/multiline_content.txt +0 -7
  67. package/lib/parse/dbml/multiline_string.pegjs +0 -17
  68. package/lib/parse/dbml/parser1.json +0 -219
  69. package/lib/parse/dbml/parser1.pegjs +0 -702
  70. package/lib/parse/dbml/parser1_content.txt +0 -29
  71. package/lib/parse/dbml/test_multiline.js +0 -53
  72. package/lib/parse/dbml/test_parser1.js +0 -16
  73. package/lib/parse/mssql/base_utils.js +0 -28
  74. package/lib/parse/mssql/composite_parsers.js +0 -109
  75. package/lib/parse/mssql/statement_types/alter_table/actions.js +0 -29
  76. package/lib/parse/mssql/statement_types/alter_table/add/actions.js +0 -8
  77. package/lib/parse/mssql/statement_types/alter_table/add/index.js +0 -58
  78. package/lib/parse/mssql/statement_types/alter_table/index.js +0 -32
  79. package/lib/parse/mssql/statement_types/create_index/actions.js +0 -25
  80. package/lib/parse/mssql/statement_types/create_index/index.js +0 -50
  81. package/lib/parse/mssql/statement_types/create_table/actions.js +0 -95
  82. package/lib/parse/mssql/statement_types/create_table/index.js +0 -50
  83. package/lib/parse/mssql/statement_types/index.js +0 -13
  84. package/lib/schema/element.js +0 -84
  85. package/lib/schema/endpoint.js +0 -102
  86. package/lib/schema/enum.js +0 -102
  87. package/lib/schema/enumValue.js +0 -70
  88. package/lib/schema/field.js +0 -104
  89. package/lib/schema/indexes.js +0 -74
  90. package/lib/schema/ref.js +0 -93
  91. package/lib/schema/schema.js +0 -245
  92. package/lib/schema/table.js +0 -163
  93. package/lib/schema/tableGroup.js +0 -93
@@ -9,6 +9,8 @@ var _lodash = _interopRequireDefault(require("lodash"));
9
9
 
10
10
  var _utils = require("./utils");
11
11
 
12
+ var _config = require("../model_structure/config");
13
+
12
14
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
13
15
 
14
16
  function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
@@ -17,9 +19,7 @@ function _defineProperties(target, props) { for (var i = 0; i < props.length; i+
17
19
 
18
20
  function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
19
21
 
20
- var DbmlExporter =
21
- /*#__PURE__*/
22
- function () {
22
+ var DbmlExporter = /*#__PURE__*/function () {
23
23
  function DbmlExporter() {
24
24
  _classCallCheck(this, DbmlExporter);
25
25
  }
@@ -29,6 +29,11 @@ function () {
29
29
  value: function hasWhiteSpace(str) {
30
30
  return /\s/g.test(str);
31
31
  }
32
+ }, {
33
+ key: "hasSquareBracket",
34
+ value: function hasSquareBracket(str) {
35
+ return /\[|\]/.test(str);
36
+ }
32
37
  }, {
33
38
  key: "isExpression",
34
39
  value: function isExpression(str) {
@@ -52,7 +57,13 @@ function () {
52
57
  var table = model.tables[tableId];
53
58
  var lines = table.fieldIds.map(function (fieldId) {
54
59
  var field = model.fields[fieldId];
55
- var line = "\"".concat(field.name, "\" ").concat(DbmlExporter.hasWhiteSpace(field.type.type_name) ? "\"".concat(field.type.type_name, "\"") : field.type.type_name);
60
+ var schemaName = '';
61
+
62
+ if (field.type.schemaName && field.type.schemaName !== _config.DEFAULT_SCHEMA_NAME) {
63
+ schemaName = DbmlExporter.hasWhiteSpace(field.type.schemaName) ? "\"".concat(field.type.schemaName, "\".") : "".concat(field.type.schemaName, ".");
64
+ }
65
+
66
+ var line = "\"".concat(field.name, "\" ").concat(schemaName).concat(DbmlExporter.hasWhiteSpace(field.type.type_name) || DbmlExporter.hasSquareBracket(field.type.type_name) ? "\"".concat(field.type.type_name, "\"") : field.type.type_name);
56
67
  var constraints = [];
57
68
 
58
69
  if (field.unique) {
@@ -11,9 +11,7 @@ function _defineProperties(target, props) { for (var i = 0; i < props.length; i+
11
11
 
12
12
  function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
13
13
 
14
- var JsonExporter =
15
- /*#__PURE__*/
16
- function () {
14
+ var JsonExporter = /*#__PURE__*/function () {
17
15
  function JsonExporter() {
18
16
  _classCallCheck(this, JsonExporter);
19
17
  }
@@ -23,9 +23,7 @@ function _defineProperties(target, props) { for (var i = 0; i < props.length; i+
23
23
 
24
24
  function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
25
25
 
26
- var ModelExporter =
27
- /*#__PURE__*/
28
- function () {
26
+ var ModelExporter = /*#__PURE__*/function () {
29
27
  function ModelExporter() {
30
28
  _classCallCheck(this, ModelExporter);
31
29
  }
@@ -11,13 +11,17 @@ var _utils = require("./utils");
11
11
 
12
12
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
13
13
 
14
- function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); }
14
+ function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
15
15
 
16
- function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance"); }
16
+ 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."); }
17
17
 
18
- function _iterableToArray(iter) { if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter); }
18
+ function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
19
19
 
20
- function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } }
20
+ function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && Symbol.iterator in Object(iter)) return Array.from(iter); }
21
+
22
+ function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
23
+
24
+ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
21
25
 
22
26
  function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
23
27
 
@@ -25,9 +29,7 @@ function _defineProperties(target, props) { for (var i = 0; i < props.length; i+
25
29
 
26
30
  function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
27
31
 
28
- var MySQLExporter =
29
- /*#__PURE__*/
30
- function () {
32
+ var MySQLExporter = /*#__PURE__*/function () {
31
33
  function MySQLExporter() {
32
34
  _classCallCheck(this, MySQLExporter);
33
35
  }
@@ -142,7 +144,7 @@ function () {
142
144
  }).join(',\n'), "\n);\n");
143
145
  return tableStr;
144
146
  });
145
- return tableStrs.length ? tableStrs.join('\n') : '';
147
+ return tableStrs;
146
148
  }
147
149
  }, {
148
150
  key: "buildFieldName",
@@ -197,7 +199,7 @@ function () {
197
199
  line += ';\n';
198
200
  return line;
199
201
  });
200
- return strArr.length ? strArr.join('\n') : '';
202
+ return strArr;
201
203
  }
202
204
  }, {
203
205
  key: "exportIndexes",
@@ -239,7 +241,7 @@ function () {
239
241
  line += ';\n';
240
242
  return line;
241
243
  });
242
- return indexArr.length ? indexArr.join('\n') : '';
244
+ return indexArr;
243
245
  }
244
246
  }, {
245
247
  key: "exportComments",
@@ -255,62 +257,68 @@ function () {
255
257
  line += ';\n';
256
258
  return line;
257
259
  });
258
- return commentArr.length ? commentArr.join('\n') : '';
260
+ return commentArr;
259
261
  }
260
262
  }, {
261
263
  key: "export",
262
264
  value: function _export(model) {
263
- var res = '';
264
- var hasBlockAbove = false;
265
265
  var database = model.database['1'];
266
- var indexIds = [];
267
- var comments = [];
268
- database.schemaIds.forEach(function (schemaId) {
266
+ var statements = database.schemaIds.reduce(function (prevStatements, schemaId) {
269
267
  var schema = model.schemas[schemaId];
270
268
  var tableIds = schema.tableIds,
271
269
  refIds = schema.refIds;
272
270
 
273
271
  if ((0, _utils.shouldPrintSchema)(schema, model)) {
274
- if (hasBlockAbove) res += '\n';
275
- res += "CREATE DATABASE `".concat(schema.name, "`;\n");
276
- hasBlockAbove = true;
272
+ prevStatements.schemas.push("CREATE SCHEMA `".concat(schema.name, "`;\n"));
277
273
  }
278
274
 
279
275
  if (!_lodash["default"].isEmpty(tableIds)) {
280
- if (hasBlockAbove) res += '\n';
281
- res += MySQLExporter.exportTables(tableIds, model);
282
- hasBlockAbove = true;
283
- }
276
+ var _prevStatements$table;
284
277
 
285
- if (!_lodash["default"].isEmpty(refIds)) {
286
- if (hasBlockAbove) res += '\n';
287
- res += MySQLExporter.exportRefs(refIds, model);
288
- hasBlockAbove = true;
278
+ (_prevStatements$table = prevStatements.tables).push.apply(_prevStatements$table, _toConsumableArray(MySQLExporter.exportTables(tableIds, model)));
289
279
  }
290
280
 
291
- indexIds.push.apply(indexIds, _toConsumableArray(_lodash["default"].flatten(tableIds.map(function (tableId) {
281
+ var indexIds = _lodash["default"].flatten(tableIds.map(function (tableId) {
292
282
  return model.tables[tableId].indexIds;
293
- }))));
294
- comments.push.apply(comments, _toConsumableArray(_lodash["default"].flatten(tableIds.map(function (tableId) {
283
+ }));
284
+
285
+ if (!_lodash["default"].isEmpty(indexIds)) {
286
+ var _prevStatements$index;
287
+
288
+ (_prevStatements$index = prevStatements.indexes).push.apply(_prevStatements$index, _toConsumableArray(MySQLExporter.exportIndexes(indexIds, model)));
289
+ }
290
+
291
+ var commentNodes = _lodash["default"].flatten(tableIds.map(function (tableId) {
295
292
  var note = model.tables[tableId].note;
296
293
  return note ? [{
297
294
  type: 'table',
298
295
  tableId: tableId
299
296
  }] : [];
300
- }))));
297
+ }));
298
+
299
+ if (!_lodash["default"].isEmpty(commentNodes)) {
300
+ var _prevStatements$comme;
301
+
302
+ (_prevStatements$comme = prevStatements.comments).push.apply(_prevStatements$comme, _toConsumableArray(MySQLExporter.exportComments(commentNodes, model)));
303
+ }
304
+
305
+ if (!_lodash["default"].isEmpty(refIds)) {
306
+ var _prevStatements$refs;
307
+
308
+ (_prevStatements$refs = prevStatements.refs).push.apply(_prevStatements$refs, _toConsumableArray(MySQLExporter.exportRefs(refIds, model)));
309
+ }
310
+
311
+ return prevStatements;
312
+ }, {
313
+ schemas: [],
314
+ enums: [],
315
+ tables: [],
316
+ indexes: [],
317
+ comments: [],
318
+ refs: []
301
319
  });
302
320
 
303
- if (!_lodash["default"].isEmpty(indexIds)) {
304
- if (hasBlockAbove) res += '\n';
305
- res += MySQLExporter.exportIndexes(indexIds, model);
306
- hasBlockAbove = true;
307
- }
308
-
309
- if (!_lodash["default"].isEmpty(comments)) {
310
- if (hasBlockAbove) res += '\n';
311
- res += MySQLExporter.exportComments(comments, model);
312
- hasBlockAbove = true;
313
- }
321
+ var res = _lodash["default"].concat(statements.schemas, statements.enums, statements.tables, statements.indexes, statements.comments, statements.refs).join('\n');
314
322
 
315
323
  return res;
316
324
  }
@@ -9,15 +9,21 @@ var _lodash = _interopRequireDefault(require("lodash"));
9
9
 
10
10
  var _utils = require("./utils");
11
11
 
12
+ var _config = require("../model_structure/config");
13
+
12
14
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
13
15
 
14
- function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); }
16
+ function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
17
+
18
+ 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."); }
19
+
20
+ function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
15
21
 
16
- function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance"); }
22
+ function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && Symbol.iterator in Object(iter)) return Array.from(iter); }
17
23
 
18
- function _iterableToArray(iter) { if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter); }
24
+ function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
19
25
 
20
- function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } }
26
+ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
21
27
 
22
28
  function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
23
29
 
@@ -25,9 +31,7 @@ function _defineProperties(target, props) { for (var i = 0; i < props.length; i+
25
31
 
26
32
  function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
27
33
 
28
- var PostgresExporter =
29
- /*#__PURE__*/
30
- function () {
34
+ var PostgresExporter = /*#__PURE__*/function () {
31
35
  function PostgresExporter() {
32
36
  _classCallCheck(this, PostgresExporter);
33
37
  }
@@ -48,7 +52,7 @@ function () {
48
52
  var line = "CREATE TYPE ".concat((0, _utils.shouldPrintSchema)(schema, model) ? "\"".concat(schema.name, "\".") : '', "\"").concat(_enum.name, "\" AS ENUM (\n").concat(enumValueStr, "\n);\n");
49
53
  return line;
50
54
  });
51
- return enumArr.length ? enumArr.join('\n') : '';
55
+ return enumArr;
52
56
  }
53
57
  }, {
54
58
  key: "getFieldLines",
@@ -61,10 +65,15 @@ function () {
61
65
  if (field.increment) {
62
66
  var typeSerial = field.type.type_name === 'bigint' ? 'BIGSERIAL' : 'SERIAL';
63
67
  line = "\"".concat(field.name, "\" ").concat(typeSerial);
64
- } else if ((0, _utils.hasWhiteSpace)(field.type.type_name)) {
65
- line = "\"".concat(field.name, "\" \"").concat(field.type.type_name, "\"");
66
68
  } else {
67
- line = "\"".concat(field.name, "\" ").concat(field.type.type_name);
69
+ var schemaName = '';
70
+
71
+ if (field.type.schemaName && field.type.schemaName !== _config.DEFAULT_SCHEMA_NAME) {
72
+ schemaName = (0, _utils.hasWhiteSpace)(field.type.schemaName) ? "\"".concat(field.type.schemaName, "\".") : "".concat(field.type.schemaName, ".");
73
+ }
74
+
75
+ var typeName = (0, _utils.hasWhiteSpace)(field.type.type_name) ? "\"".concat(field.type.type_name, "\"") : field.type.type_name;
76
+ line = "\"".concat(field.name, "\" ").concat(schemaName).concat(typeName);
68
77
  }
69
78
 
70
79
  if (field.unique) {
@@ -148,7 +157,7 @@ function () {
148
157
  }).join(',\n'), "\n);\n");
149
158
  return tableStr;
150
159
  });
151
- return tableStrs.length ? tableStrs.join('\n') : '';
160
+ return tableStrs;
152
161
  }
153
162
  }, {
154
163
  key: "buildFieldName",
@@ -203,7 +212,7 @@ function () {
203
212
  line += ';\n';
204
213
  return line;
205
214
  });
206
- return strArr.length ? strArr.join('\n') : '';
215
+ return strArr;
207
216
  }
208
217
  }, {
209
218
  key: "exportIndexes",
@@ -251,7 +260,7 @@ function () {
251
260
  line += ';\n';
252
261
  return line;
253
262
  });
254
- return indexArr.length ? indexArr.join('\n') : '';
263
+ return indexArr;
255
264
  }
256
265
  }, {
257
266
  key: "exportComments",
@@ -282,50 +291,45 @@ function () {
282
291
  line += ';\n';
283
292
  return line;
284
293
  });
285
- return commentArr.length ? commentArr.join('\n') : '';
294
+ return commentArr;
286
295
  }
287
296
  }, {
288
297
  key: "export",
289
298
  value: function _export(model) {
290
- var res = '';
291
- var hasBlockAbove = false;
292
299
  var database = model.database['1'];
293
- var indexIds = [];
294
- var comments = [];
295
- database.schemaIds.forEach(function (schemaId) {
300
+ var statements = database.schemaIds.reduce(function (prevStatements, schemaId) {
296
301
  var schema = model.schemas[schemaId];
297
302
  var tableIds = schema.tableIds,
298
303
  enumIds = schema.enumIds,
299
304
  refIds = schema.refIds;
300
305
 
301
306
  if ((0, _utils.shouldPrintSchema)(schema, model)) {
302
- if (hasBlockAbove) res += '\n';
303
- res += "CREATE SCHEMA \"".concat(schema.name, "\";\n");
304
- hasBlockAbove = true;
307
+ prevStatements.schemas.push("CREATE SCHEMA \"".concat(schema.name, "\";\n"));
305
308
  }
306
309
 
307
310
  if (!_lodash["default"].isEmpty(enumIds)) {
308
- if (hasBlockAbove) res += '\n';
309
- res += PostgresExporter.exportEnums(enumIds, model);
310
- hasBlockAbove = true;
311
+ var _prevStatements$enums;
312
+
313
+ (_prevStatements$enums = prevStatements.enums).push.apply(_prevStatements$enums, _toConsumableArray(PostgresExporter.exportEnums(enumIds, model)));
311
314
  }
312
315
 
313
316
  if (!_lodash["default"].isEmpty(tableIds)) {
314
- if (hasBlockAbove) res += '\n';
315
- res += PostgresExporter.exportTables(tableIds, model);
316
- hasBlockAbove = true;
317
- }
317
+ var _prevStatements$table;
318
318
 
319
- if (!_lodash["default"].isEmpty(refIds)) {
320
- if (hasBlockAbove) res += '\n';
321
- res += PostgresExporter.exportRefs(refIds, model);
322
- hasBlockAbove = true;
319
+ (_prevStatements$table = prevStatements.tables).push.apply(_prevStatements$table, _toConsumableArray(PostgresExporter.exportTables(tableIds, model)));
323
320
  }
324
321
 
325
- indexIds.push.apply(indexIds, _toConsumableArray(_lodash["default"].flatten(tableIds.map(function (tableId) {
322
+ var indexIds = _lodash["default"].flatten(tableIds.map(function (tableId) {
326
323
  return model.tables[tableId].indexIds;
327
- }))));
328
- comments.push.apply(comments, _toConsumableArray(_lodash["default"].flatten(tableIds.map(function (tableId) {
324
+ }));
325
+
326
+ if (!_lodash["default"].isEmpty(indexIds)) {
327
+ var _prevStatements$index;
328
+
329
+ (_prevStatements$index = prevStatements.indexes).push.apply(_prevStatements$index, _toConsumableArray(PostgresExporter.exportIndexes(indexIds, model)));
330
+ }
331
+
332
+ var commentNodes = _lodash["default"].flatten(tableIds.map(function (tableId) {
329
333
  var _model$tables$tableId = model.tables[tableId],
330
334
  fieldIds = _model$tables$tableId.fieldIds,
331
335
  note = _model$tables$tableId.note;
@@ -342,20 +346,31 @@ function () {
342
346
  type: 'table',
343
347
  tableId: tableId
344
348
  }].concat(fieldObjects) : fieldObjects;
345
- }))));
349
+ }));
350
+
351
+ if (!_lodash["default"].isEmpty(commentNodes)) {
352
+ var _prevStatements$comme;
353
+
354
+ (_prevStatements$comme = prevStatements.comments).push.apply(_prevStatements$comme, _toConsumableArray(PostgresExporter.exportComments(commentNodes, model)));
355
+ }
356
+
357
+ if (!_lodash["default"].isEmpty(refIds)) {
358
+ var _prevStatements$refs;
359
+
360
+ (_prevStatements$refs = prevStatements.refs).push.apply(_prevStatements$refs, _toConsumableArray(PostgresExporter.exportRefs(refIds, model)));
361
+ }
362
+
363
+ return prevStatements;
364
+ }, {
365
+ schemas: [],
366
+ enums: [],
367
+ tables: [],
368
+ indexes: [],
369
+ comments: [],
370
+ refs: []
346
371
  });
347
372
 
348
- if (!_lodash["default"].isEmpty(indexIds)) {
349
- if (hasBlockAbove) res += '\n';
350
- res += PostgresExporter.exportIndexes(indexIds, model);
351
- hasBlockAbove = true;
352
- }
353
-
354
- if (!_lodash["default"].isEmpty(comments)) {
355
- if (hasBlockAbove) res += '\n';
356
- res += PostgresExporter.exportComments(comments, model);
357
- hasBlockAbove = true;
358
- }
373
+ var res = _lodash["default"].concat(statements.schemas, statements.enums, statements.tables, statements.indexes, statements.comments, statements.refs).join('\n');
359
374
 
360
375
  return res;
361
376
  }
@@ -11,13 +11,17 @@ var _utils = require("./utils");
11
11
 
12
12
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
13
13
 
14
- function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); }
14
+ function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
15
15
 
16
- function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance"); }
16
+ 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."); }
17
17
 
18
- function _iterableToArray(iter) { if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter); }
18
+ function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
19
19
 
20
- function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } }
20
+ function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && Symbol.iterator in Object(iter)) return Array.from(iter); }
21
+
22
+ function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
23
+
24
+ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
21
25
 
22
26
  function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
23
27
 
@@ -25,9 +29,7 @@ function _defineProperties(target, props) { for (var i = 0; i < props.length; i+
25
29
 
26
30
  function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
27
31
 
28
- var SqlServerExporter =
29
- /*#__PURE__*/
30
- function () {
32
+ var SqlServerExporter = /*#__PURE__*/function () {
31
33
  function SqlServerExporter() {
32
34
  _classCallCheck(this, SqlServerExporter);
33
35
  }
@@ -139,7 +141,7 @@ function () {
139
141
  }).join(',\n'), "\n)\nGO\n");
140
142
  return tableStr;
141
143
  });
142
- return tableStrs.length ? tableStrs.join('\n') : '';
144
+ return tableStrs;
143
145
  }
144
146
  }, {
145
147
  key: "buildFieldName",
@@ -194,7 +196,7 @@ function () {
194
196
  line += '\nGO\n';
195
197
  return line;
196
198
  });
197
- return strArr.length ? strArr.join('\n') : '';
199
+ return strArr;
198
200
  }
199
201
  }, {
200
202
  key: "exportIndexes",
@@ -231,7 +233,7 @@ function () {
231
233
  line += '\nGO\n';
232
234
  return line;
233
235
  });
234
- return indexArr.length ? indexArr.join('\n') : '';
236
+ return indexArr;
235
237
  }
236
238
  }, {
237
239
  key: "exportComments",
@@ -270,49 +272,42 @@ function () {
270
272
  line += 'GO\n';
271
273
  return line;
272
274
  });
273
- return commentArr.length ? commentArr.join('\n') : '';
275
+ return commentArr;
274
276
  }
275
277
  }, {
276
278
  key: "export",
277
279
  value: function _export(model) {
278
- var res = '';
279
- var hasBlockAbove = false;
280
280
  var database = model.database['1'];
281
- var indexIds = [];
282
- var comments = [];
283
- database.schemaIds.forEach(function (schemaId) {
281
+ var statements = database.schemaIds.reduce(function (prevStatements, schemaId) {
284
282
  var schema = model.schemas[schemaId];
285
283
  var tableIds = schema.tableIds,
286
284
  refIds = schema.refIds;
287
285
 
288
286
  if ((0, _utils.shouldPrintSchema)(schema, model)) {
289
- if (hasBlockAbove) res += '\n';
290
- res += "CREATE SCHEMA [".concat(schema.name, "];\nGO\n");
291
- hasBlockAbove = true;
287
+ prevStatements.schemas.push("CREATE SCHEMA [".concat(schema.name, "]\nGO\n"));
292
288
  }
293
289
 
294
290
  if (!_lodash["default"].isEmpty(tableIds)) {
295
- if (hasBlockAbove) res += '\n';
296
- res += SqlServerExporter.exportTables(tableIds, model);
297
- hasBlockAbove = true;
291
+ var _prevStatements$table;
292
+
293
+ (_prevStatements$table = prevStatements.tables).push.apply(_prevStatements$table, _toConsumableArray(SqlServerExporter.exportTables(tableIds, model)));
298
294
  }
299
295
 
300
- if (!_lodash["default"].isEmpty(refIds)) {
301
- if (hasBlockAbove) res += '\n';
302
- res += SqlServerExporter.exportRefs(refIds, model);
303
- hasBlockAbove = true;
304
- } /////////PUSH COMMENTS OF TABLE & COLUMN/////////
305
- // console.log(JSON.stringify(tableIds, null, 2));
296
+ var indexIds = _lodash["default"].flatten(tableIds.map(function (tableId) {
297
+ return model.tables[tableId].indexIds;
298
+ }));
306
299
 
300
+ if (!_lodash["default"].isEmpty(indexIds)) {
301
+ var _prevStatements$index;
307
302
 
308
- indexIds.push.apply(indexIds, _toConsumableArray(_lodash["default"].flatten(tableIds.map(function (tableId) {
309
- return model.tables[tableId].indexIds;
310
- }))));
311
- comments.push.apply(comments, _toConsumableArray(_lodash["default"].flatten(tableIds.map(function (tableId) {
303
+ (_prevStatements$index = prevStatements.indexes).push.apply(_prevStatements$index, _toConsumableArray(SqlServerExporter.exportIndexes(indexIds, model)));
304
+ }
305
+
306
+ var commentNodes = _lodash["default"].flatten(tableIds.map(function (tableId) {
312
307
  var _model$tables$tableId = model.tables[tableId],
313
308
  fieldIds = _model$tables$tableId.fieldIds,
314
309
  note = _model$tables$tableId.note;
315
- var fieldObject = fieldIds.filter(function (fieldId) {
310
+ var fieldObjects = fieldIds.filter(function (fieldId) {
316
311
  return model.fields[fieldId].note;
317
312
  }).map(function (fieldId) {
318
313
  return {
@@ -324,21 +319,32 @@ function () {
324
319
  return note ? [{
325
320
  type: 'table',
326
321
  tableId: tableId
327
- }].concat(fieldObject) : fieldObject;
328
- }))));
322
+ }].concat(fieldObjects) : fieldObjects;
323
+ }));
324
+
325
+ if (!_lodash["default"].isEmpty(commentNodes)) {
326
+ var _prevStatements$comme;
327
+
328
+ (_prevStatements$comme = prevStatements.comments).push.apply(_prevStatements$comme, _toConsumableArray(SqlServerExporter.exportComments(commentNodes, model)));
329
+ }
330
+
331
+ if (!_lodash["default"].isEmpty(refIds)) {
332
+ var _prevStatements$refs;
333
+
334
+ (_prevStatements$refs = prevStatements.refs).push.apply(_prevStatements$refs, _toConsumableArray(SqlServerExporter.exportRefs(refIds, model)));
335
+ }
336
+
337
+ return prevStatements;
338
+ }, {
339
+ schemas: [],
340
+ enums: [],
341
+ tables: [],
342
+ indexes: [],
343
+ comments: [],
344
+ refs: []
329
345
  });
330
346
 
331
- if (!_lodash["default"].isEmpty(indexIds)) {
332
- if (hasBlockAbove) res += '\n';
333
- res += SqlServerExporter.exportIndexes(indexIds, model);
334
- hasBlockAbove = true;
335
- }
336
-
337
- if (!_lodash["default"].isEmpty(comments)) {
338
- if (hasBlockAbove) res += '\n';
339
- res += SqlServerExporter.exportComments(comments, model);
340
- hasBlockAbove = true;
341
- }
347
+ var res = _lodash["default"].concat(statements.schemas, statements.enums, statements.tables, statements.indexes, statements.comments, statements.refs).join('\n');
342
348
 
343
349
  return res;
344
350
  }