@dbml/core 2.5.2 → 2.5.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.
- package/lib/export/DbmlExporter.js +9 -64
- package/lib/export/JsonExporter.js +5 -11
- package/lib/export/ModelExporter.js +5 -22
- package/lib/export/MysqlExporter.js +8 -70
- package/lib/export/PostgresExporter.js +11 -80
- package/lib/export/SqlServerExporter.js +10 -71
- package/lib/export/index.js +0 -6
- package/lib/export/utils.js +2 -12
- package/lib/import/index.js +0 -7
- package/lib/index.js +4 -9
- package/lib/model_structure/config.js +1 -1
- package/lib/model_structure/database.js +32 -83
- package/lib/model_structure/dbState.js +5 -10
- package/lib/model_structure/element.js +13 -38
- package/lib/model_structure/endpoint.js +22 -68
- package/lib/model_structure/enum.js +18 -49
- package/lib/model_structure/enumValue.js +17 -44
- package/lib/model_structure/field.js +26 -62
- package/lib/model_structure/indexColumn.js +15 -40
- package/lib/model_structure/indexes.js +22 -50
- package/lib/model_structure/ref.js +24 -56
- package/lib/model_structure/schema.js +27 -65
- package/lib/model_structure/table.js +24 -59
- package/lib/model_structure/tableGroup.js +18 -49
- package/lib/model_structure/utils.js +0 -3
- package/lib/parse/Parser.js +5 -27
- package/lib/parse/buildParser.js +11 -23
- package/lib/parse/dbmlParser.js +863 -2131
- package/lib/parse/mssql/base_parsers.js +2 -11
- package/lib/parse/mssql/column_definition/actions.js +0 -3
- package/lib/parse/mssql/column_definition/index.js +9 -17
- package/lib/parse/mssql/constraint_definition/actions.js +6 -12
- package/lib/parse/mssql/constraint_definition/index.js +10 -18
- package/lib/parse/mssql/expression.js +6 -16
- package/lib/parse/mssql/fk_definition/actions.js +1 -12
- package/lib/parse/mssql/fk_definition/index.js +5 -10
- package/lib/parse/mssql/index.js +2 -4
- package/lib/parse/mssql/index_definition/actions.js +0 -6
- package/lib/parse/mssql/index_definition/index.js +8 -13
- package/lib/parse/mssql/keyword_parsers.js +2 -3
- package/lib/parse/mssql/keyword_utils.js +0 -4
- package/lib/parse/mssql/statements/actions.js +0 -24
- package/lib/parse/mssql/statements/index.js +1 -7
- package/lib/parse/mssql/statements/statement_types/alter_table/actions.js +3 -10
- package/lib/parse/mssql/statements/statement_types/alter_table/add/actions.js +2 -6
- package/lib/parse/mssql/statements/statement_types/alter_table/add/index.js +9 -18
- package/lib/parse/mssql/statements/statement_types/alter_table/index.js +2 -7
- package/lib/parse/mssql/statements/statement_types/comments/actions.js +3 -11
- package/lib/parse/mssql/statements/statement_types/comments/index.js +3 -13
- package/lib/parse/mssql/statements/statement_types/create_index/actions.js +1 -3
- package/lib/parse/mssql/statements/statement_types/create_index/index.js +9 -15
- package/lib/parse/mssql/statements/statement_types/create_table/actions.js +7 -18
- package/lib/parse/mssql/statements/statement_types/create_table/index.js +8 -16
- package/lib/parse/mssql/statements/statement_types/index.js +0 -4
- package/lib/parse/mssql/utils.js +4 -14
- package/lib/parse/mssql/whitespaces.js +0 -1
- package/lib/parse/mssqlParser.js +0 -3
- package/lib/parse/mysqlParser.js +822 -2395
- package/lib/parse/postgresParser.js +3 -3
- package/lib/parse/postgresql/get_parser.js +6 -9
- package/lib/parse/schemarbParser.js +219 -714
- package/package.json +10 -11
- package/types/.DS_Store +0 -0
|
@@ -4,36 +4,25 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports["default"] = void 0;
|
|
7
|
-
|
|
8
7
|
var _lodash = _interopRequireDefault(require("lodash"));
|
|
9
|
-
|
|
10
8
|
var _utils = require("./utils");
|
|
11
|
-
|
|
12
9
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
|
13
|
-
|
|
10
|
+
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
|
|
14
11
|
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
|
|
15
|
-
|
|
16
12
|
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
|
-
|
|
18
13
|
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
|
-
|
|
20
|
-
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && Symbol.iterator in Object(iter)) return Array.from(iter); }
|
|
21
|
-
|
|
14
|
+
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
|
|
22
15
|
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; }
|
|
25
|
-
|
|
16
|
+
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; }
|
|
26
17
|
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
27
|
-
|
|
28
|
-
function
|
|
29
|
-
|
|
30
|
-
function
|
|
31
|
-
|
|
18
|
+
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
|
|
19
|
+
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
|
|
20
|
+
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
|
|
21
|
+
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
|
|
32
22
|
var MySQLExporter = /*#__PURE__*/function () {
|
|
33
23
|
function MySQLExporter() {
|
|
34
24
|
_classCallCheck(this, MySQLExporter);
|
|
35
25
|
}
|
|
36
|
-
|
|
37
26
|
_createClass(MySQLExporter, null, [{
|
|
38
27
|
key: "getFieldLines",
|
|
39
28
|
value: function getFieldLines(tableId, model) {
|
|
@@ -41,36 +30,28 @@ var MySQLExporter = /*#__PURE__*/function () {
|
|
|
41
30
|
var lines = table.fieldIds.map(function (fieldId) {
|
|
42
31
|
var field = model.fields[fieldId];
|
|
43
32
|
var line = '';
|
|
44
|
-
|
|
45
33
|
if (field.enumId) {
|
|
46
34
|
var _enum = model.enums[field.enumId];
|
|
47
35
|
line = "`".concat(field.name, "` ENUM (");
|
|
48
|
-
|
|
49
36
|
var enumValues = _enum.valueIds.map(function (valueId) {
|
|
50
37
|
return "'".concat(model.enumValues[valueId].name, "'");
|
|
51
38
|
});
|
|
52
|
-
|
|
53
39
|
line += "".concat(enumValues.join(', '), ")");
|
|
54
40
|
} else {
|
|
55
41
|
line = "`".concat(field.name, "` ").concat(field.type.type_name !== 'varchar' ? field.type.type_name : 'varchar(255)');
|
|
56
42
|
}
|
|
57
|
-
|
|
58
43
|
if (field.unique) {
|
|
59
44
|
line += ' UNIQUE';
|
|
60
45
|
}
|
|
61
|
-
|
|
62
46
|
if (field.pk) {
|
|
63
47
|
line += ' PRIMARY KEY';
|
|
64
48
|
}
|
|
65
|
-
|
|
66
49
|
if (field.not_null) {
|
|
67
50
|
line += ' NOT NULL';
|
|
68
51
|
}
|
|
69
|
-
|
|
70
52
|
if (field.increment) {
|
|
71
53
|
line += ' AUTO_INCREMENT';
|
|
72
54
|
}
|
|
73
|
-
|
|
74
55
|
if (field.dbdefault) {
|
|
75
56
|
if (field.dbdefault.type === 'expression') {
|
|
76
57
|
line += " DEFAULT (".concat(field.dbdefault.value, ")");
|
|
@@ -80,11 +61,9 @@ var MySQLExporter = /*#__PURE__*/function () {
|
|
|
80
61
|
line += " DEFAULT ".concat(field.dbdefault.value);
|
|
81
62
|
}
|
|
82
63
|
}
|
|
83
|
-
|
|
84
64
|
if (field.note) {
|
|
85
65
|
line += " COMMENT '".concat(field.note.replace(/'/g, "\\'"), "'");
|
|
86
66
|
}
|
|
87
|
-
|
|
88
67
|
return line;
|
|
89
68
|
});
|
|
90
69
|
return lines;
|
|
@@ -103,13 +82,11 @@ var MySQLExporter = /*#__PURE__*/function () {
|
|
|
103
82
|
key.columnIds.forEach(function (columnId) {
|
|
104
83
|
var column = model.indexColumns[columnId];
|
|
105
84
|
var columnStr = '';
|
|
106
|
-
|
|
107
85
|
if (column.type === 'expression') {
|
|
108
86
|
columnStr = "(".concat(column.value, ")");
|
|
109
87
|
} else {
|
|
110
88
|
columnStr = "`".concat(column.value, "`");
|
|
111
89
|
}
|
|
112
|
-
|
|
113
90
|
columnArr.push(columnStr);
|
|
114
91
|
});
|
|
115
92
|
line += " (".concat(columnArr.join(', '), ")");
|
|
@@ -158,11 +135,8 @@ var MySQLExporter = /*#__PURE__*/function () {
|
|
|
158
135
|
key: "buildTableManyToMany",
|
|
159
136
|
value: function buildTableManyToMany(firstTableFieldsMap, secondTableFieldsMap, tableName, refEndpointSchema, model) {
|
|
160
137
|
var line = "CREATE TABLE ".concat((0, _utils.shouldPrintSchema)(refEndpointSchema, model) ? "`".concat(refEndpointSchema.name, "`.") : '', "`").concat(tableName, "` (\n");
|
|
161
|
-
|
|
162
138
|
var key1s = _toConsumableArray(firstTableFieldsMap.keys()).join('`, `');
|
|
163
|
-
|
|
164
139
|
var key2s = _toConsumableArray(secondTableFieldsMap.keys()).join('`, `');
|
|
165
|
-
|
|
166
140
|
firstTableFieldsMap.forEach(function (fieldType, fieldName) {
|
|
167
141
|
line += " `".concat(fieldName, "` ").concat(fieldType, ",\n");
|
|
168
142
|
});
|
|
@@ -177,7 +151,6 @@ var MySQLExporter = /*#__PURE__*/function () {
|
|
|
177
151
|
key: "buildForeignKeyManyToMany",
|
|
178
152
|
value: function buildForeignKeyManyToMany(fieldsMap, foreignEndpointFields, refEndpointTableName, foreignEndpointTableName, refEndpointSchema, foreignEndpointSchema, model) {
|
|
179
153
|
var refEndpointFields = _toConsumableArray(fieldsMap.keys()).join('`, `');
|
|
180
|
-
|
|
181
154
|
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
155
|
return line;
|
|
183
156
|
}
|
|
@@ -185,7 +158,6 @@ var MySQLExporter = /*#__PURE__*/function () {
|
|
|
185
158
|
key: "exportRefs",
|
|
186
159
|
value: function exportRefs(refIds, model, usedTableNames) {
|
|
187
160
|
var _this = this;
|
|
188
|
-
|
|
189
161
|
var strArr = refIds.map(function (refId) {
|
|
190
162
|
var line = '';
|
|
191
163
|
var ref = model.refs[refId];
|
|
@@ -200,15 +172,11 @@ var MySQLExporter = /*#__PURE__*/function () {
|
|
|
200
172
|
var refEndpointField = model.fields[refEndpoint.fieldIds[0]];
|
|
201
173
|
var refEndpointTable = model.tables[refEndpointField.tableId];
|
|
202
174
|
var refEndpointSchema = model.schemas[refEndpointTable.schemaId];
|
|
203
|
-
|
|
204
175
|
var refEndpointFieldName = _this.buildFieldName(refEndpoint.fieldIds, model, 'mysql');
|
|
205
|
-
|
|
206
176
|
var foreignEndpointField = model.fields[foreignEndpoint.fieldIds[0]];
|
|
207
177
|
var foreignEndpointTable = model.tables[foreignEndpointField.tableId];
|
|
208
178
|
var foreignEndpointSchema = model.schemas[foreignEndpointTable.schemaId];
|
|
209
|
-
|
|
210
179
|
var foreignEndpointFieldName = _this.buildFieldName(foreignEndpoint.fieldIds, model, 'mysql');
|
|
211
|
-
|
|
212
180
|
if (refOneIndex === -1) {
|
|
213
181
|
var firstTableFieldsMap = (0, _utils.buildJunctionFields1)(refEndpoint.fieldIds, model);
|
|
214
182
|
var secondTableFieldsMap = (0, _utils.buildJunctionFields2)(foreignEndpoint.fieldIds, model, firstTableFieldsMap);
|
|
@@ -218,24 +186,18 @@ var MySQLExporter = /*#__PURE__*/function () {
|
|
|
218
186
|
line += _this.buildForeignKeyManyToMany(secondTableFieldsMap, foreignEndpointFieldName, newTableName, foreignEndpointTable.name, refEndpointSchema, foreignEndpointSchema, model);
|
|
219
187
|
} else {
|
|
220
188
|
line = "ALTER TABLE ".concat((0, _utils.shouldPrintSchema)(foreignEndpointSchema, model) ? "`".concat(foreignEndpointSchema.name, "`.") : '', "`").concat(foreignEndpointTable.name, "` ADD ");
|
|
221
|
-
|
|
222
189
|
if (ref.name) {
|
|
223
190
|
line += "CONSTRAINT `".concat(ref.name, "` ");
|
|
224
191
|
}
|
|
225
|
-
|
|
226
192
|
line += "FOREIGN KEY ".concat(foreignEndpointFieldName, " REFERENCES ").concat((0, _utils.shouldPrintSchema)(refEndpointSchema, model) ? "`".concat(refEndpointSchema.name, "`.") : '', "`").concat(refEndpointTable.name, "` ").concat(refEndpointFieldName);
|
|
227
|
-
|
|
228
193
|
if (ref.onDelete) {
|
|
229
194
|
line += " ON DELETE ".concat(ref.onDelete.toUpperCase());
|
|
230
195
|
}
|
|
231
|
-
|
|
232
196
|
if (ref.onUpdate) {
|
|
233
197
|
line += " ON UPDATE ".concat(ref.onUpdate.toUpperCase());
|
|
234
198
|
}
|
|
235
|
-
|
|
236
199
|
line += ';\n';
|
|
237
200
|
}
|
|
238
|
-
|
|
239
201
|
return line;
|
|
240
202
|
});
|
|
241
203
|
return strArr;
|
|
@@ -251,32 +213,26 @@ var MySQLExporter = /*#__PURE__*/function () {
|
|
|
251
213
|
var table = model.tables[index.tableId];
|
|
252
214
|
var schema = model.schemas[table.schemaId];
|
|
253
215
|
var line = 'CREATE';
|
|
254
|
-
|
|
255
216
|
if (index.unique) {
|
|
256
217
|
line += ' UNIQUE';
|
|
257
218
|
}
|
|
258
|
-
|
|
259
219
|
var indexName = index.name ? "`".concat(index.name, "`") : "`".concat((0, _utils.shouldPrintSchema)(schema, model) ? "`".concat(schema.name, "`.") : '').concat(table.name, "_index_").concat(i, "`");
|
|
260
220
|
line += " INDEX ".concat(indexName, " ON ").concat((0, _utils.shouldPrintSchema)(schema, model) ? "`".concat(schema.name, "`.") : '', "`").concat(table.name, "`");
|
|
261
221
|
var columnArr = [];
|
|
262
222
|
index.columnIds.forEach(function (columnId) {
|
|
263
223
|
var column = model.indexColumns[columnId];
|
|
264
224
|
var columnStr = '';
|
|
265
|
-
|
|
266
225
|
if (column.type === 'expression') {
|
|
267
226
|
columnStr = "(".concat(column.value, ")");
|
|
268
227
|
} else {
|
|
269
228
|
columnStr = "`".concat(column.value, "`");
|
|
270
229
|
}
|
|
271
|
-
|
|
272
230
|
columnArr.push(columnStr);
|
|
273
231
|
});
|
|
274
232
|
line += " (".concat(columnArr.join(', '), ")");
|
|
275
|
-
|
|
276
233
|
if (index.type) {
|
|
277
234
|
line += " USING ".concat(index.type.toUpperCase());
|
|
278
235
|
}
|
|
279
|
-
|
|
280
236
|
line += ';\n';
|
|
281
237
|
return line;
|
|
282
238
|
});
|
|
@@ -287,13 +243,11 @@ var MySQLExporter = /*#__PURE__*/function () {
|
|
|
287
243
|
value: function exportComments(comments, model) {
|
|
288
244
|
var commentArr = comments.map(function (comment) {
|
|
289
245
|
var line = '';
|
|
290
|
-
|
|
291
246
|
if (comment.type === 'table') {
|
|
292
247
|
var table = model.tables[comment.tableId];
|
|
293
248
|
var schema = model.schemas[table.schemaId];
|
|
294
249
|
line += "ALTER TABLE ".concat((0, _utils.shouldPrintSchema)(schema, model) ? "`".concat(schema.name, "`.") : '', "`").concat(table.name, "` COMMENT = '").concat(table.note.replace(/'/g, "\\'"), "'");
|
|
295
250
|
}
|
|
296
|
-
|
|
297
251
|
line += ';\n';
|
|
298
252
|
return line;
|
|
299
253
|
});
|
|
@@ -309,28 +263,21 @@ var MySQLExporter = /*#__PURE__*/function () {
|
|
|
309
263
|
var statements = database.schemaIds.reduce(function (prevStatements, schemaId) {
|
|
310
264
|
var schema = model.schemas[schemaId];
|
|
311
265
|
var tableIds = schema.tableIds,
|
|
312
|
-
|
|
313
|
-
|
|
266
|
+
refIds = schema.refIds;
|
|
314
267
|
if ((0, _utils.shouldPrintSchema)(schema, model)) {
|
|
315
268
|
prevStatements.schemas.push("CREATE SCHEMA `".concat(schema.name, "`;\n"));
|
|
316
269
|
}
|
|
317
|
-
|
|
318
270
|
if (!_lodash["default"].isEmpty(tableIds)) {
|
|
319
271
|
var _prevStatements$table;
|
|
320
|
-
|
|
321
272
|
(_prevStatements$table = prevStatements.tables).push.apply(_prevStatements$table, _toConsumableArray(MySQLExporter.exportTables(tableIds, model)));
|
|
322
273
|
}
|
|
323
|
-
|
|
324
274
|
var indexIds = _lodash["default"].flatten(tableIds.map(function (tableId) {
|
|
325
275
|
return model.tables[tableId].indexIds;
|
|
326
276
|
}));
|
|
327
|
-
|
|
328
277
|
if (!_lodash["default"].isEmpty(indexIds)) {
|
|
329
278
|
var _prevStatements$index;
|
|
330
|
-
|
|
331
279
|
(_prevStatements$index = prevStatements.indexes).push.apply(_prevStatements$index, _toConsumableArray(MySQLExporter.exportIndexes(indexIds, model)));
|
|
332
280
|
}
|
|
333
|
-
|
|
334
281
|
var commentNodes = _lodash["default"].flatten(tableIds.map(function (tableId) {
|
|
335
282
|
var note = model.tables[tableId].note;
|
|
336
283
|
return note ? [{
|
|
@@ -338,19 +285,14 @@ var MySQLExporter = /*#__PURE__*/function () {
|
|
|
338
285
|
tableId: tableId
|
|
339
286
|
}] : [];
|
|
340
287
|
}));
|
|
341
|
-
|
|
342
288
|
if (!_lodash["default"].isEmpty(commentNodes)) {
|
|
343
289
|
var _prevStatements$comme;
|
|
344
|
-
|
|
345
290
|
(_prevStatements$comme = prevStatements.comments).push.apply(_prevStatements$comme, _toConsumableArray(MySQLExporter.exportComments(commentNodes, model)));
|
|
346
291
|
}
|
|
347
|
-
|
|
348
292
|
if (!_lodash["default"].isEmpty(refIds)) {
|
|
349
293
|
var _prevStatements$refs;
|
|
350
|
-
|
|
351
294
|
(_prevStatements$refs = prevStatements.refs).push.apply(_prevStatements$refs, _toConsumableArray(MySQLExporter.exportRefs(refIds, model, usedTableNames)));
|
|
352
295
|
}
|
|
353
|
-
|
|
354
296
|
return prevStatements;
|
|
355
297
|
}, {
|
|
356
298
|
schemas: [],
|
|
@@ -360,15 +302,11 @@ var MySQLExporter = /*#__PURE__*/function () {
|
|
|
360
302
|
comments: [],
|
|
361
303
|
refs: []
|
|
362
304
|
});
|
|
363
|
-
|
|
364
305
|
var res = _lodash["default"].concat(statements.schemas, statements.enums, statements.tables, statements.indexes, statements.comments, statements.refs).join('\n');
|
|
365
|
-
|
|
366
306
|
return res;
|
|
367
307
|
}
|
|
368
308
|
}]);
|
|
369
|
-
|
|
370
309
|
return MySQLExporter;
|
|
371
310
|
}();
|
|
372
|
-
|
|
373
311
|
var _default = MySQLExporter;
|
|
374
312
|
exports["default"] = _default;
|
|
@@ -4,50 +4,36 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports["default"] = void 0;
|
|
7
|
-
|
|
8
7
|
var _lodash = _interopRequireDefault(require("lodash"));
|
|
9
|
-
|
|
10
8
|
var _utils = require("./utils");
|
|
11
|
-
|
|
12
9
|
var _config = require("../model_structure/config");
|
|
13
|
-
|
|
14
10
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
|
15
|
-
|
|
11
|
+
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
|
|
16
12
|
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
|
|
17
|
-
|
|
18
13
|
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
14
|
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); }
|
|
21
|
-
|
|
22
|
-
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && Symbol.iterator in Object(iter)) return Array.from(iter); }
|
|
23
|
-
|
|
15
|
+
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
|
|
24
16
|
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
|
|
25
|
-
|
|
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; }
|
|
27
|
-
|
|
17
|
+
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; }
|
|
28
18
|
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
29
|
-
|
|
30
|
-
function
|
|
31
|
-
|
|
32
|
-
function
|
|
33
|
-
|
|
19
|
+
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
|
|
20
|
+
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
|
|
21
|
+
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
|
|
22
|
+
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
|
|
34
23
|
var PostgresExporter = /*#__PURE__*/function () {
|
|
35
24
|
function PostgresExporter() {
|
|
36
25
|
_classCallCheck(this, PostgresExporter);
|
|
37
26
|
}
|
|
38
|
-
|
|
39
27
|
_createClass(PostgresExporter, null, [{
|
|
40
28
|
key: "exportEnums",
|
|
41
29
|
value: function exportEnums(enumIds, model) {
|
|
42
30
|
var enumArr = enumIds.map(function (enumId) {
|
|
43
31
|
var _enum = model.enums[enumId];
|
|
44
32
|
var schema = model.schemas[_enum.schemaId];
|
|
45
|
-
|
|
46
33
|
var enumValueArr = _enum.valueIds.map(function (valueId) {
|
|
47
34
|
var value = model.enumValues[valueId];
|
|
48
35
|
return " '".concat(value.name, "'");
|
|
49
36
|
});
|
|
50
|
-
|
|
51
37
|
var enumValueStr = enumValueArr.join(',\n');
|
|
52
38
|
var line = "CREATE TYPE ".concat((0, _utils.shouldPrintSchema)(schema, model) ? "\"".concat(schema.name, "\".") : '', "\"").concat(_enum.name, "\" AS ENUM (\n").concat(enumValueStr, "\n);\n");
|
|
53
39
|
return line;
|
|
@@ -61,7 +47,6 @@ var PostgresExporter = /*#__PURE__*/function () {
|
|
|
61
47
|
var lines = table.fieldIds.map(function (fieldId) {
|
|
62
48
|
var field = model.fields[fieldId];
|
|
63
49
|
var line = '';
|
|
64
|
-
|
|
65
50
|
if (field.increment) {
|
|
66
51
|
var typicalIntergers = new Set(['BIGINT', 'INT', 'INTEGER', 'SMALLINT']);
|
|
67
52
|
var incrementIntergers = new Set(['SMALLSERIAL', 'SERIAL', 'BIGSERIAL']);
|
|
@@ -71,27 +56,21 @@ var PostgresExporter = /*#__PURE__*/function () {
|
|
|
71
56
|
line = "\"".concat(field.name, "\" ").concat(type);
|
|
72
57
|
} else {
|
|
73
58
|
var schemaName = '';
|
|
74
|
-
|
|
75
59
|
if (field.type.schemaName && field.type.schemaName !== _config.DEFAULT_SCHEMA_NAME) {
|
|
76
60
|
schemaName = (0, _utils.hasWhiteSpace)(field.type.schemaName) ? "\"".concat(field.type.schemaName, "\".") : "".concat(field.type.schemaName, ".");
|
|
77
61
|
}
|
|
78
|
-
|
|
79
62
|
var typeName = (0, _utils.hasWhiteSpace)(field.type.type_name) ? "\"".concat(field.type.type_name, "\"") : field.type.type_name;
|
|
80
63
|
line = "\"".concat(field.name, "\" ").concat(schemaName).concat(typeName);
|
|
81
64
|
}
|
|
82
|
-
|
|
83
65
|
if (field.unique) {
|
|
84
66
|
line += ' UNIQUE';
|
|
85
67
|
}
|
|
86
|
-
|
|
87
68
|
if (field.pk) {
|
|
88
69
|
line += ' PRIMARY KEY';
|
|
89
70
|
}
|
|
90
|
-
|
|
91
71
|
if (field.not_null) {
|
|
92
72
|
line += ' NOT NULL';
|
|
93
73
|
}
|
|
94
|
-
|
|
95
74
|
if (field.dbdefault) {
|
|
96
75
|
if (field.dbdefault.type === 'expression') {
|
|
97
76
|
line += " DEFAULT (".concat(field.dbdefault.value, ")");
|
|
@@ -101,7 +80,6 @@ var PostgresExporter = /*#__PURE__*/function () {
|
|
|
101
80
|
line += " DEFAULT ".concat(field.dbdefault.value);
|
|
102
81
|
}
|
|
103
82
|
}
|
|
104
|
-
|
|
105
83
|
return line;
|
|
106
84
|
});
|
|
107
85
|
return lines;
|
|
@@ -120,13 +98,11 @@ var PostgresExporter = /*#__PURE__*/function () {
|
|
|
120
98
|
key.columnIds.forEach(function (columnId) {
|
|
121
99
|
var column = model.indexColumns[columnId];
|
|
122
100
|
var columnStr = '';
|
|
123
|
-
|
|
124
101
|
if (column.type === 'expression') {
|
|
125
102
|
columnStr = "(".concat(column.value, ")");
|
|
126
103
|
} else {
|
|
127
104
|
columnStr = "\"".concat(column.value, "\"");
|
|
128
105
|
}
|
|
129
|
-
|
|
130
106
|
columnArr.push(columnStr);
|
|
131
107
|
});
|
|
132
108
|
line += " (".concat(columnArr.join(', '), ")");
|
|
@@ -175,11 +151,8 @@ var PostgresExporter = /*#__PURE__*/function () {
|
|
|
175
151
|
key: "buildTableManyToMany",
|
|
176
152
|
value: function buildTableManyToMany(firstTableFieldsMap, secondTableFieldsMap, tableName, refEndpointSchema, model) {
|
|
177
153
|
var line = "CREATE TABLE ".concat((0, _utils.shouldPrintSchema)(refEndpointSchema, model) ? "\"".concat(refEndpointSchema.name, "\".") : '', "\"").concat(tableName, "\" (\n");
|
|
178
|
-
|
|
179
154
|
var key1s = _toConsumableArray(firstTableFieldsMap.keys()).join('", "');
|
|
180
|
-
|
|
181
155
|
var key2s = _toConsumableArray(secondTableFieldsMap.keys()).join('", "');
|
|
182
|
-
|
|
183
156
|
firstTableFieldsMap.forEach(function (fieldType, fieldName) {
|
|
184
157
|
line += " \"".concat(fieldName, "\" ").concat(fieldType, ",\n");
|
|
185
158
|
});
|
|
@@ -194,7 +167,6 @@ var PostgresExporter = /*#__PURE__*/function () {
|
|
|
194
167
|
key: "buildForeignKeyManyToMany",
|
|
195
168
|
value: function buildForeignKeyManyToMany(fieldsMap, foreignEndpointFields, refEndpointTableName, foreignEndpointTableName, refEndpointSchema, foreignEndpointSchema, model) {
|
|
196
169
|
var refEndpointFields = _toConsumableArray(fieldsMap.keys()).join('", "');
|
|
197
|
-
|
|
198
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, ";\n\n");
|
|
199
171
|
return line;
|
|
200
172
|
}
|
|
@@ -202,7 +174,6 @@ var PostgresExporter = /*#__PURE__*/function () {
|
|
|
202
174
|
key: "exportRefs",
|
|
203
175
|
value: function exportRefs(refIds, model, usedTableNames) {
|
|
204
176
|
var _this = this;
|
|
205
|
-
|
|
206
177
|
var strArr = refIds.map(function (refId) {
|
|
207
178
|
var line = '';
|
|
208
179
|
var ref = model.refs[refId];
|
|
@@ -217,15 +188,11 @@ var PostgresExporter = /*#__PURE__*/function () {
|
|
|
217
188
|
var refEndpointField = model.fields[refEndpoint.fieldIds[0]];
|
|
218
189
|
var refEndpointTable = model.tables[refEndpointField.tableId];
|
|
219
190
|
var refEndpointSchema = model.schemas[refEndpointTable.schemaId];
|
|
220
|
-
|
|
221
191
|
var refEndpointFieldName = _this.buildFieldName(refEndpoint.fieldIds, model, 'postgres');
|
|
222
|
-
|
|
223
192
|
var foreignEndpointField = model.fields[foreignEndpoint.fieldIds[0]];
|
|
224
193
|
var foreignEndpointTable = model.tables[foreignEndpointField.tableId];
|
|
225
194
|
var foreignEndpointSchema = model.schemas[foreignEndpointTable.schemaId];
|
|
226
|
-
|
|
227
195
|
var foreignEndpointFieldName = _this.buildFieldName(foreignEndpoint.fieldIds, model, 'postgres');
|
|
228
|
-
|
|
229
196
|
if (refOneIndex === -1) {
|
|
230
197
|
// many to many relationship
|
|
231
198
|
var firstTableFieldsMap = (0, _utils.buildJunctionFields1)(refEndpoint.fieldIds, model);
|
|
@@ -236,24 +203,18 @@ var PostgresExporter = /*#__PURE__*/function () {
|
|
|
236
203
|
line += _this.buildForeignKeyManyToMany(secondTableFieldsMap, foreignEndpointFieldName, newTableName, foreignEndpointTable.name, refEndpointSchema, foreignEndpointSchema, model);
|
|
237
204
|
} else {
|
|
238
205
|
line = "ALTER TABLE ".concat((0, _utils.shouldPrintSchema)(foreignEndpointSchema, model) ? "\"".concat(foreignEndpointSchema.name, "\".") : '', "\"").concat(foreignEndpointTable.name, "\" ADD ");
|
|
239
|
-
|
|
240
206
|
if (ref.name) {
|
|
241
207
|
line += "CONSTRAINT \"".concat(ref.name, "\" ");
|
|
242
208
|
}
|
|
243
|
-
|
|
244
209
|
line += "FOREIGN KEY ".concat(foreignEndpointFieldName, " REFERENCES ").concat((0, _utils.shouldPrintSchema)(refEndpointSchema, model) ? "\"".concat(refEndpointSchema.name, "\".") : '', "\"").concat(refEndpointTable.name, "\" ").concat(refEndpointFieldName);
|
|
245
|
-
|
|
246
210
|
if (ref.onDelete) {
|
|
247
211
|
line += " ON DELETE ".concat(ref.onDelete.toUpperCase());
|
|
248
212
|
}
|
|
249
|
-
|
|
250
213
|
if (ref.onUpdate) {
|
|
251
214
|
line += " ON UPDATE ".concat(ref.onUpdate.toUpperCase());
|
|
252
215
|
}
|
|
253
|
-
|
|
254
216
|
line += ';\n';
|
|
255
217
|
}
|
|
256
|
-
|
|
257
218
|
return line;
|
|
258
219
|
});
|
|
259
220
|
return strArr;
|
|
@@ -269,35 +230,27 @@ var PostgresExporter = /*#__PURE__*/function () {
|
|
|
269
230
|
var table = model.tables[index.tableId];
|
|
270
231
|
var schema = model.schemas[table.schemaId];
|
|
271
232
|
var line = 'CREATE';
|
|
272
|
-
|
|
273
233
|
if (index.unique) {
|
|
274
234
|
line += ' UNIQUE';
|
|
275
235
|
}
|
|
276
|
-
|
|
277
236
|
var indexName = index.name ? "\"".concat(index.name, "\"") : '';
|
|
278
237
|
line += ' INDEX';
|
|
279
|
-
|
|
280
238
|
if (indexName) {
|
|
281
239
|
line += " ".concat(indexName);
|
|
282
240
|
}
|
|
283
|
-
|
|
284
241
|
line += " ON ".concat((0, _utils.shouldPrintSchema)(schema, model) ? "\"".concat(schema.name, "\".") : '', "\"").concat(table.name, "\"");
|
|
285
|
-
|
|
286
242
|
if (index.type) {
|
|
287
243
|
line += " USING ".concat(index.type.toUpperCase());
|
|
288
244
|
}
|
|
289
|
-
|
|
290
245
|
var columnArr = [];
|
|
291
246
|
index.columnIds.forEach(function (columnId) {
|
|
292
247
|
var column = model.indexColumns[columnId];
|
|
293
248
|
var columnStr = '';
|
|
294
|
-
|
|
295
249
|
if (column.type === 'expression') {
|
|
296
250
|
columnStr = "(".concat(column.value, ")");
|
|
297
251
|
} else {
|
|
298
252
|
columnStr = "\"".concat(column.value, "\"");
|
|
299
253
|
}
|
|
300
|
-
|
|
301
254
|
columnArr.push(columnStr);
|
|
302
255
|
});
|
|
303
256
|
line += " (".concat(columnArr.join(', '), ")");
|
|
@@ -313,25 +266,21 @@ var PostgresExporter = /*#__PURE__*/function () {
|
|
|
313
266
|
var line = 'COMMENT ON';
|
|
314
267
|
var table = model.tables[comment.tableId];
|
|
315
268
|
var schema = model.schemas[table.schemaId];
|
|
316
|
-
|
|
317
269
|
switch (comment.type) {
|
|
318
270
|
case 'table':
|
|
319
271
|
{
|
|
320
272
|
line += " TABLE ".concat((0, _utils.shouldPrintSchema)(schema, model) ? "\"".concat(schema.name, "\".") : '', "\"").concat(table.name, "\" IS '").concat(table.note.replace(/'/g, "\""), "'");
|
|
321
273
|
break;
|
|
322
274
|
}
|
|
323
|
-
|
|
324
275
|
case 'column':
|
|
325
276
|
{
|
|
326
277
|
var field = model.fields[comment.fieldId];
|
|
327
278
|
line += " COLUMN ".concat((0, _utils.shouldPrintSchema)(schema, model) ? "\"".concat(schema.name, "\".") : '', "\"").concat(table.name, "\".\"").concat(field.name, "\" IS '").concat(field.note.replace(/'/g, "\""), "'");
|
|
328
279
|
break;
|
|
329
280
|
}
|
|
330
|
-
|
|
331
281
|
default:
|
|
332
282
|
break;
|
|
333
283
|
}
|
|
334
|
-
|
|
335
284
|
line += ';\n';
|
|
336
285
|
return line;
|
|
337
286
|
});
|
|
@@ -347,39 +296,30 @@ var PostgresExporter = /*#__PURE__*/function () {
|
|
|
347
296
|
var statements = database.schemaIds.reduce(function (prevStatements, schemaId) {
|
|
348
297
|
var schema = model.schemas[schemaId];
|
|
349
298
|
var tableIds = schema.tableIds,
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
299
|
+
enumIds = schema.enumIds,
|
|
300
|
+
refIds = schema.refIds;
|
|
353
301
|
if ((0, _utils.shouldPrintSchema)(schema, model)) {
|
|
354
302
|
prevStatements.schemas.push("CREATE SCHEMA \"".concat(schema.name, "\";\n"));
|
|
355
303
|
}
|
|
356
|
-
|
|
357
304
|
if (!_lodash["default"].isEmpty(enumIds)) {
|
|
358
305
|
var _prevStatements$enums;
|
|
359
|
-
|
|
360
306
|
(_prevStatements$enums = prevStatements.enums).push.apply(_prevStatements$enums, _toConsumableArray(PostgresExporter.exportEnums(enumIds, model)));
|
|
361
307
|
}
|
|
362
|
-
|
|
363
308
|
if (!_lodash["default"].isEmpty(tableIds)) {
|
|
364
309
|
var _prevStatements$table;
|
|
365
|
-
|
|
366
310
|
(_prevStatements$table = prevStatements.tables).push.apply(_prevStatements$table, _toConsumableArray(PostgresExporter.exportTables(tableIds, model)));
|
|
367
311
|
}
|
|
368
|
-
|
|
369
312
|
var indexIds = _lodash["default"].flatten(tableIds.map(function (tableId) {
|
|
370
313
|
return model.tables[tableId].indexIds;
|
|
371
314
|
}));
|
|
372
|
-
|
|
373
315
|
if (!_lodash["default"].isEmpty(indexIds)) {
|
|
374
316
|
var _prevStatements$index;
|
|
375
|
-
|
|
376
317
|
(_prevStatements$index = prevStatements.indexes).push.apply(_prevStatements$index, _toConsumableArray(PostgresExporter.exportIndexes(indexIds, model)));
|
|
377
318
|
}
|
|
378
|
-
|
|
379
319
|
var commentNodes = _lodash["default"].flatten(tableIds.map(function (tableId) {
|
|
380
320
|
var _model$tables$tableId = model.tables[tableId],
|
|
381
|
-
|
|
382
|
-
|
|
321
|
+
fieldIds = _model$tables$tableId.fieldIds,
|
|
322
|
+
note = _model$tables$tableId.note;
|
|
383
323
|
var fieldObjects = fieldIds.filter(function (fieldId) {
|
|
384
324
|
return model.fields[fieldId].note;
|
|
385
325
|
}).map(function (fieldId) {
|
|
@@ -394,19 +334,14 @@ var PostgresExporter = /*#__PURE__*/function () {
|
|
|
394
334
|
tableId: tableId
|
|
395
335
|
}].concat(fieldObjects) : fieldObjects;
|
|
396
336
|
}));
|
|
397
|
-
|
|
398
337
|
if (!_lodash["default"].isEmpty(commentNodes)) {
|
|
399
338
|
var _prevStatements$comme;
|
|
400
|
-
|
|
401
339
|
(_prevStatements$comme = prevStatements.comments).push.apply(_prevStatements$comme, _toConsumableArray(PostgresExporter.exportComments(commentNodes, model)));
|
|
402
340
|
}
|
|
403
|
-
|
|
404
341
|
if (!_lodash["default"].isEmpty(refIds)) {
|
|
405
342
|
var _prevStatements$refs;
|
|
406
|
-
|
|
407
343
|
(_prevStatements$refs = prevStatements.refs).push.apply(_prevStatements$refs, _toConsumableArray(PostgresExporter.exportRefs(refIds, model, usedTableNames)));
|
|
408
344
|
}
|
|
409
|
-
|
|
410
345
|
return prevStatements;
|
|
411
346
|
}, {
|
|
412
347
|
schemas: [],
|
|
@@ -416,15 +351,11 @@ var PostgresExporter = /*#__PURE__*/function () {
|
|
|
416
351
|
comments: [],
|
|
417
352
|
refs: []
|
|
418
353
|
});
|
|
419
|
-
|
|
420
354
|
var res = _lodash["default"].concat(statements.schemas, statements.enums, statements.tables, statements.indexes, statements.comments, statements.refs).join('\n');
|
|
421
|
-
|
|
422
355
|
return res;
|
|
423
356
|
}
|
|
424
357
|
}]);
|
|
425
|
-
|
|
426
358
|
return PostgresExporter;
|
|
427
359
|
}();
|
|
428
|
-
|
|
429
360
|
var _default = PostgresExporter;
|
|
430
361
|
exports["default"] = _default;
|