@dbml/core 2.3.1 → 2.4.2

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 (40) hide show
  1. package/lib/export/DbmlExporter.js +11 -6
  2. package/lib/export/MysqlExporter.js +127 -48
  3. package/lib/export/PostgresExporter.js +142 -55
  4. package/lib/export/SqlServerExporter.js +130 -52
  5. package/lib/export/utils.js +40 -0
  6. package/lib/model_structure/database.js +53 -12
  7. package/lib/model_structure/endpoint.js +2 -2
  8. package/lib/model_structure/field.js +31 -1
  9. package/lib/model_structure/ref.js +1 -2
  10. package/lib/model_structure/schema.js +3 -19
  11. package/lib/model_structure/tableGroup.js +1 -1
  12. package/lib/model_structure/utils.js +5 -0
  13. package/lib/parse/dbml/parser.pegjs +74 -24
  14. package/lib/parse/dbmlParser.js +1275 -886
  15. package/lib/parse/mssql/fk_definition/actions.js +10 -3
  16. package/lib/parse/mssql/keyword_parsers.js +12 -1
  17. package/lib/parse/mssql/statements/actions.js +37 -6
  18. package/lib/parse/mssql/statements/index.js +1 -1
  19. package/lib/parse/mssql/statements/statement_types/alter_table/actions.js +11 -5
  20. package/lib/parse/mssql/statements/statement_types/comments/actions.js +57 -0
  21. package/lib/parse/mssql/statements/statement_types/comments/index.js +97 -0
  22. package/lib/parse/mssql/statements/statement_types/create_index/actions.js +6 -1
  23. package/lib/parse/mssql/statements/statement_types/create_table/actions.js +11 -9
  24. package/lib/parse/mssql/statements/statement_types/index.js +4 -1
  25. package/lib/parse/mssql/utils.js +15 -0
  26. package/lib/parse/mysql/parser.pegjs +55 -20
  27. package/lib/parse/mysqlParser.js +479 -308
  28. package/lib/parse/postgresParser.js +15 -14
  29. package/lib/parse/postgresql/Base_rules.pegjs +24 -3
  30. package/lib/parse/postgresql/Commands/Alter_table/Alter_table.pegjs +49 -4
  31. package/lib/parse/postgresql/Commands/Comment.pegjs +18 -6
  32. package/lib/parse/postgresql/Commands/Create_table/Create_table_normal.pegjs +5 -3
  33. package/lib/parse/postgresql/Commands/Create_table/Create_table_of.pegjs +1 -1
  34. package/lib/parse/postgresql/Commands/Create_table/Create_table_partition_of.pegjs +1 -1
  35. package/lib/parse/postgresql/Commands/Create_type/Create_type_enum.pegjs +2 -2
  36. package/lib/parse/postgresql/Commands/Ignore_syntax.pegjs +10 -1
  37. package/lib/parse/postgresql/InitializerUtils.pegjs +14 -2
  38. package/lib/parse/postgresql/Keywords.pegjs +5 -1
  39. package/lib/parse/postgresql/parser.pegjs +22 -8
  40. package/package.json +2 -2
