@dbml/core 2.3.1 → 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 (31) hide show
  1. package/lib/export/DbmlExporter.js +9 -1
  2. package/lib/export/MysqlExporter.js +41 -35
  3. package/lib/export/PostgresExporter.js +55 -42
  4. package/lib/export/SqlServerExporter.js +43 -39
  5. package/lib/model_structure/database.js +53 -12
  6. package/lib/model_structure/endpoint.js +2 -2
  7. package/lib/model_structure/field.js +31 -1
  8. package/lib/model_structure/ref.js +1 -2
  9. package/lib/model_structure/schema.js +3 -19
  10. package/lib/model_structure/tableGroup.js +1 -1
  11. package/lib/model_structure/utils.js +5 -0
  12. package/lib/parse/dbml/parser.pegjs +61 -19
  13. package/lib/parse/dbmlParser.js +1219 -883
  14. package/lib/parse/mssql/fk_definition/actions.js +10 -3
  15. package/lib/parse/mssql/statements/actions.js +9 -6
  16. package/lib/parse/mssql/statements/statement_types/alter_table/actions.js +11 -5
  17. package/lib/parse/mssql/statements/statement_types/create_index/actions.js +6 -1
  18. package/lib/parse/mssql/statements/statement_types/create_table/actions.js +11 -9
  19. package/lib/parse/mssql/utils.js +15 -0
  20. package/lib/parse/mysql/parser.pegjs +46 -14
  21. package/lib/parse/mysqlParser.js +215 -167
  22. package/lib/parse/postgresParser.js +11 -10
  23. package/lib/parse/postgresql/Base_rules.pegjs +24 -3
  24. package/lib/parse/postgresql/Commands/Alter_table/Alter_table.pegjs +2 -1
  25. package/lib/parse/postgresql/Commands/Create_table/Create_table_normal.pegjs +5 -3
  26. package/lib/parse/postgresql/Commands/Create_table/Create_table_of.pegjs +1 -1
  27. package/lib/parse/postgresql/Commands/Create_table/Create_table_partition_of.pegjs +1 -1
  28. package/lib/parse/postgresql/Commands/Create_type/Create_type_enum.pegjs +2 -2
  29. package/lib/parse/postgresql/InitializerUtils.pegjs +10 -0
  30. package/lib/parse/postgresql/parser.pegjs +2 -1
  31. package/package.json +2 -2
@@ -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"); } }
@@ -55,7 +57,13 @@ var DbmlExporter = /*#__PURE__*/function () {
55
57
  var table = model.tables[tableId];
56
58
  var lines = table.fieldIds.map(function (fieldId) {
57
59
  var field = model.fields[fieldId];
58
- var line = "\"".concat(field.name, "\" ").concat(DbmlExporter.hasWhiteSpace(field.type.type_name) || DbmlExporter.hasSquareBracket(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);
59
67
  var constraints = [];
60
68
 
61
69
  if (field.unique) {
@@ -144,7 +144,7 @@ var MySQLExporter = /*#__PURE__*/function () {
144
144
  }).join(',\n'), "\n);\n");
145
145
  return tableStr;
146
146
  });
147
- return tableStrs.length ? tableStrs.join('\n') : '';
147
+ return tableStrs;
148
148
  }
149
149
  }, {
150
150
  key: "buildFieldName",
@@ -199,7 +199,7 @@ var MySQLExporter = /*#__PURE__*/function () {
199
199
  line += ';\n';
200
200
  return line;
201
201
  });
202
- return strArr.length ? strArr.join('\n') : '';
202
+ return strArr;
203
203
  }
204
204
  }, {
205
205
  key: "exportIndexes",
@@ -241,7 +241,7 @@ var MySQLExporter = /*#__PURE__*/function () {
241
241
  line += ';\n';
242
242
  return line;
243
243
  });
244
- return indexArr.length ? indexArr.join('\n') : '';
244
+ return indexArr;
245
245
  }
