@dbml/core 2.4.1 → 2.4.3

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.
@@ -154,16 +154,45 @@ var MySQLExporter = /*#__PURE__*/function () {
154
154
  }).join(', ');
155
155
  return "(".concat(fieldNames, ")");
156
156
  }
157
+ }, {
158
+ key: "buildTableManyToMany",
159
+ value: function buildTableManyToMany(firstTableFieldsMap, secondTableFieldsMap, tableName, refEndpointSchema, model) {
160
+ var line = "CREATE TABLE ".concat((0, _utils.shouldPrintSchema)(refEndpointSchema, model) ? "`".concat(refEndpointSchema.name, "`.") : '', "`").concat(tableName, "` (\n");
161
+
162
+ var key1s = _toConsumableArray(firstTableFieldsMap.keys()).join('`, `');
163
+
164
+ var key2s = _toConsumableArray(secondTableFieldsMap.keys()).join('`, `');
165
+
166
+ firstTableFieldsMap.forEach(function (fieldType, fieldName) {
167
+ line += " `".concat(fieldName, "` ").concat(fieldType, ",\n");
168
+ });
169
+ secondTableFieldsMap.forEach(function (fieldType, fieldName) {
170
+ line += " `".concat(fieldName, "` ").concat(fieldType, ",\n");
171
+ });
172
+ line += " PRIMARY KEY (`".concat(key1s, "`, `").concat(key2s, "`)\n");
173
+ line += ');\n\n';
174
+ return line;
175
+ }
176
+ }, {
177
+ key: "buildForeignKeyManyToMany",
178
+ value: function buildForeignKeyManyToMany(fieldsMap, foreignEndpointFields, refEndpointTableName, foreignEndpointTableName, refEndpointSchema, foreignEndpointSchema, model) {
179
+ var refEndpointFields = _toConsumableArray(fieldsMap.keys()).join('`, `');
180
+
181
+ var line = "ALTER TABLE ".concat((0, _utils.shouldPrintSchema)(refEndpointSchema, model) ? "`".concat(refEndpointSchema.name, "`.") : '', "`").concat(refEndpointTableName, "` ADD FOREIGN KEY (`").concat(refEndpointFields, "`) REFERENCES ").concat((0, _utils.shouldPrintSchema)(foreignEndpointSchema, model) ? "`".concat(foreignEndpointSchema.name, "`.") : '', "`").concat(foreignEndpointTableName, "` ").concat(foreignEndpointFields, ";\n\n");
182
+ return line;
183
+ }
157
184
  }, {
158
185
  key: "exportRefs",
159
- value: function exportRefs(refIds, model) {
186
+ value: function exportRefs(refIds, model, usedTableNames) {
160
187
  var _this = this;
161
188
 
162
189
  var strArr = refIds.map(function (refId) {
190
+ var line = '';
163
191
  var ref = model.refs[refId];
164
- var refEndpointIndex = ref.endpointIds.findIndex(function (endpointId) {
192
+ var refOneIndex = ref.endpointIds.findIndex(function (endpointId) {
165
193
  return model.endpoints[endpointId].relation === '1';
166
194
  });
195
+ var refEndpointIndex = refOneIndex === -1 ? 0 : refOneIndex;
167
196
  var foreignEndpointId = ref.endpointIds[1 - refEndpointIndex];
168
197
  var refEndpointId = ref.endpointIds[refEndpointIndex];
169
198
  var foreignEndpoint = model.endpoints[foreignEndpointId];
@@ -180,23 +209,33 @@ var MySQLExporter = /*#__PURE__*/function () {
180
209
 
181
210
  var foreignEndpointFieldName = _this.buildFieldName(foreignEndpoint.fieldIds, model, 'mysql');
182
211
 
183
- var line = "ALTER TABLE ".concat((0, _utils.shouldPrintSchema)(foreignEndpointSchema, model) ? "`".concat(foreignEndpointSchema.name, "`.") : '', "`").concat(foreignEndpointTable.name, "` ADD ");
212
+ if (refOneIndex === -1) {
213
+ var firstTableFieldsMap = (0, _utils.buildJunctionFields1)(refEndpoint.fieldIds, model);
214
+ var secondTableFieldsMap = (0, _utils.buildJunctionFields2)(foreignEndpoint.fieldIds, model, firstTableFieldsMap);
215
+ var newTableName = (0, _utils.buildNewTableName)(refEndpointTable.name, foreignEndpointTable.name, usedTableNames);
216
+ line += _this.buildTableManyToMany(firstTableFieldsMap, secondTableFieldsMap, newTableName, refEndpointSchema, model);
217
+ line += _this.buildForeignKeyManyToMany(firstTableFieldsMap, refEndpointFieldName, newTableName, refEndpointTable.name, refEndpointSchema, refEndpointSchema, model);
218
+ line += _this.buildForeignKeyManyToMany(secondTableFieldsMap, foreignEndpointFieldName, newTableName, foreignEndpointTable.name, refEndpointSchema, foreignEndpointSchema, model);
219
+ } else {
220
+ line = "ALTER TABLE ".concat((0, _utils.shouldPrintSchema)(foreignEndpointSchema, model) ? "`".concat(foreignEndpointSchema.name, "`.") : '', "`").concat(foreignEndpointTable.name, "` ADD ");
184
221
 
185
- if (ref.name) {
186
- line += "CONSTRAINT `".concat(ref.name, "` ");
187
- }
222
+ if (ref.name) {
223
+ line += "CONSTRAINT `".concat(ref.name, "` ");
224
+ }
188
225
 
189
- line += "FOREIGN KEY ".concat(foreignEndpointFieldName, " REFERENCES ").concat((0, _utils.shouldPrintSchema)(refEndpointSchema, model) ? "`".concat(refEndpointSchema.name, "`.") : '', "`").concat(refEndpointTable.name, "` ").concat(refEndpointFieldName);
226
+ line += "FOREIGN KEY ".concat(foreignEndpointFieldName, " REFERENCES ").concat((0, _utils.shouldPrintSchema)(refEndpointSchema, model) ? "`".concat(refEndpointSchema.name, "`.") : '', "`").concat(refEndpointTable.name, "` ").concat(refEndpointFieldName);
190
227
 
191
- if (ref.onDelete) {
192
- line += " ON DELETE ".concat(ref.onDelete.toUpperCase());
193
- }
228
+ if (ref.onDelete) {
229
+ line += " ON DELETE ".concat(ref.onDelete.toUpperCase());
230
+ }
231
+
232
+ if (ref.onUpdate) {
233
+ line += " ON UPDATE ".concat(ref.onUpdate.toUpperCase());
234
+ }
194
235
 
195
- if (ref.onUpdate) {
196
- line += " ON UPDATE ".concat(ref.onUpdate.toUpperCase());
236
+ line += ';\n';
197
237
  }
198
238
 
199
- line += ';\n';
200
239
  return line;
201
240
  });
202
241
  return strArr;
@@ -263,6 +302,9 @@ var MySQLExporter = /*#__PURE__*/function () {
263
302
  key: "export",
264
303
  value: function _export(model) {
265
304
  var database = model.database['1'];
305
+ var usedTableNames = new Set(Object.values(model.tables).map(function (table) {
306
+ return table.name;
307
+ }));
266
308
  var statements = database.schemaIds.reduce(function (prevStatements, schemaId) {
267
309
  var schema = model.schemas[schemaId];
268
310
  var tableIds = schema.tableIds,
@@ -305,7 +347,7 @@ var MySQLExporter = /*#__PURE__*/function () {
305
347
  if (!_lodash["default"].isEmpty(refIds)) {
306
348
  var _prevStatements$refs;
307
349
 
308
- (_prevStatements$refs = prevStatements.refs).push.apply(_prevStatements$refs, _toConsumableArray(MySQLExporter.exportRefs(refIds, model)));
350
+ (_prevStatements$refs = prevStatements.refs).push.apply(_prevStatements$refs, _toConsumableArray(MySQLExporter.exportRefs(refIds, model, usedTableNames)));
309
351
  }
310
352
 
311
353
  return prevStatements;
@@ -63,7 +63,8 @@ var PostgresExporter = /*#__PURE__*/function () {
63
63
  var line = '';
64
64
 
65
65
  if (field.increment) {
66
- var typeSerial = field.type.type_name === 'bigint' ? 'BIGSERIAL' : 'SERIAL';
66
+ var typeSerial = 'SERIAL';
67
+ if (field.type.type_name.toLowerCase() === 'bigint') typeSerial = 'BIGSERIAL';else if (field.type.type_name.toLowerCase() === 'smallserial') typeSerial = 'SMALLSERIAL';
67
68
  line = "\"".concat(field.name, "\" ").concat(typeSerial);
68
69
  } else {
69
70
  var schemaName = '';
@@ -167,16 +168,45 @@ var PostgresExporter = /*#__PURE__*/function () {
167
168
  }).join(', ');
168
169
  return "(".concat(fieldNames, ")");
169
170
  }
171
+ }, {
172
+ key: "buildTableManyToMany",
173
+ value: function buildTableManyToMany(firstTableFieldsMap, secondTableFieldsMap, tableName, refEndpointSchema, model) {
174
+ var line = "CREATE TABLE ".concat((0, _utils.shouldPrintSchema)(refEndpointSchema, model) ? "\"".concat(refEndpointSchema.name, "\".") : '', "\"").concat(tableName, "\" (\n");
175
+
176
+ var key1s = _toConsumableArray(firstTableFieldsMap.keys()).join('", "');
177
+
178
+ var key2s = _toConsumableArray(secondTableFieldsMap.keys()).join('", "');
179
+
180
+ firstTableFieldsMap.forEach(function (fieldType, fieldName) {
181
+ line += " \"".concat(fieldName, "\" ").concat(fieldType, ",\n");
182
+ });
183
+ secondTableFieldsMap.forEach(function (fieldType, fieldName) {
184
+ line += " \"".concat(fieldName, "\" ").concat(fieldType, ",\n");
185
+ });
186
+ line += " PRIMARY KEY (\"".concat(key1s, "\", \"").concat(key2s, "\")\n");
187
+ line += ');\n\n';
188
+ return line;
189
+ }
190
+ }, {
191
+ key: "buildForeignKeyManyToMany",
192
+ value: function buildForeignKeyManyToMany(fieldsMap, foreignEndpointFields, refEndpointTableName, foreignEndpointTableName, refEndpointSchema, foreignEndpointSchema, model) {
193
+ var refEndpointFields = _toConsumableArray(fieldsMap.keys()).join('", "');
194
+
195
+ var line = "ALTER TABLE ".concat((0, _utils.shouldPrintSchema)(refEndpointSchema, model) ? "\"".concat(refEndpointSchema.name, "\".") : '', "\"").concat(refEndpointTableName, "\" ADD FOREIGN KEY (\"").concat(refEndpointFields, "\") REFERENCES ").concat((0, _utils.shouldPrintSchema)(foreignEndpointSchema, model) ? "\"".concat(foreignEndpointSchema.name, "\".") : '', "\"").concat(foreignEndpointTableName, "\" ").concat(foreignEndpointFields, ";\n\n");
196
+ return line;
197
+ }
170
198
  }, {
171
199
  key: "exportRefs",
172
- value: function exportRefs(refIds, model) {
200
+ value: function exportRefs(refIds, model, usedTableNames) {
173
201
  var _this = this;
174
202
 
175
203
  var strArr = refIds.map(function (refId) {
204
+ var line = '';
176
205
  var ref = model.refs[refId];
177
- var refEndpointIndex = ref.endpointIds.findIndex(function (endpointId) {
206
+ var refOneIndex = ref.endpointIds.findIndex(function (endpointId) {
178
207
  return model.endpoints[endpointId].relation === '1';
179
208
  });
209
+ var refEndpointIndex = refOneIndex === -1 ? 0 : refOneIndex;
180
210
  var foreignEndpointId = ref.endpointIds[1 - refEndpointIndex];
181
211
  var refEndpointId = ref.endpointIds[refEndpointIndex];
182
212
  var foreignEndpoint = model.endpoints[foreignEndpointId];
@@ -193,23 +223,34 @@ var PostgresExporter = /*#__PURE__*/function () {
193
223
 
194
224
  var foreignEndpointFieldName = _this.buildFieldName(foreignEndpoint.fieldIds, model, 'postgres');
195
225
 
196
- var line = "ALTER TABLE ".concat((0, _utils.shouldPrintSchema)(foreignEndpointSchema, model) ? "\"".concat(foreignEndpointSchema.name, "\".") : '', "\"").concat(foreignEndpointTable.name, "\" ADD ");
226
+ if (refOneIndex === -1) {
227
+ // many to many relationship
228
+ var firstTableFieldsMap = (0, _utils.buildJunctionFields1)(refEndpoint.fieldIds, model);
229
+ var secondTableFieldsMap = (0, _utils.buildJunctionFields2)(foreignEndpoint.fieldIds, model, firstTableFieldsMap);
230
+ var newTableName = (0, _utils.buildNewTableName)(refEndpointTable.name, foreignEndpointTable.name, usedTableNames);
231
+ line += _this.buildTableManyToMany(firstTableFieldsMap, secondTableFieldsMap, newTableName, refEndpointSchema, model);
232
+ line += _this.buildForeignKeyManyToMany(firstTableFieldsMap, refEndpointFieldName, newTableName, refEndpointTable.name, refEndpointSchema, refEndpointSchema, model);
233
+ line += _this.buildForeignKeyManyToMany(secondTableFieldsMap, foreignEndpointFieldName, newTableName, foreignEndpointTable.name, refEndpointSchema, foreignEndpointSchema, model);
234
+ } else {
235
+ line = "ALTER TABLE ".concat((0, _utils.shouldPrintSchema)(foreignEndpointSchema, model) ? "\"".concat(foreignEndpointSchema.name, "\".") : '', "\"").concat(foreignEndpointTable.name, "\" ADD ");
197
236
 
198
- if (ref.name) {
199
- line += "CONSTRAINT \"".concat(ref.name, "\" ");
200
- }
237
+ if (ref.name) {
238
+ line += "CONSTRAINT \"".concat(ref.name, "\" ");
239
+ }
201
240
 
202
- line += "FOREIGN KEY ".concat(foreignEndpointFieldName, " REFERENCES ").concat((0, _utils.shouldPrintSchema)(refEndpointSchema, model) ? "\"".concat(refEndpointSchema.name, "\".") : '', "\"").concat(refEndpointTable.name, "\" ").concat(refEndpointFieldName);
241
+ line += "FOREIGN KEY ".concat(foreignEndpointFieldName, " REFERENCES ").concat((0, _utils.shouldPrintSchema)(refEndpointSchema, model) ? "\"".concat(refEndpointSchema.name, "\".") : '', "\"").concat(refEndpointTable.name, "\" ").concat(refEndpointFieldName);
203
242
 
204
- if (ref.onDelete) {
205
- line += " ON DELETE ".concat(ref.onDelete.toUpperCase());
206
- }
243
+ if (ref.onDelete) {
244
+ line += " ON DELETE ".concat(ref.onDelete.toUpperCase());
245
+ }
246
+
247
+ if (ref.onUpdate) {
248
+ line += " ON UPDATE ".concat(ref.onUpdate.toUpperCase());
249
+ }
207
250
 
208
- if (ref.onUpdate) {
209
- line += " ON UPDATE ".concat(ref.onUpdate.toUpperCase());
251
+ line += ';\n';
210
252
  }
211
253
 
212
- line += ';\n';
213
254
  return line;
214
255
  });
215
256
  return strArr;
@@ -297,6 +338,9 @@ var PostgresExporter = /*#__PURE__*/function () {
297
338
  key: "export",
298
339
  value: function _export(model) {
299
340
  var database = model.database['1'];
341
+ var usedTableNames = new Set(Object.values(model.tables).map(function (table) {
342
+ return table.name;
343
+ }));
300
344
  var statements = database.schemaIds.reduce(function (prevStatements, schemaId) {
301
345
  var schema = model.schemas[schemaId];
302
346
  var tableIds = schema.tableIds,
@@ -357,7 +401,7 @@ var PostgresExporter = /*#__PURE__*/function () {
357
401
  if (!_lodash["default"].isEmpty(refIds)) {
358
402
  var _prevStatements$refs;
359
403
 
360
- (_prevStatements$refs = prevStatements.refs).push.apply(_prevStatements$refs, _toConsumableArray(PostgresExporter.exportRefs(refIds, model)));
404
+ (_prevStatements$refs = prevStatements.refs).push.apply(_prevStatements$refs, _toConsumableArray(PostgresExporter.exportRefs(refIds, model, usedTableNames)));
361
405
  }
362
406
 
363
407
  return prevStatements;
@@ -143,6 +143,33 @@ var SqlServerExporter = /*#__PURE__*/function () {
143
143
  });
144
144
  return tableStrs;
145
145
  }
146
+ }, {
147
+ key: "buildTableManyToMany",
148
+ value: function buildTableManyToMany(firstTableFieldsMap, secondTableFieldsMap, tableName, refEndpointSchema, model) {
149
+ var line = "CREATE TABLE ".concat((0, _utils.shouldPrintSchema)(refEndpointSchema, model) ? "[".concat(refEndpointSchema.name, "].") : '', "[").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, ",\n");
157
+ });
158
+ secondTableFieldsMap.forEach(function (fieldType, fieldName) {
159
+ line += " [".concat(fieldName, "] ").concat(fieldType, ",\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, refEndpointSchema, foreignEndpointSchema, model) {
168
+ var refEndpointFields = _toConsumableArray(fieldsMap.keys()).join('], [');
169
+
170
+ var line = "ALTER TABLE ".concat((0, _utils.shouldPrintSchema)(refEndpointSchema, model) ? "[".concat(refEndpointSchema.name, "].") : '', "[").concat(refEndpointTableName, "] ADD FOREIGN KEY ([").concat(refEndpointFields, "]) REFERENCES ").concat((0, _utils.shouldPrintSchema)(foreignEndpointSchema, model) ? "[".concat(foreignEndpointSchema.name, "].") : '', "[").concat(foreignEndpointTableName, "] ").concat(foreignEndpointFields, ";\nGO\n\n");
171
+ return line;
172
+ }
146
173
  }, {
147
174
  key: "buildFieldName",
148
175
  value: function buildFieldName(fieldIds, model) {
@@ -153,14 +180,16 @@ var SqlServerExporter = /*#__PURE__*/function () {
153
180
  }
154
181
  }, {
155
182
  key: "exportRefs",
156
- value: function exportRefs(refIds, model) {
183
+ value: function exportRefs(refIds, model, usedTableNames) {
157
184
  var _this = this;
158
185
 
159
186
  var strArr = refIds.map(function (refId) {
187
+ var line = '';
160
188
  var ref = model.refs[refId];
161
- var refEndpointIndex = ref.endpointIds.findIndex(function (endpointId) {
189
+ var refOneIndex = ref.endpointIds.findIndex(function (endpointId) {
162
190
  return model.endpoints[endpointId].relation === '1';
163
191
  });
192
+ var refEndpointIndex = refOneIndex === -1 ? 0 : refOneIndex;
164
193
  var foreignEndpointId = ref.endpointIds[1 - refEndpointIndex];
165
194
  var refEndpointId = ref.endpointIds[refEndpointIndex];
166
195
  var foreignEndpoint = model.endpoints[foreignEndpointId];
@@ -177,23 +206,34 @@ var SqlServerExporter = /*#__PURE__*/function () {
177
206
 
178
207
  var foreignEndpointFieldName = _this.buildFieldName(foreignEndpoint.fieldIds, model, 'mssql');
179
208
 
180
- var line = "ALTER TABLE ".concat((0, _utils.shouldPrintSchema)(foreignEndpointSchema, model) ? "[".concat(foreignEndpointSchema.name, "].") : '', "[").concat(foreignEndpointTable.name, "] ADD ");
209
+ if (refOneIndex === -1) {
210
+ // many to many relationship
211
+ var firstTableFieldsMap = (0, _utils.buildJunctionFields1)(refEndpoint.fieldIds, model);
212
+ var secondTableFieldsMap = (0, _utils.buildJunctionFields2)(foreignEndpoint.fieldIds, model, firstTableFieldsMap);
213
+ var newTableName = (0, _utils.buildNewTableName)(refEndpointTable.name, foreignEndpointTable.name, usedTableNames);
214
+ line += _this.buildTableManyToMany(firstTableFieldsMap, secondTableFieldsMap, newTableName, refEndpointSchema, model);
215
+ line += _this.buildForeignKeyManyToMany(firstTableFieldsMap, refEndpointFieldName, newTableName, refEndpointTable.name, refEndpointSchema, refEndpointSchema, model);
216
+ line += _this.buildForeignKeyManyToMany(secondTableFieldsMap, foreignEndpointFieldName, newTableName, foreignEndpointTable.name, refEndpointSchema, foreignEndpointSchema, model);
217
+ } else {
218
+ line = "ALTER TABLE ".concat((0, _utils.shouldPrintSchema)(foreignEndpointSchema, model) ? "[".concat(foreignEndpointSchema.name, "].") : '', "[").concat(foreignEndpointTable.name, "] ADD ");
181
219
 
182
- if (ref.name) {
183
- line += "CONSTRAINT [".concat(ref.name, "] ");
184
- }
220
+ if (ref.name) {
221
+ line += "CONSTRAINT [".concat(ref.name, "] ");
222
+ }
185
223
 
186
- line += "FOREIGN KEY ".concat(foreignEndpointFieldName, " REFERENCES ").concat((0, _utils.shouldPrintSchema)(refEndpointSchema, model) ? "[".concat(refEndpointSchema.name, "].") : '', "[").concat(refEndpointTable.name, "] ").concat(refEndpointFieldName);
224
+ line += "FOREIGN KEY ".concat(foreignEndpointFieldName, " REFERENCES ").concat((0, _utils.shouldPrintSchema)(refEndpointSchema, model) ? "[".concat(refEndpointSchema.name, "].") : '', "[").concat(refEndpointTable.name, "] ").concat(refEndpointFieldName);
187
225
 
188
- if (ref.onDelete) {
189
- line += " ON DELETE ".concat(ref.onDelete.toUpperCase());
190
- }
226
+ if (ref.onDelete) {
227
+ line += " ON DELETE ".concat(ref.onDelete.toUpperCase());
228
+ }
229
+
230
+ if (ref.onUpdate) {
231
+ line += " ON UPDATE ".concat(ref.onUpdate.toUpperCase());
232
+ }
191
233
 
192
- if (ref.onUpdate) {
193
- line += " ON UPDATE ".concat(ref.onUpdate.toUpperCase());
234
+ line += '\nGO\n';
194
235
  }
195
236
 
196
- line += '\nGO\n';
197
237
  return line;
198
238
  });
199
239
  return strArr;
@@ -278,6 +318,9 @@ var SqlServerExporter = /*#__PURE__*/function () {
278
318
  key: "export",
279
319
  value: function _export(model) {
280
320
  var database = model.database['1'];
321
+ var usedTableNames = new Set(Object.values(model.tables).map(function (table) {
322
+ return table.name;
323
+ }));
281
324
  var statements = database.schemaIds.reduce(function (prevStatements, schemaId) {
282
325
  var schema = model.schemas[schemaId];
283
326
  var tableIds = schema.tableIds,
@@ -331,7 +374,7 @@ var SqlServerExporter = /*#__PURE__*/function () {
331
374
  if (!_lodash["default"].isEmpty(refIds)) {
332
375
  var _prevStatements$refs;
333
376
 
334
- (_prevStatements$refs = prevStatements.refs).push.apply(_prevStatements$refs, _toConsumableArray(SqlServerExporter.exportRefs(refIds, model)));
377
+ (_prevStatements$refs = prevStatements.refs).push.apply(_prevStatements$refs, _toConsumableArray(SqlServerExporter.exportRefs(refIds, model, usedTableNames)));
335
378
  }
336
379
 
337
380
  return prevStatements;
@@ -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
  }
@@ -15,6 +15,18 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "d
15
15
 
16
16
  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); }
17
17
 
18
+ function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
19
+
20
+ 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."); }
21
+
22
+ 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); }
23
+
24
+ function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && Symbol.iterator in Object(iter)) return Array.from(iter); }
25
+
26
+ function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
27
+
28
+ 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; }
29
+
18
30
  function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
19
31
 
20
32
  function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
@@ -140,11 +152,35 @@ var Endpoint = /*#__PURE__*/function (_Element) {
140
152
  value: function setFields(fieldNames, table) {
141
153
  var _this2 = this;
142
154
 
143
- fieldNames.forEach(function (fieldName) {
155
+ var newFieldNames = fieldNames && fieldNames.length ? _toConsumableArray(fieldNames) : [];
156
+
157
+ if (!newFieldNames.length) {
158
+ var fieldHasPK = table.fields.find(function (field) {
159
+ return field.pk;
160
+ });
161
+
162
+ if (fieldHasPK) {
163
+ newFieldNames.push(fieldHasPK.name);
164
+ } else {
165
+ var indexHasPK = table.indexes.find(function (index) {
166
+ return index.pk;
167
+ });
168
+
169
+ if (indexHasPK) {
170
+ newFieldNames = indexHasPK.columns.map(function (column) {
171
+ return column.value;
172
+ });
173
+ } else {
174
+ this.error("Can't find primary or composite key in table ".concat((0, _utils.shouldPrintSchema)(table.schema) ? "\"".concat(table.schema.name, "\".") : '', "\"").concat(table.name, "\""));
175
+ }
176
+ }
177
+ }
178
+
179
+ newFieldNames.forEach(function (fieldName) {
144
180
  var field = table.findField(fieldName);
145
181
 
146
182
  if (!field) {
147
- _this2.error("Can't find field ".concat((0, _utils.shouldPrintSchema)(table.schema) ? "\"".concat(table.schema.name, "\".") : '', "\"").concat(fieldName, "\" in table \"").concat(_this2.tableName, "\""));
183
+ _this2.error("Can't find field ".concat((0, _utils.shouldPrintSchema)(table.schema) ? "\"".concat(table.schema.name, "\".") : '', "\"").concat(fieldName, "\" in table \"").concat(table.name, "\""));
148
184
  }
149
185
 
150
186
  _this2.fields.push(field);
@@ -9,6 +9,12 @@
9
9
  project: {},
10
10
  };
11
11
  let projectCnt = 0;
12
+ function getRelations(operator) {
13
+ if(operator === '<>') return ['*','*'];
14
+ if(operator === '>') return ['*','1'];
15
+ if(operator === '<') return ['1','*'];
16
+ if(operator === '-') return ['1','1'];
17
+ }
12
18
  }
13
19
 
14
20
  rules
@@ -173,19 +179,20 @@ ref_short
173
179
 
174
180
  ref_body
175
181
  = field1:field_identifier sp+ relation:relation sp+ field2:field_identifier sp* ref_settings:RefSettings? {
182
+ const rel = getRelations(relation);
176
183
  const endpoints = [
177
184
  {
178
185
  schemaName: field1.schemaName,
179
186
  tableName: field1.tableName,
180
187
  fieldNames: field1.fieldNames,
181
- relation: relation === ">" ? "*" : "1",
188
+ relation: rel[0],
182
189
  token: location()
183
190
  },
184
191
  {
185
192
  schemaName: field2.schemaName,
186
193
  tableName: field2.tableName,
187
194
  fieldNames: field2.fieldNames,
188
- relation: relation === "<" ? "*" : "1",
195
+ relation: rel[1],
189
196
  token: location()
190
197
  }
191
198
  ];
@@ -243,19 +250,20 @@ TableSyntax
243
250
 
244
251
  fields.forEach((field) => {
245
252
  (field.inline_refs || []).forEach((iref) => {
253
+ const rel = getRelations(iref.relation);
246
254
  const endpoints = [
247
255
  {
248
256
  schemaName: iref.schemaName,
249
257
  tableName: iref.tableName,
250
258
  fieldNames: iref.fieldNames,
251
- relation: iref.relation === "<" ? "*" : "1",
259
+ relation: rel[1],
252
260
  token: iref.token
253
261
  },
254
262
  {
255
263
  schemaName: schemaName,
256
264
  tableName: name,
257
265
  fieldNames: [field.name],
258
- relation: iref.relation === ">" ? "*" : "1",
266
+ relation: rel[0],
259
267
  token: iref.token
260
268
  }];
261
269
 
@@ -611,7 +619,7 @@ set_null "set null" = "set null"i
611
619
  set_default "set default" = "set default"i
612
620
 
613
621
  // Commonly used tokens
614
- relation ">, - or <" = [>\-<]
622
+ relation "<>, >, - or <" = '<>' / '>' / '<' / '-'
615
623
  name "valid name"
616
624
  = c:(character+) { return c.join("") }
617
625
  / quote c:[^\"\n]+ quote { return c.join("") }