@dbml/core 3.3.0 → 3.4.1-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/export/ModelExporter.js +5 -1
- package/lib/export/OracleExporter.js +453 -0
- package/lib/export/README.md +32 -0
- package/lib/export/utils.js +44 -1
- package/lib/index.js +7 -0
- package/lib/model_structure/element.js +1 -0
- package/lib/model_structure/note.js +72 -0
- package/lib/parse/Parser.js +44 -41
- package/lib/parse/error.js +40 -0
- package/package.json +3 -3
- package/types/export/ModelExporter.d.ts +3 -1
- package/types/export/index.d.ts +3 -1
- package/types/index.d.ts +1 -0
- package/types/parse/error.d.ts +28 -0
|
@@ -9,6 +9,7 @@ var _MysqlExporter = _interopRequireDefault(require("./MysqlExporter"));
|
|
|
9
9
|
var _PostgresExporter = _interopRequireDefault(require("./PostgresExporter"));
|
|
10
10
|
var _JsonExporter = _interopRequireDefault(require("./JsonExporter"));
|
|
11
11
|
var _SqlServerExporter = _interopRequireDefault(require("./SqlServerExporter"));
|
|
12
|
+
var _OracleExporter = _interopRequireDefault(require("./OracleExporter"));
|
|
12
13
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
|
13
14
|
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
15
|
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
@@ -24,7 +25,7 @@ var ModelExporter = /*#__PURE__*/function () {
|
|
|
24
25
|
key: "export",
|
|
25
26
|
value: function _export() {
|
|
26
27
|
var model = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
27
|
-
var format = arguments.length > 1 ? arguments[1] :
|
|
28
|
+
var format = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
|
|
28
29
|
var isNormalized = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
|
|
29
30
|
var res = '';
|
|
30
31
|
var normalizedModel = isNormalized ? model : model.normalize();
|
|
@@ -44,6 +45,9 @@ var ModelExporter = /*#__PURE__*/function () {
|
|
|
44
45
|
case 'mssql':
|
|
45
46
|
res = _SqlServerExporter["default"]["export"](normalizedModel);
|
|
46
47
|
break;
|
|
48
|
+
case 'oracle':
|
|
49
|
+
res = _OracleExporter["default"]["export"](normalizedModel);
|
|
50
|
+
break;
|
|
47
51
|
default:
|
|
48
52
|
break;
|
|
49
53
|
}
|
|
@@ -0,0 +1,453 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports["default"] = void 0;
|
|
7
|
+
var _lodash = _interopRequireDefault(require("lodash"));
|
|
8
|
+
var _utils = require("./utils");
|
|
9
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
|
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); }
|
|
11
|
+
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
|
|
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."); }
|
|
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); }
|
|
14
|
+
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
|
|
15
|
+
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
|
|
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; }
|
|
17
|
+
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
|
18
|
+
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
|
19
|
+
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
20
|
+
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
21
|
+
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); } }
|
|
22
|
+
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
|
|
23
|
+
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
|
|
24
|
+
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); }
|
|
25
|
+
var OracleExporter = /*#__PURE__*/function () {
|
|
26
|
+
function OracleExporter() {
|
|
27
|
+
_classCallCheck(this, OracleExporter);
|
|
28
|
+
}
|
|
29
|
+
_createClass(OracleExporter, null, [{
|
|
30
|
+
key: "buildSchemaToTableNameSetMap",
|
|
31
|
+
value: function buildSchemaToTableNameSetMap(model) {
|
|
32
|
+
var schemaToTableNameSetMap = new Map();
|
|
33
|
+
_lodash["default"].forEach(model.tables, function (table) {
|
|
34
|
+
var schema = model.schemas[table.schemaId];
|
|
35
|
+
var tableSet = schemaToTableNameSetMap.get(schema.name);
|
|
36
|
+
if (!tableSet) {
|
|
37
|
+
schemaToTableNameSetMap.set(schema.name, new Set([table.name]));
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
tableSet.add(table.name);
|
|
41
|
+
});
|
|
42
|
+
return schemaToTableNameSetMap;
|
|
43
|
+
}
|
|
44
|
+
}, {
|
|
45
|
+
key: "buildTableNameWithSchema",
|
|
46
|
+
value: function buildTableNameWithSchema(model, schema, table) {
|
|
47
|
+
return "".concat((0, _utils.shouldPrintSchema)(schema, model) ? "".concat((0, _utils.escapeObjectName)(schema.name, 'oracle'), ".") : '').concat((0, _utils.escapeObjectName)(table.name, 'oracle'));
|
|
48
|
+
}
|
|
49
|
+
}, {
|
|
50
|
+
key: "exportSchema",
|
|
51
|
+
value: function exportSchema(schemaName) {
|
|
52
|
+
// According to Oracle, CREATE SCHEMA statement does not actually create a schema and it automatically creates a schema when we create a user
|
|
53
|
+
// Learn more: https://docs.oracle.com/en/database/oracle/oracle-database/19/sqlrf/CREATE-SCHEMA.html#GUID-2D154F9C-9E2B-4A09-B658-2EA5B99AC838__GUID-CC0A5080-2AF3-4460-AB2B-DEA6C79519BA
|
|
54
|
+
return "CREATE USER ".concat((0, _utils.escapeObjectName)(schemaName, 'oracle'), "\n") + 'NO AUTHENTICATION\n' + 'DEFAULT TABLESPACE system\n' + 'TEMPORARY TABLESPACE temp\n' + 'QUOTA UNLIMITED ON system;\n';
|
|
55
|
+
}
|
|
56
|
+
}, {
|
|
57
|
+
key: "getFieldLines",
|
|
58
|
+
value: function getFieldLines(tableId, model) {
|
|
59
|
+
var table = model.tables[tableId];
|
|
60
|
+
var lines = table.fieldIds.map(function (fieldId) {
|
|
61
|
+
var field = model.fields[fieldId];
|
|
62
|
+
var fieldName = (0, _utils.escapeObjectName)(field.name, 'oracle');
|
|
63
|
+
var line = fieldName;
|
|
64
|
+
if (field.enumId) {
|
|
65
|
+
var _enum = model.enums[field.enumId];
|
|
66
|
+
var enumValues = _enum.valueIds.map(function (valueId) {
|
|
67
|
+
var value = model.enumValues[valueId];
|
|
68
|
+
return "'".concat(value.name, "'");
|
|
69
|
+
});
|
|
70
|
+
var enumString = enumValues.join(', ');
|
|
71
|
+
line += " nvarchar2(255) NOT NULL CHECK (".concat(fieldName, " IN (").concat(enumString, "))");
|
|
72
|
+
} else {
|
|
73
|
+
line += " ".concat(field.type.type_name);
|
|
74
|
+
}
|
|
75
|
+
var cloneField = _objectSpread({}, field);
|
|
76
|
+
if (cloneField.increment) {
|
|
77
|
+
line += ' GENERATED AS IDENTITY';
|
|
78
|
+
|
|
79
|
+
// in oracle, increment means using identity. If a clause includes identity, we must ignore not null + default value
|
|
80
|
+
cloneField.dbdefault = null;
|
|
81
|
+
cloneField.not_null = false;
|
|
82
|
+
}
|
|
83
|
+
if (cloneField.unique) {
|
|
84
|
+
line += ' UNIQUE';
|
|
85
|
+
}
|
|
86
|
+
if (cloneField.pk) {
|
|
87
|
+
line += ' PRIMARY KEY';
|
|
88
|
+
}
|
|
89
|
+
if (cloneField.not_null) {
|
|
90
|
+
line += ' NOT NULL';
|
|
91
|
+
}
|
|
92
|
+
if (cloneField.dbdefault) {
|
|
93
|
+
if (cloneField.dbdefault.type === 'string') {
|
|
94
|
+
line += " DEFAULT '".concat(cloneField.dbdefault.value, "'");
|
|
95
|
+
} else {
|
|
96
|
+
line += " DEFAULT ".concat(cloneField.dbdefault.value);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
return line;
|
|
100
|
+
});
|
|
101
|
+
return lines;
|
|
102
|
+
}
|
|
103
|
+
}, {
|
|
104
|
+
key: "getCompositePKs",
|
|
105
|
+
value: function getCompositePKs(tableId, model) {
|
|
106
|
+
var table = model.tables[tableId];
|
|
107
|
+
var compositePkIds = table.indexIds ? table.indexIds.filter(function (indexId) {
|
|
108
|
+
return model.indexes[indexId].pk;
|
|
109
|
+
}) : [];
|
|
110
|
+
var lines = compositePkIds.map(function (keyId) {
|
|
111
|
+
var key = model.indexes[keyId];
|
|
112
|
+
var line = 'PRIMARY KEY';
|
|
113
|
+
var columnArr = [];
|
|
114
|
+
key.columnIds.forEach(function (columnId) {
|
|
115
|
+
var column = model.indexColumns[columnId];
|
|
116
|
+
var columnStr = '';
|
|
117
|
+
if (column.type === 'expression') {
|
|
118
|
+
columnStr = "(".concat(column.value, ")");
|
|
119
|
+
} else {
|
|
120
|
+
columnStr = "\"".concat(column.value, "\"");
|
|
121
|
+
}
|
|
122
|
+
columnArr.push(columnStr);
|
|
123
|
+
});
|
|
124
|
+
line += " (".concat(columnArr.join(', '), ")");
|
|
125
|
+
return line;
|
|
126
|
+
});
|
|
127
|
+
return lines;
|
|
128
|
+
}
|
|
129
|
+
}, {
|
|
130
|
+
key: "getTableContents",
|
|
131
|
+
value: function getTableContents(tableIds, model) {
|
|
132
|
+
var _this = this;
|
|
133
|
+
var tableContentArr = tableIds.map(function (tableId) {
|
|
134
|
+
var fieldContents = _this.getFieldLines(tableId, model);
|
|
135
|
+
var compositePKs = _this.getCompositePKs(tableId, model);
|
|
136
|
+
return {
|
|
137
|
+
tableId: tableId,
|
|
138
|
+
fieldContents: fieldContents,
|
|
139
|
+
compositePKs: compositePKs
|
|
140
|
+
};
|
|
141
|
+
});
|
|
142
|
+
return tableContentArr;
|
|
143
|
+
}
|
|
144
|
+
}, {
|
|
145
|
+
key: "exportTables",
|
|
146
|
+
value: function exportTables(tableIds, model) {
|
|
147
|
+
var _this2 = this;
|
|
148
|
+
var tableContentList = this.getTableContents(tableIds, model);
|
|
149
|
+
var tableStrs = tableContentList.map(function (tableContent) {
|
|
150
|
+
var content = [].concat(_toConsumableArray(tableContent.fieldContents), _toConsumableArray(tableContent.compositePKs));
|
|
151
|
+
var table = model.tables[tableContent.tableId];
|
|
152
|
+
var schema = model.schemas[table.schemaId];
|
|
153
|
+
var tableName = _this2.buildTableNameWithSchema(model, schema, table);
|
|
154
|
+
var contentString = content.map(function (line) {
|
|
155
|
+
return " ".concat(line);
|
|
156
|
+
}).join(',\n');
|
|
157
|
+
var tableStr = "CREATE TABLE ".concat(tableName, " (\n").concat(contentString, "\n);\n");
|
|
158
|
+
return tableStr;
|
|
159
|
+
});
|
|
160
|
+
return tableStrs;
|
|
161
|
+
}
|
|
162
|
+
}, {
|
|
163
|
+
key: "buildReferenceFieldNamesString",
|
|
164
|
+
value: function buildReferenceFieldNamesString(fieldIds, model) {
|
|
165
|
+
var fieldNames = fieldIds.map(function (fieldId) {
|
|
166
|
+
return "\"".concat(model.fields[fieldId].name, "\"");
|
|
167
|
+
}).join(', ');
|
|
168
|
+
return "(".concat(fieldNames, ")");
|
|
169
|
+
}
|
|
170
|
+
}, {
|
|
171
|
+
key: "buildTableManyToMany",
|
|
172
|
+
value: function buildTableManyToMany(firstTableFieldsMap, secondTableFieldsMap, tableName) {
|
|
173
|
+
var line = "CREATE TABLE ".concat(tableName, " (\n");
|
|
174
|
+
firstTableFieldsMap.forEach(function (fieldType, fieldName) {
|
|
175
|
+
line += " \"".concat(fieldName, "\" ").concat(fieldType, ",\n");
|
|
176
|
+
});
|
|
177
|
+
secondTableFieldsMap.forEach(function (fieldType, fieldName) {
|
|
178
|
+
line += " \"".concat(fieldName, "\" ").concat(fieldType, ",\n");
|
|
179
|
+
});
|
|
180
|
+
var key1s = _toConsumableArray(firstTableFieldsMap.keys()).join('`, `');
|
|
181
|
+
var key2s = _toConsumableArray(secondTableFieldsMap.keys()).join('`, `');
|
|
182
|
+
line += " PRIMARY KEY (\"".concat(key1s, "\", \"").concat(key2s, "\")\n);\n");
|
|
183
|
+
return line;
|
|
184
|
+
}
|
|
185
|
+
}, {
|
|
186
|
+
key: "buildForeignKeyManyToMany",
|
|
187
|
+
value: function buildForeignKeyManyToMany(foreignEndpointTableName, foreignEndpointFields, refEndpointTableName, refEndpointFieldsString) {
|
|
188
|
+
var foreignEndpointFieldsString = _toConsumableArray(foreignEndpointFields.keys()).join('`, `');
|
|
189
|
+
var line = "ALTER TABLE ".concat(foreignEndpointTableName, " ADD FOREIGN KEY (\"").concat(foreignEndpointFieldsString, "\") REFERENCES ").concat(refEndpointTableName, " ").concat(refEndpointFieldsString, ";\n");
|
|
190
|
+
return line;
|
|
191
|
+
}
|
|
192
|
+
}, {
|
|
193
|
+
key: "exportReferencesAndNewTablesIfExists",
|
|
194
|
+
value: function exportReferencesAndNewTablesIfExists(refIds, model, usedTableNameMap) {
|
|
195
|
+
var _this3 = this;
|
|
196
|
+
var result = {
|
|
197
|
+
refs: [],
|
|
198
|
+
tables: []
|
|
199
|
+
};
|
|
200
|
+
refIds.forEach(function (refId) {
|
|
201
|
+
var ref = model.refs[refId];
|
|
202
|
+
|
|
203
|
+
// find the one relation in one-to-xxx or xxx-to-one relationship
|
|
204
|
+
var refOneIndex = ref.endpointIds.findIndex(function (endpointId) {
|
|
205
|
+
return model.endpoints[endpointId].relation === '1';
|
|
206
|
+
});
|
|
207
|
+
var refEndpointIndex = refOneIndex === -1 ? 0 : refOneIndex;
|
|
208
|
+
|
|
209
|
+
// refEndpointIndex could be 0 or 1, so use 1 - refEndpointIndex will take the remained index
|
|
210
|
+
var foreignEndpointId = ref.endpointIds[1 - refEndpointIndex];
|
|
211
|
+
var foreignEndpoint = model.endpoints[foreignEndpointId];
|
|
212
|
+
var foreignEndpointField = model.fields[foreignEndpoint.fieldIds[0]];
|
|
213
|
+
var foreignEndpointTable = model.tables[foreignEndpointField.tableId];
|
|
214
|
+
var foreignEndpointSchema = model.schemas[foreignEndpointTable.schemaId];
|
|
215
|
+
var foreignEndpointFieldNameString = _this3.buildReferenceFieldNamesString(foreignEndpoint.fieldIds, model);
|
|
216
|
+
var refEndpointId = ref.endpointIds[refEndpointIndex];
|
|
217
|
+
var refEndpoint = model.endpoints[refEndpointId];
|
|
218
|
+
var refEndpointField = model.fields[refEndpoint.fieldIds[0]];
|
|
219
|
+
var refEndpointTable = model.tables[refEndpointField.tableId];
|
|
220
|
+
var refEndpointSchema = model.schemas[refEndpointTable.schemaId];
|
|
221
|
+
var refEndpointFieldNameString = _this3.buildReferenceFieldNamesString(refEndpoint.fieldIds, model);
|
|
222
|
+
if (refOneIndex !== -1) {
|
|
223
|
+
var foreignTableName = _this3.buildTableNameWithSchema(model, foreignEndpointSchema, foreignEndpointTable);
|
|
224
|
+
var line = "ALTER TABLE ".concat(foreignTableName, " ADD");
|
|
225
|
+
if (ref.name) {
|
|
226
|
+
line += " CONSTRAINT ".concat((0, _utils.escapeObjectName)(ref.name, 'oracle'));
|
|
227
|
+
}
|
|
228
|
+
var refTableName = _this3.buildTableNameWithSchema(model, refEndpointSchema, refEndpointTable);
|
|
229
|
+
line += " FOREIGN KEY ".concat(foreignEndpointFieldNameString, " REFERENCES ").concat(refTableName, " ").concat(refEndpointFieldNameString);
|
|
230
|
+
if (ref.onDelete) {
|
|
231
|
+
line += " ON DELETE ".concat(ref.onDelete.toUpperCase());
|
|
232
|
+
}
|
|
233
|
+
line += ';\n';
|
|
234
|
+
result.refs.push(line);
|
|
235
|
+
return;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
// many to many relationship
|
|
239
|
+
var firstTableFieldsMap = (0, _utils.buildJunctionFields1)(refEndpoint.fieldIds, model);
|
|
240
|
+
var secondTableFieldsMap = (0, _utils.buildJunctionFields2)(foreignEndpoint.fieldIds, model, firstTableFieldsMap);
|
|
241
|
+
var newTableName = (0, _utils.buildUniqueTableName)(refEndpointSchema, refEndpointTable.name, foreignEndpointTable.name, usedTableNameMap);
|
|
242
|
+
var tableNameSet = usedTableNameMap.get(refEndpointSchema);
|
|
243
|
+
if (!tableNameSet) {
|
|
244
|
+
usedTableNameMap.set(refEndpointSchema, new Set([newTableName]));
|
|
245
|
+
} else {
|
|
246
|
+
tableNameSet.add(newTableName);
|
|
247
|
+
}
|
|
248
|
+
var escapedNewTableName = "".concat((0, _utils.shouldPrintSchema)(refEndpointSchema, model) ? "\"".concat(refEndpointSchema.name, "\".") : '', "\"").concat(newTableName, "\"");
|
|
249
|
+
result.tables.push(_this3.buildTableManyToMany(firstTableFieldsMap, secondTableFieldsMap, escapedNewTableName));
|
|
250
|
+
var firstTableName = _this3.buildTableNameWithSchema(model, refEndpointSchema, refEndpointTable);
|
|
251
|
+
result.refs.push(_this3.buildForeignKeyManyToMany(escapedNewTableName, firstTableFieldsMap, firstTableName, refEndpointFieldNameString));
|
|
252
|
+
var secondTableName = _this3.buildTableNameWithSchema(model, foreignEndpointSchema, foreignEndpointTable);
|
|
253
|
+
result.refs.push(_this3.buildForeignKeyManyToMany(escapedNewTableName, secondTableFieldsMap, secondTableName, foreignEndpointFieldNameString));
|
|
254
|
+
});
|
|
255
|
+
return result;
|
|
256
|
+
}
|
|
257
|
+
}, {
|
|
258
|
+
key: "exportReferenceGrants",
|
|
259
|
+
value: function exportReferenceGrants(model, refIds) {
|
|
260
|
+
var _this4 = this;
|
|
261
|
+
// only default schema -> ignore it
|
|
262
|
+
if (Object.keys(model.schemas).length <= 1) {
|
|
263
|
+
return [];
|
|
264
|
+
}
|
|
265
|
+
var tableNameList = [];
|
|
266
|
+
refIds.forEach(function (refId) {
|
|
267
|
+
var ref = model.refs[refId];
|
|
268
|
+
|
|
269
|
+
// find the one relation in one - many, many - one, one - one relationship
|
|
270
|
+
var refOneIndex = ref.endpointIds.findIndex(function (endpointId) {
|
|
271
|
+
return model.endpoints[endpointId].relation === '1';
|
|
272
|
+
});
|
|
273
|
+
var refEndpointIndex = refOneIndex === -1 ? 0 : refOneIndex;
|
|
274
|
+
var refEndpointId = ref.endpointIds[refEndpointIndex];
|
|
275
|
+
var refEndpoint = model.endpoints[refEndpointId];
|
|
276
|
+
var refEndpointField = model.fields[refEndpoint.fieldIds[0]];
|
|
277
|
+
var refEndpointTable = model.tables[refEndpointField.tableId];
|
|
278
|
+
var refEndpointSchema = model.schemas[refEndpointTable.schemaId];
|
|
279
|
+
|
|
280
|
+
// refEndpointIndex could be 0 or 1, so use 1 - refEndpointIndex will take the remained index
|
|
281
|
+
var foreignEndpointId = ref.endpointIds[1 - refEndpointIndex];
|
|
282
|
+
var foreignEndpoint = model.endpoints[foreignEndpointId];
|
|
283
|
+
var foreignEndpointField = model.fields[foreignEndpoint.fieldIds[0]];
|
|
284
|
+
var foreignEndpointTable = model.tables[foreignEndpointField.tableId];
|
|
285
|
+
var foreignEndpointSchema = model.schemas[foreignEndpointTable.schemaId];
|
|
286
|
+
|
|
287
|
+
// reference in the same schema
|
|
288
|
+
if (refEndpointSchema.name === foreignEndpointSchema.name) {
|
|
289
|
+
tableNameList.push('');
|
|
290
|
+
return;
|
|
291
|
+
}
|
|
292
|
+
var refTableName = _this4.buildTableNameWithSchema(model, refEndpointSchema, refEndpointTable);
|
|
293
|
+
// refTableName is always needed to be grant
|
|
294
|
+
tableNameList.push(refTableName);
|
|
295
|
+
|
|
296
|
+
// one - many, many - one, one - one relationship
|
|
297
|
+
if (refOneIndex !== -1) {
|
|
298
|
+
return;
|
|
299
|
+
}
|
|
300
|
+
var foreignTableName = _this4.buildTableNameWithSchema(model, foreignEndpointSchema, foreignEndpointTable);
|
|
301
|
+
tableNameList.push(foreignTableName);
|
|
302
|
+
});
|
|
303
|
+
var tableToGrantList = tableNameList
|
|
304
|
+
// remove duplicate
|
|
305
|
+
.filter(function (table, index) {
|
|
306
|
+
return table && tableNameList.indexOf(table) === index;
|
|
307
|
+
})
|
|
308
|
+
// map into grant statement
|
|
309
|
+
.map(function (table) {
|
|
310
|
+
return "GRANT REFERENCES ON ".concat(table, " TO PUBLIC;\n");
|
|
311
|
+
});
|
|
312
|
+
return tableToGrantList;
|
|
313
|
+
}
|
|
314
|
+
}, {
|
|
315
|
+
key: "exportIndexes",
|
|
316
|
+
value: function exportIndexes(indexIds, model) {
|
|
317
|
+
var _this5 = this;
|
|
318
|
+
// exclude composite pk index
|
|
319
|
+
var indexArr = indexIds.filter(function (indexId) {
|
|
320
|
+
return !model.indexes[indexId].pk;
|
|
321
|
+
}).map(function (indexId) {
|
|
322
|
+
var index = model.indexes[indexId];
|
|
323
|
+
var table = model.tables[index.tableId];
|
|
324
|
+
var schema = model.schemas[table.schemaId];
|
|
325
|
+
var line = 'CREATE';
|
|
326
|
+
// ignore dbml index type: b-tree, hash
|
|
327
|
+
if (index.unique) {
|
|
328
|
+
line += ' UNIQUE';
|
|
329
|
+
}
|
|
330
|
+
line += ' INDEX';
|
|
331
|
+
if (index.name) {
|
|
332
|
+
line += " ".concat((0, _utils.escapeObjectName)(index.name, 'oracle'));
|
|
333
|
+
}
|
|
334
|
+
var tableName = _this5.buildTableNameWithSchema(model, schema, table);
|
|
335
|
+
line += " ON ".concat(tableName);
|
|
336
|
+
var columnArr = [];
|
|
337
|
+
index.columnIds.forEach(function (columnId) {
|
|
338
|
+
var column = model.indexColumns[columnId];
|
|
339
|
+
var columnStr = '';
|
|
340
|
+
if (column.type === 'expression') {
|
|
341
|
+
columnStr = "".concat(column.value);
|
|
342
|
+
} else {
|
|
343
|
+
columnStr = "\"".concat(column.value, "\"");
|
|
344
|
+
}
|
|
345
|
+
columnArr.push(columnStr);
|
|
346
|
+
});
|
|
347
|
+
var columnString = columnArr.join(', ');
|
|
348
|
+
line += " (".concat(columnString, ");\n");
|
|
349
|
+
return line;
|
|
350
|
+
});
|
|
351
|
+
return indexArr;
|
|
352
|
+
}
|
|
353
|
+
}, {
|
|
354
|
+
key: "exportComments",
|
|
355
|
+
value: function exportComments(comments, model) {
|
|
356
|
+
var _this6 = this;
|
|
357
|
+
var commentArr = comments.map(function (comment) {
|
|
358
|
+
var line = 'COMMENT ON';
|
|
359
|
+
var table = model.tables[comment.tableId];
|
|
360
|
+
var schema = model.schemas[table.schemaId];
|
|
361
|
+
var tableName = _this6.buildTableNameWithSchema(model, schema, table);
|
|
362
|
+
switch (comment.type) {
|
|
363
|
+
case 'table':
|
|
364
|
+
{
|
|
365
|
+
line += " TABLE ".concat(tableName, " IS '").concat(table.note.replace(/'/g, "''"), "'");
|
|
366
|
+
break;
|
|
367
|
+
}
|
|
368
|
+
case 'column':
|
|
369
|
+
{
|
|
370
|
+
var field = model.fields[comment.fieldId];
|
|
371
|
+
line += " COLUMN ".concat(tableName, ".").concat((0, _utils.escapeObjectName)(field.name, 'oracle'), " IS '").concat(field.note.replace(/'/g, "''"), "'");
|
|
372
|
+
break;
|
|
373
|
+
}
|
|
374
|
+
default:
|
|
375
|
+
break;
|
|
376
|
+
}
|
|
377
|
+
line += ';\n';
|
|
378
|
+
return line;
|
|
379
|
+
});
|
|
380
|
+
return commentArr;
|
|
381
|
+
}
|
|
382
|
+
}, {
|
|
383
|
+
key: "export",
|
|
384
|
+
value: function _export(model) {
|
|
385
|
+
var _this7 = this;
|
|
386
|
+
var database = model.database['1'];
|
|
387
|
+
var schemaToTableNameSetMap = this.buildSchemaToTableNameSetMap(model);
|
|
388
|
+
var statements = database.schemaIds.reduce(function (prevStatements, schemaId) {
|
|
389
|
+
var schema = model.schemas[schemaId];
|
|
390
|
+
var tableIds = schema.tableIds,
|
|
391
|
+
refIds = schema.refIds;
|
|
392
|
+
if ((0, _utils.shouldPrintSchema)(schema, model)) {
|
|
393
|
+
prevStatements.schemas.push(_this7.exportSchema(schema.name));
|
|
394
|
+
}
|
|
395
|
+
if (!_lodash["default"].isEmpty(tableIds)) {
|
|
396
|
+
var _prevStatements$table;
|
|
397
|
+
(_prevStatements$table = prevStatements.tables).push.apply(_prevStatements$table, _toConsumableArray(_this7.exportTables(tableIds, model)));
|
|
398
|
+
}
|
|
399
|
+
var indexIds = _lodash["default"].flatten(tableIds.map(function (tableId) {
|
|
400
|
+
return model.tables[tableId].indexIds;
|
|
401
|
+
}));
|
|
402
|
+
if (!_lodash["default"].isEmpty(indexIds)) {
|
|
403
|
+
var _prevStatements$index;
|
|
404
|
+
(_prevStatements$index = prevStatements.indexes).push.apply(_prevStatements$index, _toConsumableArray(_this7.exportIndexes(indexIds, model)));
|
|
405
|
+
}
|
|
406
|
+
var commentNodes = _lodash["default"].flatten(tableIds.map(function (tableId) {
|
|
407
|
+
var _model$tables$tableId = model.tables[tableId],
|
|
408
|
+
fieldIds = _model$tables$tableId.fieldIds,
|
|
409
|
+
note = _model$tables$tableId.note;
|
|
410
|
+
var fieldObjects = fieldIds.filter(function (fieldId) {
|
|
411
|
+
return model.fields[fieldId].note;
|
|
412
|
+
}).map(function (fieldId) {
|
|
413
|
+
return {
|
|
414
|
+
type: 'column',
|
|
415
|
+
fieldId: fieldId,
|
|
416
|
+
tableId: tableId
|
|
417
|
+
};
|
|
418
|
+
});
|
|
419
|
+
return note ? [{
|
|
420
|
+
type: 'table',
|
|
421
|
+
tableId: tableId
|
|
422
|
+
}].concat(fieldObjects) : fieldObjects;
|
|
423
|
+
}));
|
|
424
|
+
if (!_lodash["default"].isEmpty(commentNodes)) {
|
|
425
|
+
var _prevStatements$comme;
|
|
426
|
+
(_prevStatements$comme = prevStatements.comments).push.apply(_prevStatements$comme, _toConsumableArray(_this7.exportComments(commentNodes, model)));
|
|
427
|
+
}
|
|
428
|
+
if (!_lodash["default"].isEmpty(refIds)) {
|
|
429
|
+
var _prevStatements$table2, _prevStatements$refs, _prevStatements$refer;
|
|
430
|
+
var _this7$exportReferenc = _this7.exportReferencesAndNewTablesIfExists(refIds, model, schemaToTableNameSetMap),
|
|
431
|
+
refs = _this7$exportReferenc.refs,
|
|
432
|
+
manyToManyTables = _this7$exportReferenc.tables;
|
|
433
|
+
(_prevStatements$table2 = prevStatements.tables).push.apply(_prevStatements$table2, _toConsumableArray(manyToManyTables));
|
|
434
|
+
(_prevStatements$refs = prevStatements.refs).push.apply(_prevStatements$refs, _toConsumableArray(refs));
|
|
435
|
+
(_prevStatements$refer = prevStatements.referenceGrants).push.apply(_prevStatements$refer, _toConsumableArray(_this7.exportReferenceGrants(model, refIds)));
|
|
436
|
+
}
|
|
437
|
+
return prevStatements;
|
|
438
|
+
}, {
|
|
439
|
+
schemas: [],
|
|
440
|
+
tables: [],
|
|
441
|
+
indexes: [],
|
|
442
|
+
comments: [],
|
|
443
|
+
referenceGrants: [],
|
|
444
|
+
refs: []
|
|
445
|
+
});
|
|
446
|
+
var res = _lodash["default"].concat(statements.schemas, statements.tables, statements.indexes, statements.comments, statements.referenceGrants, statements.refs).join('\n');
|
|
447
|
+
return res;
|
|
448
|
+
}
|
|
449
|
+
}]);
|
|
450
|
+
return OracleExporter;
|
|
451
|
+
}();
|
|
452
|
+
var _default = OracleExporter;
|
|
453
|
+
exports["default"] = _default;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# DBML Exporter
|
|
2
|
+
|
|
3
|
+
## DBML vs Oracle Mapping
|
|
4
|
+
|
|
5
|
+
Currently, our exporter supports Oracle version 19c syntax.
|
|
6
|
+
|
|
7
|
+
| DBML | Oracle | Notes |
|
|
8
|
+
| --- | --- | --- |
|
|
9
|
+
| **Schema** | Schema - User | |
|
|
10
|
+
| **Table** | Table | |
|
|
11
|
+
| `note` | Table comment | |
|
|
12
|
+
| **Column** | Column | |
|
|
13
|
+
| `note` | Column comment | |
|
|
14
|
+
| `primary key or pk` | PRIMARY KEY constraint | |
|
|
15
|
+
| `null or not null` | NULL or NOT NULL constraint | |
|
|
16
|
+
| `unique` | UNIQUE constraint | |
|
|
17
|
+
| `default` | DEFAULT value | |
|
|
18
|
+
| `increment` | Identity column | |
|
|
19
|
+
| **Index** | Index | |
|
|
20
|
+
| `type` | Index type | |
|
|
21
|
+
| `name` | Index name | |
|
|
22
|
+
| `unique` | Unique index | |
|
|
23
|
+
| Index with an expression | Function-based index | |
|
|
24
|
+
| Composite index | Index on multiple columns | |
|
|
25
|
+
| **Relationship** | Foreign key | |
|
|
26
|
+
| `delete` | ON DELETE clause | |
|
|
27
|
+
| `update` | ❌ | |
|
|
28
|
+
| Many-to-many relationship | ❌ | Use junction table |
|
|
29
|
+
| **Composite keys** | | |
|
|
30
|
+
| Composite primary key | Composite primary key | |
|
|
31
|
+
| Composite foreign key | Composite foreign key | |
|
|
32
|
+
| **Enum** | ❌ | Use CHECK constraint |
|
package/lib/export/utils.js
CHANGED
|
@@ -6,6 +6,8 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
6
6
|
exports.buildJunctionFields1 = buildJunctionFields1;
|
|
7
7
|
exports.buildJunctionFields2 = buildJunctionFields2;
|
|
8
8
|
exports.buildNewTableName = buildNewTableName;
|
|
9
|
+
exports.buildUniqueTableName = buildUniqueTableName;
|
|
10
|
+
exports.escapeObjectName = escapeObjectName;
|
|
9
11
|
exports.hasWhiteSpace = hasWhiteSpace;
|
|
10
12
|
exports.shouldPrintSchema = shouldPrintSchema;
|
|
11
13
|
var _config = require("../model_structure/config");
|
|
@@ -24,7 +26,7 @@ function buildJunctionFields1(fieldIds, model) {
|
|
|
24
26
|
}
|
|
25
27
|
function buildJunctionFields2(fieldIds, model, firstTableFieldsMap) {
|
|
26
28
|
var fieldsMap = new Map();
|
|
27
|
-
fieldIds.
|
|
29
|
+
fieldIds.forEach(function (fieldId) {
|
|
28
30
|
var fieldName = "".concat(model.tables[model.fields[fieldId].tableId].name, "_").concat(model.fields[fieldId].name);
|
|
29
31
|
var count = 1;
|
|
30
32
|
while (firstTableFieldsMap.has(fieldName)) {
|
|
@@ -44,4 +46,45 @@ function buildNewTableName(firstTable, secondTable, usedTableNames) {
|
|
|
44
46
|
}
|
|
45
47
|
usedTableNames.add(newTableName);
|
|
46
48
|
return newTableName;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
*
|
|
53
|
+
* @param {string} schemaName
|
|
54
|
+
* @param {string} firstTableName
|
|
55
|
+
* @param {string} secondTableName
|
|
56
|
+
* @param {Map<string, Set>} schemaToTableNameSetMap
|
|
57
|
+
* @returns string
|
|
58
|
+
* @description This function is a clone version of the buildNewTableName, but without side effect - update the original usedTableNames
|
|
59
|
+
*/
|
|
60
|
+
function buildUniqueTableName(schemaName, firstTableName, secondTableName, schemaToTableNameSetMap) {
|
|
61
|
+
var newTableName = "".concat(firstTableName, "_").concat(secondTableName);
|
|
62
|
+
var count = 1;
|
|
63
|
+
var tableNameSet = schemaToTableNameSetMap.get(schemaName);
|
|
64
|
+
if (!tableNameSet) {
|
|
65
|
+
return newTableName;
|
|
66
|
+
}
|
|
67
|
+
while (tableNameSet.has(newTableName)) {
|
|
68
|
+
newTableName = "".concat(firstTableName, "_").concat(secondTableName, "(").concat(count, ")");
|
|
69
|
+
count += 1;
|
|
70
|
+
}
|
|
71
|
+
return newTableName;
|
|
72
|
+
}
|
|
73
|
+
function escapeObjectName(name, database) {
|
|
74
|
+
if (!name) {
|
|
75
|
+
return '';
|
|
76
|
+
}
|
|
77
|
+
var escapeSignature = '';
|
|
78
|
+
switch (database) {
|
|
79
|
+
case 'mysql':
|
|
80
|
+
escapeSignature = '`';
|
|
81
|
+
break;
|
|
82
|
+
case 'postgres':
|
|
83
|
+
case 'oracle':
|
|
84
|
+
escapeSignature = '"';
|
|
85
|
+
break;
|
|
86
|
+
default:
|
|
87
|
+
break;
|
|
88
|
+
}
|
|
89
|
+
return "".concat(escapeSignature).concat(name).concat(escapeSignature);
|
|
47
90
|
}
|
package/lib/index.js
CHANGED
|
@@ -3,6 +3,12 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
+
Object.defineProperty(exports, "CompilerError", {
|
|
7
|
+
enumerable: true,
|
|
8
|
+
get: function get() {
|
|
9
|
+
return _error.CompilerError;
|
|
10
|
+
}
|
|
11
|
+
});
|
|
6
12
|
Object.defineProperty(exports, "ModelExporter", {
|
|
7
13
|
enumerable: true,
|
|
8
14
|
get: function get() {
|
|
@@ -29,6 +35,7 @@ Object.defineProperty(exports, "importer", {
|
|
|
29
35
|
});
|
|
30
36
|
var _ModelExporter = _interopRequireDefault(require("./export/ModelExporter"));
|
|
31
37
|
var _Parser = _interopRequireDefault(require("./parse/Parser"));
|
|
38
|
+
var _error = require("../lib/parse/error");
|
|
32
39
|
var _import = _interopRequireDefault(require("./import"));
|
|
33
40
|
var _export = _interopRequireDefault(require("./export"));
|
|
34
41
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
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); }
|
|
4
|
+
Object.defineProperty(exports, "__esModule", {
|
|
5
|
+
value: true
|
|
6
|
+
});
|
|
7
|
+
exports["default"] = void 0;
|
|
8
|
+
var _element = _interopRequireDefault(require("./element"));
|
|
9
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
|
10
|
+
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
|
11
|
+
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
|
12
|
+
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
13
|
+
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
14
|
+
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); } }
|
|
15
|
+
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
|
|
16
|
+
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
|
|
17
|
+
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); }
|
|
18
|
+
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }
|
|
19
|
+
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
|
|
20
|
+
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
|
|
21
|
+
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }
|
|
22
|
+
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
|
|
23
|
+
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
|
|
24
|
+
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
|
|
25
|
+
var Note = /*#__PURE__*/function (_Element) {
|
|
26
|
+
_inherits(Note, _Element);
|
|
27
|
+
var _super = _createSuper(Note);
|
|
28
|
+
function Note() {
|
|
29
|
+
var _this;
|
|
30
|
+
var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
|
|
31
|
+
name = _ref.name,
|
|
32
|
+
content = _ref.content,
|
|
33
|
+
headerColor = _ref.headerColor,
|
|
34
|
+
token = _ref.token,
|
|
35
|
+
_ref$database = _ref.database,
|
|
36
|
+
database = _ref$database === void 0 ? {} : _ref$database;
|
|
37
|
+
_classCallCheck(this, Note);
|
|
38
|
+
_this = _super.call(this, token);
|
|
39
|
+
_this.name = name;
|
|
40
|
+
_this.content = content;
|
|
41
|
+
_this.headerColor = headerColor;
|
|
42
|
+
_this.database = database;
|
|
43
|
+
_this.dbState = _this.database.dbState;
|
|
44
|
+
_this.generateId();
|
|
45
|
+
return _this;
|
|
46
|
+
}
|
|
47
|
+
_createClass(Note, [{
|
|
48
|
+
key: "generateId",
|
|
49
|
+
value: function generateId() {
|
|
50
|
+
this.id = this.dbState.generateId('noteId');
|
|
51
|
+
}
|
|
52
|
+
}, {
|
|
53
|
+
key: "export",
|
|
54
|
+
value: function _export() {
|
|
55
|
+
return {
|
|
56
|
+
name: this.name,
|
|
57
|
+
content: this.content,
|
|
58
|
+
headerColor: this.headerColor
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
}, {
|
|
62
|
+
key: "normalize",
|
|
63
|
+
value: function normalize(model) {
|
|
64
|
+
model.notes = _objectSpread(_objectSpread({}, model.notes), {}, _defineProperty({}, this.id, _objectSpread({
|
|
65
|
+
id: this.id
|
|
66
|
+
}, this["export"]())));
|
|
67
|
+
}
|
|
68
|
+
}]);
|
|
69
|
+
return Note;
|
|
70
|
+
}(_element["default"]);
|
|
71
|
+
var _default = Note;
|
|
72
|
+
exports["default"] = _default;
|
package/lib/parse/Parser.js
CHANGED
|
@@ -12,6 +12,7 @@ var _dbmlParser = _interopRequireDefault(require("./dbmlParser"));
|
|
|
12
12
|
var _schemarbParser = _interopRequireDefault(require("./schemarbParser"));
|
|
13
13
|
var _mssqlParser = _interopRequireDefault(require("./mssqlParser"));
|
|
14
14
|
var _ASTGeneration = require("./ANTLR/ASTGeneration");
|
|
15
|
+
var _error = require("./error");
|
|
15
16
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
|
16
17
|
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); }
|
|
17
18
|
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
@@ -27,29 +28,28 @@ var Parser = /*#__PURE__*/function () {
|
|
|
27
28
|
_createClass(Parser, [{
|
|
28
29
|
key: "parse",
|
|
29
30
|
value: function parse(str, format) {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
throw errors.map(function (error) {
|
|
31
|
+
try {
|
|
32
|
+
var rawDatabase = {};
|
|
33
|
+
switch (format) {
|
|
34
|
+
case 'mysql':
|
|
35
|
+
rawDatabase = Parser.parseMySQLToJSONv2(str);
|
|
36
|
+
break;
|
|
37
|
+
case 'mysqlLegacy':
|
|
38
|
+
rawDatabase = Parser.parseMySQLToJSON(str);
|
|
39
|
+
break;
|
|
40
|
+
case 'postgres':
|
|
41
|
+
rawDatabase = Parser.parsePostgresToJSONv2(str);
|
|
42
|
+
break;
|
|
43
|
+
case 'postgresLegacy':
|
|
44
|
+
rawDatabase = Parser.parsePostgresToJSON(str);
|
|
45
|
+
break;
|
|
46
|
+
case 'dbml':
|
|
47
|
+
rawDatabase = Parser.parseDBMLToJSON(str);
|
|
48
|
+
break;
|
|
49
|
+
case 'dbmlv2':
|
|
50
|
+
{
|
|
51
|
+
this.DBMLCompiler.setSource(str);
|
|
52
|
+
var diags = this.DBMLCompiler.parse.errors().map(function (error) {
|
|
53
53
|
return {
|
|
54
54
|
message: error.diagnostic,
|
|
55
55
|
location: {
|
|
@@ -65,28 +65,31 @@ var Parser = /*#__PURE__*/function () {
|
|
|
65
65
|
code: error.code
|
|
66
66
|
};
|
|
67
67
|
});
|
|
68
|
+
if (diags.length > 0) throw _error.CompilerError.create(diags);
|
|
68
69
|
}
|
|
69
70
|
rawDatabase = this.DBMLCompiler.parse.rawDb();
|
|
70
71
|
break;
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
72
|
+
case 'schemarb':
|
|
73
|
+
rawDatabase = Parser.parseSchemaRbToJSON(str);
|
|
74
|
+
break;
|
|
75
|
+
case 'mssql':
|
|
76
|
+
rawDatabase = Parser.parseMSSQLToJSON(str);
|
|
77
|
+
break;
|
|
78
|
+
case 'json':
|
|
79
|
+
if (_typeof(str) === 'object') {
|
|
80
|
+
rawDatabase = str;
|
|
81
|
+
} else {
|
|
82
|
+
rawDatabase = JSON.parse(str);
|
|
83
|
+
}
|
|
84
|
+
break;
|
|
85
|
+
default:
|
|
86
|
+
break;
|
|
87
|
+
}
|
|
88
|
+
var schema = Parser.parseJSONToDatabase(rawDatabase);
|
|
89
|
+
return schema;
|
|
90
|
+
} catch (diags) {
|
|
91
|
+
throw _error.CompilerError.create(diags);
|
|
87
92
|
}
|
|
88
|
-
var schema = Parser.parseJSONToDatabase(rawDatabase);
|
|
89
|
-
return schema;
|
|
90
93
|
}
|
|
91
94
|
}], [{
|
|
92
95
|
key: "parseJSONToDatabase",
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.CompilerError = void 0;
|
|
7
|
+
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); }
|
|
8
|
+
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
9
|
+
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); } }
|
|
10
|
+
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
|
|
11
|
+
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
|
|
12
|
+
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); }
|
|
13
|
+
var CompilerError = /*#__PURE__*/function () {
|
|
14
|
+
/**
|
|
15
|
+
* @param {import("../../types").CompilerDiagnostic[]} diags
|
|
16
|
+
*/
|
|
17
|
+
function CompilerError(diags) {
|
|
18
|
+
_classCallCheck(this, CompilerError);
|
|
19
|
+
this.diags = diags;
|
|
20
|
+
}
|
|
21
|
+
_createClass(CompilerError, [{
|
|
22
|
+
key: "map",
|
|
23
|
+
value: function map(callback) {
|
|
24
|
+
return CompilerError.create(this.diags.map(callback));
|
|
25
|
+
}
|
|
26
|
+
}], [{
|
|
27
|
+
key: "create",
|
|
28
|
+
value: function create(nestedDiags) {
|
|
29
|
+
// eslint-disable-next-line no-use-before-define
|
|
30
|
+
return new CompilerError(flattenDiag(nestedDiags));
|
|
31
|
+
}
|
|
32
|
+
}]);
|
|
33
|
+
return CompilerError;
|
|
34
|
+
}();
|
|
35
|
+
exports.CompilerError = CompilerError;
|
|
36
|
+
function flattenDiag(diag) {
|
|
37
|
+
if (Array.isArray(diag)) return diag.flatMap(flattenDiag);
|
|
38
|
+
if (diag instanceof CompilerError) return diag.diags;
|
|
39
|
+
return [diag];
|
|
40
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dbml/core",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.4.1-alpha.0",
|
|
4
4
|
"description": "> TODO: description",
|
|
5
5
|
"author": "Holistics <dev@holistics.io>",
|
|
6
6
|
"license": "Apache-2.0",
|
|
@@ -32,7 +32,7 @@
|
|
|
32
32
|
"prepublish": "npm run build"
|
|
33
33
|
},
|
|
34
34
|
"dependencies": {
|
|
35
|
-
"@dbml/parse": "^3.
|
|
35
|
+
"@dbml/parse": "^3.4.1-alpha.0",
|
|
36
36
|
"antlr4": "^4.13.1",
|
|
37
37
|
"lodash": "^4.17.15",
|
|
38
38
|
"parsimmon": "^1.13.0",
|
|
@@ -59,5 +59,5 @@
|
|
|
59
59
|
"\\.(?!json$)[^.]*$": "@glen/jest-raw-loader"
|
|
60
60
|
}
|
|
61
61
|
},
|
|
62
|
-
"gitHead": "
|
|
62
|
+
"gitHead": "452294fcc48326294bf66a76daa418668d6ba8a9"
|
|
63
63
|
}
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import Database, { NormalizedDatabase } from '../model_structure/database';
|
|
2
|
+
|
|
3
|
+
export type ExportFormatOption = 'dbml' | 'mysql' | 'postgres' | 'json' | 'mssql' | 'oracle';
|
|
2
4
|
declare class ModelExporter {
|
|
3
|
-
static export(model: Database | NormalizedDatabase, format:
|
|
5
|
+
static export(model: Database | NormalizedDatabase, format: ExportFormatOption, isNormalized?: boolean): string;
|
|
4
6
|
}
|
|
5
7
|
export default ModelExporter;
|
package/types/export/index.d.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
|
|
1
|
+
import { ExportFormatOption } from './ModelExporter';
|
|
2
|
+
|
|
3
|
+
declare function _export(str: string, format: ExportFormatOption): string;
|
|
2
4
|
declare const _default: {
|
|
3
5
|
export: typeof _export;
|
|
4
6
|
};
|
package/types/index.d.ts
CHANGED
|
@@ -3,3 +3,4 @@ import Parser from './parse/Parser';
|
|
|
3
3
|
import importer from './import';
|
|
4
4
|
import exporter from './export';
|
|
5
5
|
export { importer, exporter, ModelExporter, Parser, };
|
|
6
|
+
export { CompilerDiagnostic, CompilerError as CompilerDiagnostics, EditorPosition, ErrorCode, WarningLevel, } from './parse/error'
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
export declare type WarningLevel =
|
|
2
|
+
| 'error'
|
|
3
|
+
| 'warning'
|
|
4
|
+
| 'info';
|
|
5
|
+
|
|
6
|
+
export declare type ErrorCode = number;
|
|
7
|
+
|
|
8
|
+
export declare interface EditorPosition {
|
|
9
|
+
line: number;
|
|
10
|
+
column: number;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface CompilerDiagnostic {
|
|
14
|
+
readonly message: Readonly<string>;
|
|
15
|
+
readonly filepath?: Readonly<string>;
|
|
16
|
+
readonly location: {
|
|
17
|
+
start: Readonly<EditorPosition>,
|
|
18
|
+
// in monaco, if an end position is not specified
|
|
19
|
+
// it consumes the word containing the start position
|
|
20
|
+
end?: Readonly<EditorPosition>,
|
|
21
|
+
};
|
|
22
|
+
readonly type?: Readonly<WarningLevel>;
|
|
23
|
+
readonly code?: Readonly<ErrorCode>; // the error code
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface CompilerError {
|
|
27
|
+
readonly diags: CompilerDiagnostic[];
|
|
28
|
+
}
|