246
246
  }, {
247
247
  key: "exportComments",
@@ -257,62 +257,68 @@ var MySQLExporter = /*#__PURE__*/function () {
257
257
  line += ';\n';
258
258
  return line;
259
259
  });
260
- return commentArr.length ? commentArr.join('\n') : '';
260
+ return commentArr;
261
261
  }
262
262
  }, {
263
263
  key: "export",
264
264
  value: function _export(model) {
265
- var res = '';
266
- var hasBlockAbove = false;
267
265
  var database = model.database['1'];
268
- var indexIds = [];
269
- var comments = [];
270
- database.schemaIds.forEach(function (schemaId) {
266
+ var statements = database.schemaIds.reduce(function (prevStatements, schemaId) {
271
267
  var schema = model.schemas[schemaId];
272
268
  var tableIds = schema.tableIds,
273
269
  refIds = schema.refIds;
274
270
 
275
271
  if ((0, _utils.shouldPrintSchema)(schema, model)) {
276
- if (hasBlockAbove) res += '\n';
277
- res += "CREATE DATABASE `".concat(schema.name, "`;\n");
278
- hasBlockAbove = true;
272
+ prevStatements.schemas.push("CREATE SCHEMA `".concat(schema.name, "`;\n"));
279
273
  }
280
274
 
281
275
  if (!_lodash["default"].isEmpty(tableIds)) {
282
- if (hasBlockAbove) res += '\n';
283
- res += MySQLExporter.exportTables(tableIds, model);
284
- hasBlockAbove = true;
285
- }
276
+ var _prevStatements$table;
286
277
 
287
- if (!_lodash["default"].isEmpty(refIds)) {
288
- if (hasBlockAbove) res += '\n';
289
- res += MySQLExporter.exportRefs(refIds, model);
290
- hasBlockAbove = true;
278
+ (_prevStatements$table = prevStatements.tables).push.apply(_prevStatements$table, _toConsumableArray(MySQLExporter.exportTables(tableIds, model)));
291
279
  }
292
280
 
293
- indexIds.push.apply(indexIds, _toConsumableArray(_lodash["default"].flatten(tableIds.map(function (tableId) {
281
+ var indexIds = _lodash["default"].flatten(tableIds.map(function (tableId) {
294
282
  return model.tables[tableId].indexIds;
295
- }))));
296
- 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) {
297
292
  var note = model.tables[tableId].note;
298
293
  return note ? [{
299
294
  type: 'table',
300
295
  tableId: tableId
301
296
  }] : [];
302
- }))));
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: []
303
319
  });
304
320
 
305
- if (!_lodash["default"].isEmpty(indexIds)) {
306
- if (hasBlockAbove) res += '\n';
307
- res += MySQLExporter.exportIndexes(indexIds, model);
308
- hasBlockAbove = true;
309
- }
310
-
311
- if (!_lodash["default"].isEmpty(comments)) {
312
- if (hasBlockAbove) res += '\n';
313
- res += MySQLExporter.exportComments(comments, model);
314
- hasBlockAbove = true;
315
- }
321
+ var res = _lodash["default"].concat(statements.schemas, statements.enums, statements.tables, statements.indexes, statements.comments, statements.refs).join('\n');
316
322
 
317
323
  return res;
318
324
  }
@@ -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 _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
@@ -50,7 +52,7 @@ var PostgresExporter = /*#__PURE__*/function () {
50
52
  var line = "CREATE TYPE ".concat((0, _utils.shouldPrintSchema)(schema, model) ? "\"".concat(schema.name, "\".") : '', "\"").concat(_enum.name, "\" AS ENUM (\n").concat(enumValueStr, "\n);\n");
51
53
  return line;
52
54
  });
53
- return enumArr.length ? enumArr.join('\n') : '';
55
+ return enumArr;
54
56
  }
