@dbml/core 3.14.1 → 4.0.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/DbmlExporter.js +38 -1
- package/lib/export/MysqlExporter.js +36 -1
- package/lib/export/OracleExporter.js +36 -1
- package/lib/export/PostgresExporter.js +36 -1
- package/lib/export/SqlServerExporter.js +36 -1
- package/lib/index.js +1 -1
- package/lib/model_structure/constraint.js +86 -0
- package/lib/model_structure/database.js +1 -0
- package/lib/model_structure/dbState.js +1 -0
- package/lib/model_structure/field.js +23 -1
- package/lib/model_structure/table.js +51 -17
- package/lib/model_structure/tablePartial.js +3 -0
- package/lib/parse/ANTLR/ASTGeneration/AST.js +18 -6
- package/lib/parse/ANTLR/ASTGeneration/constants.js +2 -1
- package/lib/parse/ANTLR/ASTGeneration/mssql/MssqlASTGen.js +53 -101
- package/lib/parse/ANTLR/ASTGeneration/mysql/MySQLASTGen.js +73 -19
- package/lib/parse/ANTLR/ASTGeneration/postgres/PostgresASTGen.js +50 -16
- package/lib/parse/ANTLR/parsers/postgresql/PostgreSQLParser.g4 +1 -1
- package/lib/parse/ANTLR/parsers/postgresql/PostgreSQLParser.interp +1 -1
- package/lib/parse/ANTLR/parsers/postgresql/PostgreSQLParser.js +2 -2
- package/lib/parse/ANTLR/parsers/postgresql/PostgreSQLParser.tokens +1314 -1314
- package/lib/parse/ANTLR/parsers/postgresql/PostgreSQLParserVisitor.js +1 -1
- package/lib/parse/schemarb/parser.pegjs +41 -0
- package/lib/parse/schemarbParser.js +568 -226
- package/package.json +3 -3
- package/types/model_structure/constraint.d.ts +52 -0
- package/types/model_structure/database.d.ts +2 -0
- package/types/model_structure/field.d.ts +7 -1
- package/types/model_structure/table.d.ts +8 -1
- package/types/model_structure/tablePartial.d.ts +4 -1
|
@@ -8,6 +8,12 @@ var _lodash = require("lodash");
|
|
|
8
8
|
var _utils = require("./utils");
|
|
9
9
|
var _config = require("../model_structure/config");
|
|
10
10
|
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
|
|
11
|
+
function _toConsumableArray(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _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(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
|
|
14
|
+
function _iterableToArray(r) { if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r); }
|
|
15
|
+
function _arrayWithoutHoles(r) { if (Array.isArray(r)) return _arrayLikeToArray(r); }
|
|
16
|
+
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
|
|
11
17
|
function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
|
|
12
18
|
function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
|
|
13
19
|
function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
|
|
@@ -83,6 +89,11 @@ var DbmlExporter = /*#__PURE__*/function () {
|
|
|
83
89
|
if (field.increment) {
|
|
84
90
|
constraints.push('increment');
|
|
85
91
|
}
|
|
92
|
+
if (field.constraintIds) {
|
|
93
|
+
constraints.push.apply(constraints, _toConsumableArray(field.constraintIds.map(function (id) {
|
|
94
|
+
return "constraint: `".concat(model.constraints[id].expression, "`");
|
|
95
|
+
})));
|
|
96
|
+
}
|
|
86
97
|
if (field.dbdefault) {
|
|
87
98
|
var value = 'default: ';
|
|
88
99
|
switch (field.dbdefault.type) {
|
|
@@ -155,15 +166,34 @@ var DbmlExporter = /*#__PURE__*/function () {
|
|
|
155
166
|
});
|
|
156
167
|
return lines;
|
|
157
168
|
}
|
|
169
|
+
}, {
|
|
170
|
+
key: "getConstraintLines",
|
|
171
|
+
value: function getConstraintLines(tableId, model) {
|
|
172
|
+
var table = model.tables[tableId];
|
|
173
|
+
var lines = table.constraintIds.map(function (constraintId) {
|
|
174
|
+
var line = '';
|
|
175
|
+
var _model$constraints$co = model.constraints[constraintId],
|
|
176
|
+
expression = _model$constraints$co.expression,
|
|
177
|
+
name = _model$constraints$co.name;
|
|
178
|
+
line += "`".concat(expression, "`");
|
|
179
|
+
if (name) {
|
|
180
|
+
line += " [name: '".concat(name, "']");
|
|
181
|
+
}
|
|
182
|
+
return line;
|
|
183
|
+
});
|
|
184
|
+
return lines;
|
|
185
|
+
}
|
|
158
186
|
}, {
|
|
159
187
|
key: "getTableContentArr",
|
|
160
188
|
value: function getTableContentArr(tableIds, model) {
|
|
161
189
|
var tableContentArr = tableIds.map(function (tableId) {
|
|
162
190
|
var fieldContents = DbmlExporter.getFieldLines(tableId, model);
|
|
191
|
+
var constraintContents = DbmlExporter.getConstraintLines(tableId, model);
|
|
163
192
|
var indexContents = DbmlExporter.getIndexLines(tableId, model);
|
|
164
193
|
return {
|
|
165
194
|
tableId: tableId,
|
|
166
195
|
fieldContents: fieldContents,
|
|
196
|
+
constraintContents: constraintContents,
|
|
167
197
|
indexContents: indexContents
|
|
168
198
|
};
|
|
169
199
|
});
|
|
@@ -196,6 +226,13 @@ var DbmlExporter = /*#__PURE__*/function () {
|
|
|
196
226
|
var fieldStr = tableContent.fieldContents.map(function (field) {
|
|
197
227
|
return " ".concat(field, "\n");
|
|
198
228
|
}).join('');
|
|
229
|
+
var constraintStr = '';
|
|
230
|
+
if (!(0, _lodash.isEmpty)(tableContent.constraintContents)) {
|
|
231
|
+
var constraintBody = tableContent.constraintContents.map(function (constraintLine) {
|
|
232
|
+
return " ".concat(constraintLine, "\n");
|
|
233
|
+
}).join('');
|
|
234
|
+
constraintStr = "\n Constraints {\n".concat(constraintBody, " }\n");
|
|
235
|
+
}
|
|
199
236
|
var indexStr = '';
|
|
200
237
|
if (!(0, _lodash.isEmpty)(tableContent.indexContents)) {
|
|
201
238
|
var indexBody = tableContent.indexContents.map(function (indexLine) {
|
|
@@ -204,7 +241,7 @@ var DbmlExporter = /*#__PURE__*/function () {
|
|
|
204
241
|
indexStr = "\n Indexes {\n".concat(indexBody, " }\n");
|
|
205
242
|
}
|
|
206
243
|
var noteStr = table.note ? " Note: ".concat(DbmlExporter.escapeNote(table.note), "\n") : '';
|
|
207
|
-
return "Table ".concat(tableName).concat(tableSettingStr, " {\n").concat(fieldStr).concat(indexStr).concat(noteStr, "}\n");
|
|
244
|
+
return "Table ".concat(tableName).concat(tableSettingStr, " {\n").concat(fieldStr).concat(constraintStr).concat(indexStr).concat(noteStr, "}\n");
|
|
208
245
|
});
|
|
209
246
|
return tableStrs.length ? tableStrs.join('\n') : '';
|
|
210
247
|
}
|
|
@@ -52,6 +52,21 @@ var MySQLExporter = /*#__PURE__*/function () {
|
|
|
52
52
|
if (field.increment) {
|
|
53
53
|
line += ' AUTO_INCREMENT';
|
|
54
54
|
}
|
|
55
|
+
if (field.constraintIds && field.constraintIds.length > 0) {
|
|
56
|
+
if (field.constraintIds.length === 1) {
|
|
57
|
+
var constraint = model.constraints[field.constraintIds[0]];
|
|
58
|
+
if (constraint.name) {
|
|
59
|
+
line += " CONSTRAINT `".concat(constraint.name, "`");
|
|
60
|
+
}
|
|
61
|
+
line += " CHECK (".concat(constraint.expression, ")");
|
|
62
|
+
} else {
|
|
63
|
+
var constraintExpressions = field.constraintIds.map(function (constraintId) {
|
|
64
|
+
var constraint = model.constraints[constraintId];
|
|
65
|
+
return "(".concat(constraint.expression, ")");
|
|
66
|
+
});
|
|
67
|
+
line += " CHECK (".concat(constraintExpressions.join(' AND '), ")");
|
|
68
|
+
}
|
|
69
|
+
}
|
|
55
70
|
if (field.dbdefault) {
|
|
56
71
|
if (field.dbdefault.type === 'expression') {
|
|
57
72
|
line += " DEFAULT (".concat(field.dbdefault.value, ")");
|
|
@@ -94,15 +109,35 @@ var MySQLExporter = /*#__PURE__*/function () {
|
|
|
94
109
|
});
|
|
95
110
|
return lines;
|
|
96
111
|
}
|
|
112
|
+
}, {
|
|
113
|
+
key: "getConstraintLines",
|
|
114
|
+
value: function getConstraintLines(tableId, model) {
|
|
115
|
+
var table = model.tables[tableId];
|
|
116
|
+
if (!table.constraintIds || table.constraintIds.length === 0) {
|
|
117
|
+
return [];
|
|
118
|
+
}
|
|
119
|
+
var lines = table.constraintIds.map(function (constraintId) {
|
|
120
|
+
var constraint = model.constraints[constraintId];
|
|
121
|
+
var line = '';
|
|
122
|
+
if (constraint.name) {
|
|
123
|
+
line = "CONSTRAINT `".concat(constraint.name, "` ");
|
|
124
|
+
}
|
|
125
|
+
line += "CHECK (".concat(constraint.expression, ")");
|
|
126
|
+
return line;
|
|
127
|
+
});
|
|
128
|
+
return lines;
|
|
129
|
+
}
|
|
97
130
|
}, {
|
|
98
131
|
key: "getTableContentArr",
|
|
99
132
|
value: function getTableContentArr(tableIds, model) {
|
|
100
133
|
var tableContentArr = tableIds.map(function (tableId) {
|
|
101
134
|
var fieldContents = MySQLExporter.getFieldLines(tableId, model);
|
|
135
|
+
var constraintContents = MySQLExporter.getConstraintLines(tableId, model);
|
|
102
136
|
var compositePKs = MySQLExporter.getCompositePKs(tableId, model);
|
|
103
137
|
return {
|
|
104
138
|
tableId: tableId,
|
|
105
139
|
fieldContents: fieldContents,
|
|
140
|
+
constraintContents: constraintContents,
|
|
106
141
|
compositePKs: compositePKs
|
|
107
142
|
};
|
|
108
143
|
});
|
|
@@ -113,7 +148,7 @@ var MySQLExporter = /*#__PURE__*/function () {
|
|
|
113
148
|
value: function exportTables(tableIds, model) {
|
|
114
149
|
var tableContentArr = MySQLExporter.getTableContentArr(tableIds, model);
|
|
115
150
|
var tableStrs = tableContentArr.map(function (tableContent) {
|
|
116
|
-
var content = [].concat(_toConsumableArray(tableContent.fieldContents), _toConsumableArray(tableContent.compositePKs));
|
|
151
|
+
var content = [].concat(_toConsumableArray(tableContent.fieldContents), _toConsumableArray(tableContent.constraintContents), _toConsumableArray(tableContent.compositePKs));
|
|
117
152
|
var table = model.tables[tableContent.tableId];
|
|
118
153
|
var schema = model.schemas[table.schemaId];
|
|
119
154
|
var tableStr = "CREATE TABLE ".concat((0, _utils.shouldPrintSchema)(schema, model) ? "`".concat(schema.name, "`.") : '', "`").concat(table.name, "` (\n").concat(content.map(function (line) {
|
|
@@ -100,6 +100,21 @@ var OracleExporter = /*#__PURE__*/function () {
|
|
|
100
100
|
if (cloneField.not_null) {
|
|
101
101
|
line += ' NOT NULL';
|
|
102
102
|
}
|
|
103
|
+
if (cloneField.constraintIds && cloneField.constraintIds.length > 0) {
|
|
104
|
+
if (cloneField.constraintIds.length === 1) {
|
|
105
|
+
var constraint = model.constraints[cloneField.constraintIds[0]];
|
|
106
|
+
if (constraint.name) {
|
|
107
|
+
line += " CONSTRAINT \"".concat(constraint.name, "\"");
|
|
108
|
+
}
|
|
109
|
+
line += " CHECK (".concat(constraint.expression, ")");
|
|
110
|
+
} else {
|
|
111
|
+
var constraintExpressions = cloneField.constraintIds.map(function (constraintId) {
|
|
112
|
+
var constraint = model.constraints[constraintId];
|
|
113
|
+
return "(".concat(constraint.expression, ")");
|
|
114
|
+
});
|
|
115
|
+
line += " CHECK (".concat(constraintExpressions.join(' AND '), ")");
|
|
116
|
+
}
|
|
117
|
+
}
|
|
103
118
|
return line;
|
|
104
119
|
});
|
|
105
120
|
return lines;
|
|
@@ -130,16 +145,36 @@ var OracleExporter = /*#__PURE__*/function () {
|
|
|
130
145
|
});
|
|
131
146
|
return lines;
|
|
132
147
|
}
|
|
148
|
+
}, {
|
|
149
|
+
key: "getConstraintLines",
|
|
150
|
+
value: function getConstraintLines(tableId, model) {
|
|
151
|
+
var table = model.tables[tableId];
|
|
152
|
+
if (!table.constraintIds || table.constraintIds.length === 0) {
|
|
153
|
+
return [];
|
|
154
|
+
}
|
|
155
|
+
var lines = table.constraintIds.map(function (constraintId) {
|
|
156
|
+
var constraint = model.constraints[constraintId];
|
|
157
|
+
var line = '';
|
|
158
|
+
if (constraint.name) {
|
|
159
|
+
line = "CONSTRAINT \"".concat(constraint.name, "\" ");
|
|
160
|
+
}
|
|
161
|
+
line += "CHECK (".concat(constraint.expression, ")");
|
|
162
|
+
return line;
|
|
163
|
+
});
|
|
164
|
+
return lines;
|
|
165
|
+
}
|
|
133
166
|
}, {
|
|
134
167
|
key: "getTableContents",
|
|
135
168
|
value: function getTableContents(tableIds, model) {
|
|
136
169
|
var _this = this;
|
|
137
170
|
var tableContentArr = tableIds.map(function (tableId) {
|
|
138
171
|
var fieldContents = _this.getFieldLines(tableId, model);
|
|
172
|
+
var constraintContents = _this.getConstraintLines(tableId, model);
|
|
139
173
|
var compositePKs = _this.getCompositePKs(tableId, model);
|
|
140
174
|
return {
|
|
141
175
|
tableId: tableId,
|
|
142
176
|
fieldContents: fieldContents,
|
|
177
|
+
constraintContents: constraintContents,
|
|
143
178
|
compositePKs: compositePKs
|
|
144
179
|
};
|
|
145
180
|
});
|
|
@@ -151,7 +186,7 @@ var OracleExporter = /*#__PURE__*/function () {
|
|
|
151
186
|
var _this2 = this;
|
|
152
187
|
var tableContentList = this.getTableContents(tableIds, model);
|
|
153
188
|
var tableStrs = tableContentList.map(function (tableContent) {
|
|
154
|
-
var content = [].concat(_toConsumableArray(tableContent.fieldContents), _toConsumableArray(tableContent.compositePKs));
|
|
189
|
+
var content = [].concat(_toConsumableArray(tableContent.fieldContents), _toConsumableArray(tableContent.constraintContents), _toConsumableArray(tableContent.compositePKs));
|
|
155
190
|
var table = model.tables[tableContent.tableId];
|
|
156
191
|
var schema = model.schemas[table.schemaId];
|
|
157
192
|
var tableName = _this2.buildTableNameWithSchema(model, schema, table);
|
|
@@ -122,6 +122,21 @@ var PostgresExporter = /*#__PURE__*/function () {
|
|
|
122
122
|
if (field.not_null) {
|
|
123
123
|
line += ' NOT NULL';
|
|
124
124
|
}
|
|
125
|
+
if (field.constraintIds && field.constraintIds.length > 0) {
|
|
126
|
+
if (field.constraintIds.length === 1) {
|
|
127
|
+
var constraint = model.constraints[field.constraintIds[0]];
|
|
128
|
+
if (constraint.name) {
|
|
129
|
+
line += " CONSTRAINT \"".concat(constraint.name, "\"");
|
|
130
|
+
}
|
|
131
|
+
line += " CHECK (".concat(constraint.expression, ")");
|
|
132
|
+
} else {
|
|
133
|
+
var constraintExpressions = field.constraintIds.map(function (constraintId) {
|
|
134
|
+
var constraint = model.constraints[constraintId];
|
|
135
|
+
return "(".concat(constraint.expression, ")");
|
|
136
|
+
});
|
|
137
|
+
line += " CHECK (".concat(constraintExpressions.join(' AND '), ")");
|
|
138
|
+
}
|
|
139
|
+
}
|
|
125
140
|
if (field.dbdefault) {
|
|
126
141
|
if (field.dbdefault.type === 'expression') {
|
|
127
142
|
line += " DEFAULT (".concat(field.dbdefault.value, ")");
|
|
@@ -161,15 +176,35 @@ var PostgresExporter = /*#__PURE__*/function () {
|
|
|
161
176
|
});
|
|
162
177
|
return lines;
|
|
163
178
|
}
|
|
179
|
+
}, {
|
|
180
|
+
key: "getConstraintLines",
|
|
181
|
+
value: function getConstraintLines(tableId, model) {
|
|
182
|
+
var table = model.tables[tableId];
|
|
183
|
+
if (!table.constraintIds || table.constraintIds.length === 0) {
|
|
184
|
+
return [];
|
|
185
|
+
}
|
|
186
|
+
var lines = table.constraintIds.map(function (constraintId) {
|
|
187
|
+
var constraint = model.constraints[constraintId];
|
|
188
|
+
var line = '';
|
|
189
|
+
if (constraint.name) {
|
|
190
|
+
line = "CONSTRAINT \"".concat(constraint.name, "\" ");
|
|
191
|
+
}
|
|
192
|
+
line += "CHECK (".concat(constraint.expression, ")");
|
|
193
|
+
return line;
|
|
194
|
+
});
|
|
195
|
+
return lines;
|
|
196
|
+
}
|
|
164
197
|
}, {
|
|
165
198
|
key: "getTableContentArr",
|
|
166
199
|
value: function getTableContentArr(tableIds, model, enumSet) {
|
|
167
200
|
var tableContentArr = tableIds.map(function (tableId) {
|
|
168
201
|
var fieldContents = PostgresExporter.getFieldLines(tableId, model, enumSet);
|
|
202
|
+
var constraintContents = PostgresExporter.getConstraintLines(tableId, model);
|
|
169
203
|
var compositePKs = PostgresExporter.getCompositePKs(tableId, model);
|
|
170
204
|
return {
|
|
171
205
|
tableId: tableId,
|
|
172
206
|
fieldContents: fieldContents,
|
|
207
|
+
constraintContents: constraintContents,
|
|
173
208
|
compositePKs: compositePKs
|
|
174
209
|
};
|
|
175
210
|
});
|
|
@@ -180,7 +215,7 @@ var PostgresExporter = /*#__PURE__*/function () {
|
|
|
180
215
|
value: function exportTables(tableIds, model, enumSet) {
|
|
181
216
|
var tableContentArr = PostgresExporter.getTableContentArr(tableIds, model, enumSet);
|
|
182
217
|
var tableStrs = tableContentArr.map(function (tableContent) {
|
|
183
|
-
var content = [].concat(_toConsumableArray(tableContent.fieldContents), _toConsumableArray(tableContent.compositePKs));
|
|
218
|
+
var content = [].concat(_toConsumableArray(tableContent.fieldContents), _toConsumableArray(tableContent.constraintContents), _toConsumableArray(tableContent.compositePKs));
|
|
184
219
|
var table = model.tables[tableContent.tableId];
|
|
185
220
|
var schema = model.schemas[table.schemaId];
|
|
186
221
|
var tableStr = "CREATE TABLE ".concat((0, _utils.shouldPrintSchema)(schema, model) ? "\"".concat(schema.name, "\".") : '', "\"").concat(table.name, "\" (\n").concat(content.map(function (line) {
|
|
@@ -53,6 +53,21 @@ var SqlServerExporter = /*#__PURE__*/function () {
|
|
|
53
53
|
if (field.increment) {
|
|
54
54
|
line += ' IDENTITY(1, 1)';
|
|
55
55
|
}
|
|
56
|
+
if (field.constraintIds && field.constraintIds.length > 0) {
|
|
57
|
+
if (field.constraintIds.length === 1) {
|
|
58
|
+
var constraint = model.constraints[field.constraintIds[0]];
|
|
59
|
+
if (constraint.name) {
|
|
60
|
+
line += " CONSTRAINT [".concat(constraint.name, "]");
|
|
61
|
+
}
|
|
62
|
+
line += " CHECK (".concat(constraint.expression, ")");
|
|
63
|
+
} else {
|
|
64
|
+
var constraintExpressions = field.constraintIds.map(function (constraintId) {
|
|
65
|
+
var constraint = model.constraints[constraintId];
|
|
66
|
+
return "(".concat(constraint.expression, ")");
|
|
67
|
+
});
|
|
68
|
+
line += " CHECK (".concat(constraintExpressions.join(' AND '), ")");
|
|
69
|
+
}
|
|
70
|
+
}
|
|
56
71
|
if (field.dbdefault) {
|
|
57
72
|
if (field.dbdefault.type === 'expression') {
|
|
58
73
|
line += " DEFAULT (".concat(field.dbdefault.value, ")");
|
|
@@ -92,15 +107,35 @@ var SqlServerExporter = /*#__PURE__*/function () {
|
|
|
92
107
|
});
|
|
93
108
|
return lines;
|
|
94
109
|
}
|
|
110
|
+
}, {
|
|
111
|
+
key: "getConstraintLines",
|
|
112
|
+
value: function getConstraintLines(tableId, model) {
|
|
113
|
+
var table = model.tables[tableId];
|
|
114
|
+
if (!table.constraintIds || table.constraintIds.length === 0) {
|
|
115
|
+
return [];
|
|
116
|
+
}
|
|
117
|
+
var lines = table.constraintIds.map(function (constraintId) {
|
|
118
|
+
var constraint = model.constraints[constraintId];
|
|
119
|
+
var line = '';
|
|
120
|
+
if (constraint.name) {
|
|
121
|
+
line = "CONSTRAINT [".concat(constraint.name, "] ");
|
|
122
|
+
}
|
|
123
|
+
line += "CHECK (".concat(constraint.expression, ")");
|
|
124
|
+
return line;
|
|
125
|
+
});
|
|
126
|
+
return lines;
|
|
127
|
+
}
|
|
95
128
|
}, {
|
|
96
129
|
key: "getTableContentArr",
|
|
97
130
|
value: function getTableContentArr(tableIds, model) {
|
|
98
131
|
var tableContentArr = tableIds.map(function (tableId) {
|
|
99
132
|
var fieldContents = SqlServerExporter.getFieldLines(tableId, model);
|
|
133
|
+
var constraintContents = SqlServerExporter.getConstraintLines(tableId, model);
|
|
100
134
|
var compositePKs = SqlServerExporter.getCompositePKs(tableId, model);
|
|
101
135
|
return {
|
|
102
136
|
tableId: tableId,
|
|
103
137
|
fieldContents: fieldContents,
|
|
138
|
+
constraintContents: constraintContents,
|
|
104
139
|
compositePKs: compositePKs
|
|
105
140
|
};
|
|
106
141
|
});
|
|
@@ -111,7 +146,7 @@ var SqlServerExporter = /*#__PURE__*/function () {
|
|
|
111
146
|
value: function exportTables(tableIds, model) {
|
|
112
147
|
var tableContentArr = SqlServerExporter.getTableContentArr(tableIds, model);
|
|
113
148
|
var tableStrs = tableContentArr.map(function (tableContent) {
|
|
114
|
-
var content = [].concat(_toConsumableArray(tableContent.fieldContents), _toConsumableArray(tableContent.compositePKs));
|
|
149
|
+
var content = [].concat(_toConsumableArray(tableContent.fieldContents), _toConsumableArray(tableContent.constraintContents), _toConsumableArray(tableContent.compositePKs));
|
|
115
150
|
var table = model.tables[tableContent.tableId];
|
|
116
151
|
var schema = model.schemas[table.schemaId];
|
|
117
152
|
var tableStr = "CREATE TABLE ".concat((0, _utils.shouldPrintSchema)(schema, model) ? "[".concat(schema.name, "].") : '', "[").concat(table.name, "] (\n").concat(content.map(function (line) {
|
package/lib/index.js
CHANGED
|
@@ -41,7 +41,7 @@ Object.defineProperty(exports, "importer", {
|
|
|
41
41
|
});
|
|
42
42
|
var _ModelExporter = _interopRequireDefault(require("./export/ModelExporter"));
|
|
43
43
|
var _Parser = _interopRequireDefault(require("./parse/Parser"));
|
|
44
|
-
var _error = require("
|
|
44
|
+
var _error = require("./parse/error");
|
|
45
45
|
var _import = _interopRequireDefault(require("./import"));
|
|
46
46
|
var _export = _interopRequireDefault(require("./export"));
|
|
47
47
|
var _version = require("./utils/version");
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
|
|
4
|
+
Object.defineProperty(exports, "__esModule", {
|
|
5
|
+
value: true
|
|
6
|
+
});
|
|
7
|
+
exports["default"] = void 0;
|
|
8
|
+
var _element = _interopRequireDefault(require("./element"));
|
|
9
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
|
|
10
|
+
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
|
|
11
|
+
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
|
|
12
|
+
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
|
|
13
|
+
function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
|
|
14
|
+
function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
|
|
15
|
+
function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
|
|
16
|
+
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
|
|
17
|
+
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
|
|
18
|
+
function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); }
|
|
19
|
+
function _possibleConstructorReturn(t, e) { if (e && ("object" == _typeof(e) || "function" == typeof e)) return e; if (void 0 !== e) throw new TypeError("Derived constructors may only return object or undefined"); return _assertThisInitialized(t); }
|
|
20
|
+
function _assertThisInitialized(e) { if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); return e; }
|
|
21
|
+
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
|
|
22
|
+
function _getPrototypeOf(t) { return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) { return t.__proto__ || Object.getPrototypeOf(t); }, _getPrototypeOf(t); }
|
|
23
|
+
function _inherits(t, e) { if ("function" != typeof e && null !== e) throw new TypeError("Super expression must either be null or a function"); t.prototype = Object.create(e && e.prototype, { constructor: { value: t, writable: !0, configurable: !0 } }), Object.defineProperty(t, "prototype", { writable: !1 }), e && _setPrototypeOf(t, e); }
|
|
24
|
+
function _setPrototypeOf(t, e) { return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) { return t.__proto__ = e, t; }, _setPrototypeOf(t, e); }
|
|
25
|
+
var Constraint = /*#__PURE__*/function (_Element) {
|
|
26
|
+
function Constraint() {
|
|
27
|
+
var _this;
|
|
28
|
+
var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
|
|
29
|
+
token = _ref.token,
|
|
30
|
+
name = _ref.name,
|
|
31
|
+
expression = _ref.expression,
|
|
32
|
+
table = _ref.table,
|
|
33
|
+
_ref$column = _ref.column,
|
|
34
|
+
column = _ref$column === void 0 ? null : _ref$column,
|
|
35
|
+
_ref$injectedPartial = _ref.injectedPartial,
|
|
36
|
+
injectedPartial = _ref$injectedPartial === void 0 ? null : _ref$injectedPartial;
|
|
37
|
+
_classCallCheck(this, Constraint);
|
|
38
|
+
_this = _callSuper(this, Constraint, [token]);
|
|
39
|
+
_this.name = name;
|
|
40
|
+
_this.expression = expression;
|
|
41
|
+
_this.table = table;
|
|
42
|
+
_this.column = column;
|
|
43
|
+
_this.injectedPartial = injectedPartial;
|
|
44
|
+
_this.dbState = _this.table.dbState;
|
|
45
|
+
_this.generateId();
|
|
46
|
+
return _this;
|
|
47
|
+
}
|
|
48
|
+
_inherits(Constraint, _Element);
|
|
49
|
+
return _createClass(Constraint, [{
|
|
50
|
+
key: "generateId",
|
|
51
|
+
value: function generateId() {
|
|
52
|
+
this.id = this.dbState.generateId('constraintId');
|
|
53
|
+
}
|
|
54
|
+
}, {
|
|
55
|
+
key: "export",
|
|
56
|
+
value: function _export() {
|
|
57
|
+
return _objectSpread({}, this.shallowExport());
|
|
58
|
+
}
|
|
59
|
+
}, {
|
|
60
|
+
key: "exportParentIds",
|
|
61
|
+
value: function exportParentIds() {
|
|
62
|
+
var _this$column, _this$injectedPartial;
|
|
63
|
+
return {
|
|
64
|
+
tableId: this.table.id,
|
|
65
|
+
columnId: (_this$column = this.column) === null || _this$column === void 0 ? void 0 : _this$column.id,
|
|
66
|
+
injectedPartialId: (_this$injectedPartial = this.injectedPartial) === null || _this$injectedPartial === void 0 ? void 0 : _this$injectedPartial.id
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
}, {
|
|
70
|
+
key: "shallowExport",
|
|
71
|
+
value: function shallowExport() {
|
|
72
|
+
return {
|
|
73
|
+
name: this.name,
|
|
74
|
+
expression: this.expression
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
}, {
|
|
78
|
+
key: "normalize",
|
|
79
|
+
value: function normalize(model) {
|
|
80
|
+
model.constraints[this.id] = _objectSpread(_objectSpread({
|
|
81
|
+
id: this.id
|
|
82
|
+
}, this.shallowExport()), this.exportParentIds());
|
|
83
|
+
}
|
|
84
|
+
}]);
|
|
85
|
+
}(_element["default"]);
|
|
86
|
+
var _default = exports["default"] = Constraint;
|
|
@@ -8,6 +8,7 @@ exports["default"] = void 0;
|
|
|
8
8
|
var _lodash = require("lodash");
|
|
9
9
|
var _element = _interopRequireDefault(require("./element"));
|
|
10
10
|
var _config = require("./config");
|
|
11
|
+
var _constraint = _interopRequireDefault(require("./constraint"));
|
|
11
12
|
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
|
|
12
13
|
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
|
|
13
14
|
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
|
|
@@ -37,6 +38,8 @@ var Field = /*#__PURE__*/function (_Element) {
|
|
|
37
38
|
note = _ref.note,
|
|
38
39
|
dbdefault = _ref.dbdefault,
|
|
39
40
|
increment = _ref.increment,
|
|
41
|
+
_ref$constraints = _ref.constraints,
|
|
42
|
+
constraints = _ref$constraints === void 0 ? [] : _ref$constraints,
|
|
40
43
|
_ref$table = _ref.table,
|
|
41
44
|
table = _ref$table === void 0 ? {} : _ref$table,
|
|
42
45
|
_ref$noteToken = _ref.noteToken,
|
|
@@ -63,6 +66,7 @@ var Field = /*#__PURE__*/function (_Element) {
|
|
|
63
66
|
_this.noteToken = note ? (0, _lodash.get)(note, 'token', noteToken) : null;
|
|
64
67
|
_this.dbdefault = dbdefault;
|
|
65
68
|
_this.increment = increment;
|
|
69
|
+
_this.constraints = [];
|
|
66
70
|
_this.endpoints = [];
|
|
67
71
|
_this.table = table;
|
|
68
72
|
_this.injectedPartial = injectedPartial;
|
|
@@ -70,6 +74,7 @@ var Field = /*#__PURE__*/function (_Element) {
|
|
|
70
74
|
_this.dbState = _this.table.dbState;
|
|
71
75
|
_this.generateId();
|
|
72
76
|
_this.bindType();
|
|
77
|
+
_this.processConstraints(constraints);
|
|
73
78
|
return _this;
|
|
74
79
|
}
|
|
75
80
|
_inherits(Field, _Element);
|
|
@@ -143,7 +148,10 @@ var Field = /*#__PURE__*/function (_Element) {
|
|
|
143
148
|
note: this.note,
|
|
144
149
|
dbdefault: this.dbdefault,
|
|
145
150
|
increment: this.increment,
|
|
146
|
-
injectedPartialId: (_this$injectedPartial = this.injectedPartial) === null || _this$injectedPartial === void 0 ? void 0 : _this$injectedPartial.id
|
|
151
|
+
injectedPartialId: (_this$injectedPartial = this.injectedPartial) === null || _this$injectedPartial === void 0 ? void 0 : _this$injectedPartial.id,
|
|
152
|
+
constraintIds: this.constraints.map(function (constraint) {
|
|
153
|
+
return constraint.id;
|
|
154
|
+
})
|
|
147
155
|
};
|
|
148
156
|
}
|
|
149
157
|
}, {
|
|
@@ -152,6 +160,20 @@ var Field = /*#__PURE__*/function (_Element) {
|
|
|
152
160
|
model.fields[this.id] = _objectSpread(_objectSpread(_objectSpread({
|
|
153
161
|
id: this.id
|
|
154
162
|
}, this.shallowExport()), this.exportChildIds()), this.exportParentIds());
|
|
163
|
+
this.constraints.forEach(function (constraint) {
|
|
164
|
+
return constraint.normalize(model);
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
}, {
|
|
168
|
+
key: "processConstraints",
|
|
169
|
+
value: function processConstraints(constraints) {
|
|
170
|
+
var _this2 = this;
|
|
171
|
+
constraints.forEach(function (constraint) {
|
|
172
|
+
_this2.constraints.push(new _constraint["default"](_objectSpread(_objectSpread({}, constraint), {}, {
|
|
173
|
+
table: _this2.table,
|
|
174
|
+
column: _this2
|
|
175
|
+
})));
|
|
176
|
+
});
|
|
155
177
|
}
|
|
156
178
|
}]);
|
|
157
179
|
}(_element["default"]);
|