@@ -141,7 +141,53 @@ 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
+ }
146
+ }, {
147
+ key: "buildTableManyToMany",
148
+ value: function buildTableManyToMany(firstTableFieldsMap, secondTableFieldsMap, tableName) {
149
+ var line = "CREATE TABLE [".concat(tableName, "] (\n");
150
+
151
+ var key1s = _toConsumableArray(firstTableFieldsMap.keys()).join('], [');
152
+
153
+ var key2s = _toConsumableArray(secondTableFieldsMap.keys()).join('], [');
154
+
155
+ firstTableFieldsMap.forEach(function (fieldType, fieldName) {
156
+ line += " [".concat(fieldName, "] ").concat(fieldType, " NOT NULL,\n");
157
+ });
158
+ secondTableFieldsMap.forEach(function (fieldType, fieldName) {
159
+ line += " [".concat(fieldName, "] ").concat(fieldType, " NOT NULL,\n");
160
+ });
161
+ line += " PRIMARY KEY ([".concat(key1s, "], [").concat(key2s, "])\n");
162
+ line += ');\nGO\n\n';
163
+ return line;
164
+ }
165
+ }, {
166
+ key: "buildForeignKeyManyToMany",
167
+ value: function buildForeignKeyManyToMany(fieldsMap, foreignEndpointFields, refEndpointTableName, foreignEndpointTableName, schema, model) {
168
+ var refEndpointFields = _toConsumableArray(fieldsMap.keys()).join('], [');
169
+
170
+ var line = "ALTER TABLE [".concat(refEndpointTableName, "] ADD FOREIGN KEY ([").concat(refEndpointFields, "]) REFERENCES ").concat((0, _utils.shouldPrintSchema)(schema, model) ? "[".concat(schema.name, "].") : '', "[").concat(foreignEndpointTableName, "] ").concat(foreignEndpointFields, ";\nGO\n\n");
171
+ return line;
172
+ }
173
+ }, {
174
+ key: "buildIndexManytoMany",
175
+ value: function buildIndexManytoMany(fieldsMap, newTableName, tableRefName, usedIndexNames) {
176
+ var newIndexName = "".concat(newTableName, "_").concat(tableRefName);
177
+ var count = 1;
178
+
179
+ while (usedIndexNames.has(newIndexName)) {
180
+ newIndexName = "".concat(newTableName, "_").concat(tableRefName, "(").concat(count, ")");
181
+ count += 1;
182
+ }
183
+
184
+ usedIndexNames.add(newIndexName);
185
+
186
+ var indexFields = _toConsumableArray(fieldsMap.keys()).join('", "');
187
+
188
+ var line = "CREATE INDEX [idx_".concat(newIndexName, "] ON [").concat(newTableName, "] (");
189
+ line += "\"".concat(indexFields, "\");\nGO\n\n");
190
+ return line;
145
191
  }
146
192
  }, {
147
193
  key: "buildFieldName",
@@ -153,14 +199,16 @@ var SqlServerExporter = /*#__PURE__*/function () {
153
199
  }
154
200
  }, {
155
201
  key: "exportRefs",
156
- value: function exportRefs(refIds, model) {
202
+ value: function exportRefs(refIds, model, usedTableNames, usedIndexNames) {
157
203
  var _this = this;
158
204
 
159
205
  var strArr = refIds.map(function (refId) {
206
+ var line = '';
160
207
  var ref = model.refs[refId];
161
- var refEndpointIndex = ref.endpointIds.findIndex(function (endpointId) {
208
+ var refOneIndex = ref.endpointIds.findIndex(function (endpointId) {
162
209
  return model.endpoints[endpointId].relation === '1';
163
210
  });
211
+ var refEndpointIndex = refOneIndex === -1 ? 0 : refOneIndex;
164
212
  var foreignEndpointId = ref.endpointIds[1 - refEndpointIndex];
165
213
  var refEndpointId = ref.endpointIds[refEndpointIndex];
166
214
  var foreignEndpoint = model.endpoints[foreignEndpointId];
@@ -177,26 +225,46 @@ var SqlServerExporter = /*#__PURE__*/function () {
177
225
 
178
226
  var foreignEndpointFieldName = _this.buildFieldName(foreignEndpoint.fieldIds, model, 'mssql');
179
227
 
180
- var line = "ALTER TABLE ".concat((0, _utils.shouldPrintSchema)(foreignEndpointSchema, model) ? "[".concat(foreignEndpointSchema.name, "].") : '', "[").concat(foreignEndpointTable.name, "] ADD ");
228
+ if (refOneIndex === -1) {
229
+ // many to many relationship
230
+ var firstTableFieldsMap = (0, _utils.buildJunctionFields1)(refEndpoint.fieldIds, model);
231
+ var secondTableFieldsMap = (0, _utils.buildJunctionFields2)(foreignEndpoint.fieldIds, model, firstTableFieldsMap);
232
+ var newTableName = (0, _utils.buildNewTableName)(refEndpointTable.name, foreignEndpointTable.name, usedTableNames);
233
+ line += _this.buildTableManyToMany(firstTableFieldsMap, secondTableFieldsMap, newTableName);
181
234
 
182
- if (ref.name) {
183
- line += "CONSTRAINT [".concat(ref.name, "] ");
184
- }
235
+ if (firstTableFieldsMap.size > 1) {
236
+ line += _this.buildIndexManytoMany(firstTableFieldsMap, newTableName, refEndpointTable.name, usedIndexNames);
237
+ }
185
238
 
186
- line += "FOREIGN KEY ".concat(foreignEndpointFieldName, " REFERENCES ").concat((0, _utils.shouldPrintSchema)(refEndpointSchema, model) ? "[".concat(refEndpointSchema.name, "].") : '', "[").concat(refEndpointTable.name, "] ").concat(refEndpointFieldName);
239
+ if (secondTableFieldsMap.size > 1) {
240
+ line += _this.buildIndexManytoMany(secondTableFieldsMap, newTableName, foreignEndpointTable.name, usedIndexNames);
241
+ }
187
242
 
188
- if (ref.onDelete) {
189
- line += " ON DELETE ".concat(ref.onDelete.toUpperCase());
190
- }
243
+ line += _this.buildForeignKeyManyToMany(firstTableFieldsMap, refEndpointFieldName, newTableName, refEndpointTable.name, refEndpointSchema, model);
244
+ line += _this.buildForeignKeyManyToMany(secondTableFieldsMap, foreignEndpointFieldName, newTableName, foreignEndpointTable.name, foreignEndpointSchema, model);
245
+ } else {
246
+ line = "ALTER TABLE ".concat((0, _utils.shouldPrintSchema)(foreignEndpointSchema, model) ? "[".concat(foreignEndpointSchema.name, "].") : '', "[").concat(foreignEndpointTable.name, "] ADD ");
247
+
248
+ if (ref.name) {
249
+ line += "CONSTRAINT [".concat(ref.name, "] ");
250
+ }
191
251
 
192
- if (ref.onUpdate) {
193
- line += " ON UPDATE ".concat(ref.onUpdate.toUpperCase());
252
+ line += "FOREIGN KEY ".concat(foreignEndpointFieldName, " REFERENCES ").concat((0, _utils.shouldPrintSchema)(refEndpointSchema, model) ? "[".concat(refEndpointSchema.name, "].") : '', "[").concat(refEndpointTable.name, "] ").concat(refEndpointFieldName);
253
+
254
+ if (ref.onDelete) {
255
+ line += " ON DELETE ".concat(ref.onDelete.toUpperCase());
256
+ }
257
+
258
+ if (ref.onUpdate) {
259
+ line += " ON UPDATE ".concat(ref.onUpdate.toUpperCase());
260
+ }
261
+
262
+ line += '\nGO\n';
194
263
  }
195
264
 
196
- line += '\nGO\n';
197
265
  return line;
198
266
  });
199
- return strArr.length ? strArr.join('\n') : '';
267
+ return strArr;
200
268
  }
201
269
  }, {
202
270
  key: "exportIndexes",
@@ -233,7 +301,7 @@ var SqlServerExporter = /*#__PURE__*/function () {
233
301
  line += '\nGO\n';
234
302
  return line;
235
303
  });
236
- return indexArr.length ? indexArr.join('\n') : '';
304
+ return indexArr;
237
305
  }
238
306
  }, {
239
307
  key: "exportComments",
@@ -272,49 +340,48 @@ var SqlServerExporter = /*#__PURE__*/function () {
272
340
  line += 'GO\n';
273
341
  return line;
274
342
  });
275
- return commentArr.length ? commentArr.join('\n') : '';
343
+ return commentArr;
276
344
  }
277
345
  }, {
278
346
  key: "export",
279
347
  value: function _export(model) {
280
- var res = '';
281
- var hasBlockAbove = false;
282
348
  var database = model.database['1'];
283
- var indexIds = [];
284
- var comments = [];
285
- database.schemaIds.forEach(function (schemaId) {
349
+ var usedTableNames = new Set(Object.values(model.tables).map(function (table) {
350
+ return table.name;
351
+ }));
352
+ var usedIndexNames = new Set(Object.values(model.indexes).map(function (index) {
353
+ return index.name;
354
+ }));
355
+ var statements = database.schemaIds.reduce(function (prevStatements, schemaId) {
286
356
  var schema = model.schemas[schemaId];
287
357
  var tableIds = schema.tableIds,
288
358
  refIds = schema.refIds;
289
359
 
290
360
  if ((0, _utils.shouldPrintSchema)(schema, model)) {
291
- if (hasBlockAbove) res += '\n';
292
- res += "CREATE SCHEMA [".concat(schema.name, "];\nGO\n");
293
- hasBlockAbove = true;
361
+ prevStatements.schemas.push("CREATE SCHEMA [".concat(schema.name, "]\nGO\n"));
294
362
  }
295
363
 
296
364
  if (!_lodash["default"].isEmpty(tableIds)) {
297
- if (hasBlockAbove) res += '\n';
298
- res += SqlServerExporter.exportTables(tableIds, model);
299
- hasBlockAbove = true;
365
+ var _prevStatements$table;
366
+
367
+ (_prevStatements$table = prevStatements.tables).push.apply(_prevStatements$table, _toConsumableArray(SqlServerExporter.exportTables(tableIds, model)));
300
368
  }
301
369
 
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));
370
+ var indexIds = _lodash["default"].flatten(tableIds.map(function (tableId) {
371
+ return model.tables[tableId].indexIds;
372
+ }));
308
373
 
374
+ if (!_lodash["default"].isEmpty(indexIds)) {
375
+ var _prevStatements$index;
309
376
 
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) {
377
+ (_prevStatements$index = prevStatements.indexes).push.apply(_prevStatements$index, _toConsumableArray(SqlServerExporter.exportIndexes(indexIds, model)));
378
+ }
379
+
380
+ var commentNodes = _lodash["default"].flatten(tableIds.map(function (tableId) {
314
381
  var _model$tables$tableId = model.tables[tableId],
315
382
  fieldIds = _model$tables$tableId.fieldIds,
316
383
  note = _model$tables$tableId.note;
317
- var fieldObject = fieldIds.filter(function (fieldId) {
384
+ var fieldObjects = fieldIds.filter(function (fieldId) {
318
385
  return model.fields[fieldId].note;
319
386
  }).map(function (fieldId) {
320
387
  return {
@@ -326,21 +393,32 @@ var SqlServerExporter = /*#__PURE__*/function () {
326
393
  return note ? [{
327
394
  type: 'table',
328
395
  tableId: tableId
329
- }].concat(fieldObject) : fieldObject;
330
- }))));
331
- });
396
+ }].concat(fieldObjects) : fieldObjects;
397
+ }));
332
398
 
333
- if (!_lodash["default"].isEmpty(indexIds)) {
334
- if (hasBlockAbove) res += '\n';
335
- res += SqlServerExporter.exportIndexes(indexIds, model);
336
- hasBlockAbove = true;
337
- }
399
+ if (!_lodash["default"].isEmpty(commentNodes)) {
400
+ var _prevStatements$comme;
338
401
 
339
- if (!_lodash["default"].isEmpty(comments)) {
340
- if (hasBlockAbove) res += '\n';
341
- res += SqlServerExporter.exportComments(comments, model);
342
- hasBlockAbove = true;
343
- }
402
+ (_prevStatements$comme = prevStatements.comments).push.apply(_prevStatements$comme, _toConsumableArray(SqlServerExporter.exportComments(commentNodes, model)));
403
+ }
404
+
405
+ if (!_lodash["default"].isEmpty(refIds)) {
406
+ var _prevStatements$refs;
407
+
408
+ (_prevStatements$refs = prevStatements.refs).push.apply(_prevStatements$refs, _toConsumableArray(SqlServerExporter.exportRefs(refIds, model, usedTableNames, usedIndexNames)));
409
+ }
410
+
411
+ return prevStatements;
412
+ }, {
413
+ schemas: [],
414
+ enums: [],
415
+ tables: [],
416
+ indexes: [],
417
+ comments: [],
418
+ refs: []
419
+ });
420
+
421
+ var res = _lodash["default"].concat(statements.schemas, statements.enums, statements.tables, statements.indexes, statements.comments, statements.refs).join('\n');
344
422
 