55
57
  }, {
56
58
  key: "getFieldLines",
@@ -63,10 +65,15 @@ var PostgresExporter = /*#__PURE__*/function () {
63
65
  if (field.increment) {
64
66
  var typeSerial = field.type.type_name === 'bigint' ? 'BIGSERIAL' : 'SERIAL';
65
67
  line = "\"".concat(field.name, "\" ").concat(typeSerial);
66
- } else if ((0, _utils.hasWhiteSpace)(field.type.type_name)) {
67
- line = "\"".concat(field.name, "\" \"").concat(field.type.type_name, "\"");
68
68
  } else {
69
- 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);
70
77
  }
71
78
 
72
79
  if (field.unique) {
@@ -150,7 +157,7 @@ var PostgresExporter = /*#__PURE__*/function () {
150
157
  }).join(',\n'), "\n);\n");
151
158
  return tableStr;
152
159
  });
153
- return tableStrs.length ? tableStrs.join('\n') : '';
160
+ return tableStrs;
154
161
  }
155
162
  }, {
156
163
  key: "buildFieldName",
@@ -205,7 +212,7 @@ var PostgresExporter = /*#__PURE__*/function () {
205
212
  line += ';\n';
206
213
  return line;
207
214
  });
208
- return strArr.length ? strArr.join('\n') : '';
215
+ return strArr;
209
216
  }
210
217
  }, {
211
218
  key: "exportIndexes",
@@ -253,7 +260,7 @@ var PostgresExporter = /*#__PURE__*/function () {
253
260
  line += ';\n';
254
261
  return line;
255
262
  });
256
- return indexArr.length ? indexArr.join('\n') : '';
263
+ return indexArr;
257
264
  }
258
265
  }, {
259
266
  key: "exportComments",
@@ -284,50 +291,45 @@ var PostgresExporter = /*#__PURE__*/function () {
284
291
  line += ';\n';
285
292
  return line;
286
293
  });
287
- return commentArr.length ? commentArr.join('\n') : '';
294
+ return commentArr;
288
295
  }
289
296
  }, {
290
297
  key: "export",
291
298
  value: function _export(model) {
292
- var res = '';
293
- var hasBlockAbove = false;
294
299
  var database = model.database['1'];
295
- var indexIds = [];
296
- var comments = [];
297
- database.schemaIds.forEach(function (schemaId) {
300
+ var statements = database.schemaIds.reduce(function (prevStatements, schemaId) {
298
301
  var schema = model.schemas[schemaId];
299
302
  var tableIds = schema.tableIds,
300
303
  enumIds = schema.enumIds,
301
304
  refIds = schema.refIds;
302
305
 
303
306
  if ((0, _utils.shouldPrintSchema)(schema, model)) {
304
- if (hasBlockAbove) res += '\n';
305
- res += "CREATE SCHEMA \"".concat(schema.name, "\";\n");
306
- hasBlockAbove = true;
307
+ prevStatements.schemas.push("CREATE SCHEMA \"".concat(schema.name, "\";\n"));
307
308
  }
308
309
 
309
310
  if (!_lodash["default"].isEmpty(enumIds)) {
310
- if (hasBlockAbove) res += '\n';
311
- res += PostgresExporter.exportEnums(enumIds, model);
312
- hasBlockAbove = true;
311
+ var _prevStatements$enums;
312
+
313
+ (_prevStatements$enums = prevStatements.enums).push.apply(_prevStatements$enums, _toConsumableArray(PostgresExporter.exportEnums(enumIds, model)));
313
314
  }
314
315
 
315
316
  if (!_lodash["default"].isEmpty(tableIds)) {
316
- if (hasBlockAbove) res += '\n';
317
- res += PostgresExporter.exportTables(tableIds, model);
318
- hasBlockAbove = true;
319
- }
317
+ var _prevStatements$table;
320
318
 
321
- if (!_lodash["default"].isEmpty(refIds)) {
322
- if (hasBlockAbove) res += '\n';
323
- res += PostgresExporter.exportRefs(refIds, model);
324
- hasBlockAbove = true;
319
+ (_prevStatements$table = prevStatements.tables).push.apply(_prevStatements$table, _toConsumableArray(PostgresExporter.exportTables(tableIds, model)));
325
320
  }
326
321
 
327
- indexIds.push.apply(indexIds, _toConsumableArray(_lodash["default"].flatten(tableIds.map(function (tableId) {
322
+ var indexIds = _lodash["default"].flatten(tableIds.map(function (tableId) {
328
323
  return model.tables[tableId].indexIds;
329
- }))));
330
- 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) {
331
333
  var _model$tables$tableId = model.tables[tableId],
332
334
  fieldIds = _model$tables$tableId.fieldIds,
333
335
  note = _model$tables$tableId.note;
@@ -344,20 +346,31 @@ var PostgresExporter = /*#__PURE__*/function () {
344
346
  type: 'table',
345
347
  tableId: tableId
346
348
  }].concat(fieldObjects) : fieldObjects;
347
- }))));
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: []
348
371
  });
349
372
 
350
- if (!_lodash["default"].isEmpty(indexIds)) {
351
- if (hasBlockAbove) res += '\n';
352
- res += PostgresExporter.exportIndexes(indexIds, model);
353
- hasBlockAbove = true;
354
- }
355
-
356
- if (!_lodash["default"].isEmpty(comments)) {
357
- if (hasBlockAbove) res += '\n';
358
- res += PostgresExporter.exportComments(comments, model);
359
- hasBlockAbove = true;
360
- }
373
+ var res = _lodash["default"].concat(statements.schemas, statements.enums, statements.tables, statements.indexes, statements.comments, statements.refs).join('\n');
361
374
 
362
375
  return res;
363
376
  }
@@ -141,7 +141,7 @@ var SqlServerExporter = /*#__PURE__*/function () {
141
141
  }).join(',\n'), "\n)\nGO\n");
142
142
  return tableStr;
143
143
  });
144
- return tableStrs.length ? tableStrs.join('\n') : '';
144
+ return tableStrs;
145
145
  }
146
146
  }, {
147
147
  key: "buildFieldName",
@@ -196,7 +196,7 @@ var SqlServerExporter = /*#__PURE__*/function () {
196
196
  line += '\nGO\n';
197
197
  return line;
198
198
  });
199
- return strArr.length ? strArr.join('\n') : '';
199
+ return strArr;
200
200
  }
201
201
  }, {
202
202
  key: "exportIndexes",
@@ -233,7 +233,7 @@ var SqlServerExporter = /*#__PURE__*/function () {
233
233
  line += '\nGO\n';
234
234
  return line;
235
235
  });
236
- return indexArr.length ? indexArr.join('\n') : '';
236
+ return indexArr;
237
237
  }
238
238
  }, {
239
239
  key: "exportComments",
@@ -272,49 +272,42 @@ var SqlServerExporter = /*#__PURE__*/function () {
272
272
  line += 'GO\n';
273
273
  return line;
274
274
  });
275
- return commentArr.length ? commentArr.join('\n') : '';
275
+ return commentArr;
276
276
  }