345
423
  return res;
346
424
  }
@@ -5,6 +5,9 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.hasWhiteSpace = hasWhiteSpace;
7
7
  exports.shouldPrintSchema = shouldPrintSchema;
8
+ exports.buildJunctionFields1 = buildJunctionFields1;
9
+ exports.buildJunctionFields2 = buildJunctionFields2;
10
+ exports.buildNewTableName = buildNewTableName;
8
11
 
9
12
  var _config = require("../model_structure/config");
10
13
 
@@ -14,4 +17,41 @@ function hasWhiteSpace(s) {
14
17
 
15
18
  function shouldPrintSchema(schema, model) {
16
19
  return schema.name !== _config.DEFAULT_SCHEMA_NAME || schema.name === _config.DEFAULT_SCHEMA_NAME && model.database['1'].hasDefaultSchema;
20
+ }
21
+
22
+ function buildJunctionFields1(fieldIds, model) {
23
+ var fieldsMap = new Map();
24
+ fieldIds.map(function (fieldId) {
25
+ return fieldsMap.set("".concat(model.tables[model.fields[fieldId].tableId].name, "_").concat(model.fields[fieldId].name), model.fields[fieldId].type.type_name);
26
+ });
27
+ return fieldsMap;
28
+ }
29
+
30
+ function buildJunctionFields2(fieldIds, model, firstTableFieldsMap) {
31
+ var fieldsMap = new Map();
32
+ fieldIds.map(function (fieldId) {
33
+ var fieldName = "".concat(model.tables[model.fields[fieldId].tableId].name, "_").concat(model.fields[fieldId].name);
34
+ var count = 1;
35
+
36
+ while (firstTableFieldsMap.has(fieldName)) {
37
+ fieldName = "".concat(model.tables[model.fields[fieldId].tableId].name, "_").concat(model.fields[fieldId].name, "(").concat(count, ")");
38
+ count += 1;
39
+ }
40
+
41
+ fieldsMap.set(fieldName, model.fields[fieldId].type.type_name);
42
+ });
43
+ return fieldsMap;
44
+ }
45
+
46
+ function buildNewTableName(firstTable, secondTable, usedTableNames) {
47
+ var newTableName = "".concat(firstTable, "_").concat(secondTable);
48
+ var count = 1;
49
+
50
+ while (usedTableNames.has(newTableName)) {
51
+ newTableName = "".concat(firstTable, "_").concat(secondTable, "(").concat(count, ")");
52
+ count += 1;
53
+ }
54
+
55
+ usedTableNames.add(newTableName);
56
+ return newTableName;
17
57
  }
@@ -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
 
@@ -88,10 +88,10 @@ var Schema = /*#__PURE__*/function (_Element) {
88
88
 
89
89
  _this.generateId();
90
90
 
91
- _this.processTables(tables);
92
-
93
91
  _this.processEnums(enums);
94
92
 
93
+ _this.processTables(tables);
94
+
95
95
  _this.processRefs(refs);
96
96
 
97
97
  _this.processTableGroups(tableGroups);
@@ -134,7 +134,7 @@ var Schema = /*#__PURE__*/function (_Element) {
134
134
  key: "findTable",
135
135
  value: function findTable(tableName) {
136
136
  return this.tables.find(function (t) {
137
- return t.name === tableName || t.alias === tableName;
137
+ return t.name === tableName;
138
138
  });
139
139
  }
140
140
  }, {
@@ -153,7 +153,6 @@ var Schema = /*#__PURE__*/function (_Element) {
153
153
  value: function pushEnum(_enum) {
154
154
  this.checkEnum(_enum);
155
155
  this.enums.push(_enum);
156
- this.bindEnumToField(_enum);
157
156
  }
158
157
  }, {
159
158
  key: "checkEnum",
@@ -164,21 +163,6 @@ var Schema = /*#__PURE__*/function (_Element) {
164
163
  _enum.error("Enum ".concat((0, _utils.shouldPrintSchema)(this) ? "\"".concat(this.name, "\".") : '', "\"").concat(_enum.name, "\" existed"));
165
164
  }
166
165
  }
167
- }, {
168
- key: "bindEnumToField",
169
- value: function bindEnumToField(_enum) {
170
- this.database.schemas.forEach(function (schema) {
171
- schema.tables.forEach(function (table) {
172
- table.fields.forEach(function (field) {
173
- if (_enum.name === field.type.type_name && (field.type.schemaName || _config.DEFAULT_SCHEMA_NAME) === schema.name) {
174
- field._enum = _enum;
175
-
176
- _enum.pushField(field);
177
- }
178
- });
179
- });
180
- });
181
- }
182
166
  }, {
183
167
  key: "processRefs",
184
168
  value: function processRefs(rawRefs) {
@@ -80,7 +80,7 @@ var TableGroup = /*#__PURE__*/function (_Element) {
80
80
  var _this2 = this;
81
81
 
82
82
  rawTables.forEach(function (rawTable) {
83
- var table = _this2.schema.database.findTable(rawTable);
83
+ var table = _this2.schema.database.findTable(rawTable.schemaName, rawTable.name);
84
84
 
85
85
  if (!table) {
86
86
  _this2.error("Table ".concat(rawTable.schemaName ? "\"".concat(rawTable.schemaName, "\".") : '').concat(rawTable.name, " don't exist"));
@@ -4,9 +4,14 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.shouldPrintSchema = shouldPrintSchema;
7
+ exports.shouldPrintSchemaName = shouldPrintSchemaName;
7
8
 
8
9
  var _config = require("./config");
9
10
 
10
11
  function shouldPrintSchema(schema) {
11
12
  return schema.name !== _config.DEFAULT_SCHEMA_NAME || schema.name === _config.DEFAULT_SCHEMA_NAME && schema.database.hasDefaultSchema;
13
+ }
14
+
15
+ function shouldPrintSchemaName(schemaName) {
16
+ return schemaName !== _config.DEFAULT_SCHEMA_NAME;
12
17
  }