277
277
  }, {
278
278
  key: "export",
279
279
  value: function _export(model) {
280
- var res = '';
281
- var hasBlockAbove = false;
282
280
  var database = model.database['1'];
283
- var indexIds = [];
284
- var comments = [];
285
- database.schemaIds.forEach(function (schemaId) {
281
+ var statements = database.schemaIds.reduce(function (prevStatements, schemaId) {
286
282
  var schema = model.schemas[schemaId];
287
283
  var tableIds = schema.tableIds,
288
284
  refIds = schema.refIds;
289
285
 
290
286
  if ((0, _utils.shouldPrintSchema)(schema, model)) {
291
- if (hasBlockAbove) res += '\n';
292
- res += "CREATE SCHEMA [".concat(schema.name, "];\nGO\n");
293
- hasBlockAbove = true;
287
+ prevStatements.schemas.push("CREATE SCHEMA [".concat(schema.name, "]\nGO\n"));
294
288
  }
295
289
 
296
290
  if (!_lodash["default"].isEmpty(tableIds)) {
297
- if (hasBlockAbove) res += '\n';
298
- res += SqlServerExporter.exportTables(tableIds, model);
299
- hasBlockAbove = true;
291
+ var _prevStatements$table;
292
+
293
+ (_prevStatements$table = prevStatements.tables).push.apply(_prevStatements$table, _toConsumableArray(SqlServerExporter.exportTables(tableIds, model)));
300
294
  }
301
295
 
302
- if (!_lodash["default"].isEmpty(refIds)) {
303
- if (hasBlockAbove) res += '\n';
304
- res += SqlServerExporter.exportRefs(refIds, model);
305
- hasBlockAbove = true;
306
- } /////////PUSH COMMENTS OF TABLE & COLUMN/////////
307
- // console.log(JSON.stringify(tableIds, null, 2));
296
+ var indexIds = _lodash["default"].flatten(tableIds.map(function (tableId) {
297
+ return model.tables[tableId].indexIds;
298
+ }));
308
299
 
300
+ if (!_lodash["default"].isEmpty(indexIds)) {
301
+ var _prevStatements$index;
309
302
 
310
- indexIds.push.apply(indexIds, _toConsumableArray(_lodash["default"].flatten(tableIds.map(function (tableId) {
311
- return model.tables[tableId].indexIds;
312
- }))));
313
- 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) {
314
307
  var _model$tables$tableId = model.tables[tableId],
315
308
  fieldIds = _model$tables$tableId.fieldIds,
316
309
  note = _model$tables$tableId.note;
317
- var fieldObject = fieldIds.filter(function (fieldId) {
310
+ var fieldObjects = fieldIds.filter(function (fieldId) {
318
311
  return model.fields[fieldId].note;
319
312
  }).map(function (fieldId) {
320
313
  return {
@@ -326,21 +319,32 @@ var SqlServerExporter = /*#__PURE__*/function () {
326
319
  return note ? [{
327
320
  type: 'table',
328
321
  tableId: tableId
329
- }].concat(fieldObject) : fieldObject;
330
- }))));
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: []
331
345
  });
332
346
 
333
- if (!_lodash["default"].isEmpty(indexIds)) {
334
- if (hasBlockAbove) res += '\n';
335
- res += SqlServerExporter.exportIndexes(indexIds, model);
336
- hasBlockAbove = true;
337
- }
338
-
339
- if (!_lodash["default"].isEmpty(comments)) {
340
- if (hasBlockAbove) res += '\n';
341
- res += SqlServerExporter.exportComments(comments, model);
342
- hasBlockAbove = true;
343
- }
347
+ var res = _lodash["default"].concat(statements.schemas, statements.enums, statements.tables, statements.indexes, statements.comments, statements.refs).join('\n');
344
348
 
345
349
  return res;
346
350
  }
@@ -11,7 +11,7 @@ var _schema = _interopRequireDefault(require("./schema"));
11
11
 
12
12
  var _ref2 = _interopRequireDefault(require("./ref"));
13
13
 
14
- var _enum = _interopRequireDefault(require("./enum"));
14
+ var _enum2 = _interopRequireDefault(require("./enum"));
15
15
 
16
16
  var _tableGroup = _interopRequireDefault(require("./tableGroup"));
17
17
 
@@ -72,7 +72,9 @@ var Database = /*#__PURE__*/function (_Element) {
72
72
  _ref$tableGroups = _ref.tableGroups,
73
73
  tableGroups = _ref$tableGroups === void 0 ? [] : _ref$tableGroups,
74
74
  _ref$project = _ref.project,
75
- project = _ref$project === void 0 ? {} : _ref$project;
75
+ project = _ref$project === void 0 ? {} : _ref$project,
76
+ _ref$aliases = _ref.aliases,
77
+ aliases = _ref$aliases === void 0 ? [] : _ref$aliases;
76
78
 
77
79
  _classCallCheck(this, Database);
78
80
 
@@ -85,16 +87,17 @@ var Database = /*#__PURE__*/function (_Element) {
85
87
  _this.schemas = [];
86
88
  _this.note = project.note;
87
89
  _this.databaseType = project.database_type;
88
- _this.name = project.name; // The process order is important. Do not change !
90
+ _this.name = project.name;
91
+ _this.aliases = aliases; // The process order is important. Do not change !
89
92
 
90
93
  _this.processSchemas(schemas);
91
94
 
95
+ _this.processSchemaElements(enums, _config.ENUM);
96
+
92
97
  _this.processSchemaElements(tables, _config.TABLE);
93
98
 
94
99
  _this.processSchemaElements(refs, _config.REF);
95
100
 
96
- _this.processSchemaElements(enums, _config.ENUM);
97
-
98
101
  _this.processSchemaElements(tableGroups, _config.TABLE_GROUP);
99
102
 
100
103
  return _this;
@@ -141,8 +144,7 @@ var Database = /*#__PURE__*/function (_Element) {
141
144
  if (element.schemaName) {
142
145
  schema = _this3.findOrCreateSchema(element.schemaName);
143
146
 
144
- if (element.schemaName === _config.DEFAULT_SCHEMA_NAME) {
145
- _this3.hasDefaultSchema = true;
147
+ if (element.schemaName === _config.DEFAULT_SCHEMA_NAME) {// this.hasDefaultSchema = true;
146
148
  }
147
149
  } else {
148
150
  schema = _this3.findOrCreateSchema(_config.DEFAULT_SCHEMA_NAME);
@@ -156,7 +158,7 @@ var Database = /*#__PURE__*/function (_Element) {
156
158
  break;
157
159
 
158
160
  case _config.ENUM:
159
- schema.pushEnum(new _enum["default"](_objectSpread(_objectSpread({}, element), {}, {
161
+ schema.pushEnum(new _enum2["default"](_objectSpread(_objectSpread({}, element), {}, {
160
162
  schema: schema
161
163
  })));
162
164
  break;
@@ -196,16 +198,55 @@ var Database = /*#__PURE__*/function (_Element) {
196
198
 
197
199
  return schema;
198
200
  }
201
+ }, {
202
+ key: "findTableAlias",
203
+ value: function findTableAlias(alias) {
204
+ var sym = this.aliases.find(function (a) {
205
+ return a.name === alias;
206
+ });
207
+ if (!sym || sym.kind !== 'table') return null;
208
+ var schemaName = sym.value.schemaName || _config.DEFAULT_SCHEMA_NAME;
209
+ var schema = this.schemas.find(function (s) {
210
+ return s.name === schemaName;
211
+ });
212
+ if (!schema) return null;
213
+ var tableName = sym.value.tableName;
214
+ var table = schema.tables.find(function (t) {
215
+ return t.name === tableName;
216
+ });
217
+ return table;
218
+ }
199
219
  }, {
200
220
  key: "findTable",
201
- value: function findTable(rawTable) {
202
- var schema = this.findOrCreateSchema(rawTable.schemaName || _config.DEFAULT_SCHEMA_NAME);
221
+ value: function findTable(schemaName, tableName) {
222
+ var table = null;
223
+
224
+ if (!schemaName) {
225
+ table = this.findTableAlias(tableName);
226
+ if (table) return table;
227
+ }
228
+
229
+ var schema = this.findOrCreateSchema(schemaName || _config.DEFAULT_SCHEMA_NAME);
203
230
 
204
231
  if (!schema) {
205
- this.error("Schema ".concat(rawTable.schemaName || _config.DEFAULT_SCHEMA_NAME, " don't exist"));
232
+ this.error("Schema ".concat(schemaName || _config.DEFAULT_SCHEMA_NAME, " don't exist"));
206
233
  }
207
234
 
208
- return schema.findTable(rawTable.name);
235
+ return schema.findTable(tableName);
236
+ }
237
+ }, {
238
+ key: "findEnum",
239
+ value: function findEnum(schemaName, name) {
240
+ var schema = this.schemas.find(function (s) {
241
+ return s.name === schemaName || s.alias === schemaName;
242
+ });
243
+ if (!schema) return null;
244
+
245
+ var _enum = schema.enums.find(function (e) {
246
+ return e.name === name;
247
+ });
248
+
249
+ return _enum;
209
250
  }
210
251
  }, {
211
252
  key: "export",
@@ -72,10 +72,10 @@ var Endpoint = /*#__PURE__*/function (_Element) {
72
72
 
73
73
 
74
74
  var schema = ref.schema.database.findOrCreateSchema(schemaName || _config.DEFAULT_SCHEMA_NAME);
75
- var table = schema.findTable(tableName);
75
+ var table = schema.database.findTable(schemaName, tableName);
76
76
 
77
77
  if (!table) {
78
- _this.error("Can't find table ".concat((0, _utils.shouldPrintSchema)(schema) ? "\"".concat(schema.name, "\".") : '', "\"").concat(tableName, "\""));
78
+ _this.error("Can't find table ".concat((0, _utils.shouldPrintSchemaName)(schemaName) ? "\"".concat(schemaName, "\".") : '', "\"").concat(tableName, "\""));
79
79
  }
80
80
 
81
81
  _this.setFields(fieldNames, table);
@@ -7,6 +7,8 @@ exports["default"] = void 0;
7
7
 
8
8
  var _element = _interopRequireDefault(require("./element"));
9
9
 
10
+ var _config = require("./config");
11
+
10
12
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
11
13
 
12
14
  function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
@@ -70,7 +72,7 @@ var Field = /*#__PURE__*/function (_Element) {
70
72
  _this.error('Field must have a type');
71
73
  }
72
74
 
73
- _this.name = name; // type : { type_name, value }
75
+ _this.name = name; // type : { type_name, value, schemaName }
74
76
 
75
77
  _this.type = type;
76
78
  _this.unique = unique;
@@ -85,6 +87,8 @@ var Field = /*#__PURE__*/function (_Element) {
85
87
 
86
88
  _this.generateId();
87
89
 
90
+ _this.bindType();
91
+
88
92
  return _this;
89
93
  }
90
94
 
@@ -93,6 +97,32 @@ var Field = /*#__PURE__*/function (_Element) {
93
97
  value: function generateId() {
94
98
  this.id = this.dbState.generateId('fieldId');
95
99
  }
100
+ }, {
101
+ key: "bindType",
102
+ value: function bindType() {
103
+ var typeName = this.type.type_name;
104
+ var typeSchemaName = this.type.schemaName || _config.DEFAULT_SCHEMA_NAME;
105
+
106
+ if (this.type.schemaName) {
107
+ var _enum = this.table.schema.database.findEnum(typeSchemaName, typeName);
108
+
109
+ if (!_enum) {
110
+ this.error("Cannot find type ".concat(typeSchemaName, " in schema ").concat(typeSchemaName));
111
+ return;
112
+ }
113
+
114
+ this._enum = _enum;
115
+
116
+ _enum.pushField(this);
117
+ } else {
118
+ var _enum2 = this.table.schema.database.findEnum(typeSchemaName, typeName);
119
+
120
+ if (!_enum2) return;
121
+ this._enum = _enum2;
122
+
123
+ _enum2.pushField(this);
124
+ }
125
+ }
96
126
  }, {
97
127
  key: "pushEndpoint",
98
128
  value: function pushEndpoint(endpoint) {
@@ -100,8 +100,7 @@ var Ref = /*#__PURE__*/function (_Element) {
100
100
  ref: _this2
101
101
  })));
102
102
 
103
- if (endpoint.schemaName === _config.DEFAULT_SCHEMA_NAME) {
104
- _this2.database.hasDefaultSchema = true;
103
+ if (endpoint.schemaName === _config.DEFAULT_SCHEMA_NAME) {// this.schema.database.hasDefaultSchema = true;
105
104
  }
106
105
  });
107
106