@dbml/core 3.6.1 → 3.7.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/connectors/ utils.js +11 -0
- package/lib/connectors/connector.js +40 -0
- package/lib/connectors/dbml.js +170 -0
- package/lib/connectors/mssqlConnector.js +391 -0
- package/lib/connectors/mysqlConnector.js +490 -0
- package/lib/connectors/postgresConnector.js +400 -0
- package/lib/export/DbmlExporter.js +5 -2
- package/lib/import/index.js +32 -1
- package/lib/model_structure/endpoint.js +2 -2
- package/lib/model_structure/enum.js +2 -2
- package/lib/model_structure/enumValue.js +2 -2
- package/lib/model_structure/field.js +2 -2
- package/lib/model_structure/indexColumn.js +2 -2
- package/lib/model_structure/indexes.js +2 -2
- package/lib/model_structure/ref.js +2 -2
- package/lib/model_structure/schema.js +2 -2
- package/lib/model_structure/stickyNote.js +2 -2
- package/lib/model_structure/table.js +2 -2
- package/lib/model_structure/tableGroup.js +2 -2
- package/lib/parse/ANTLR/ASTGeneration/AST.js +1 -0
- package/lib/parse/ANTLR/ASTGeneration/snowflake/SnowflakeASTGen.js +1 -1
- package/lib/parse/DatabaseGenerator.js +118 -0
- package/package.json +7 -3
- package/types/import/index.d.ts +2 -0
|
@@ -0,0 +1,391 @@
|
|
|
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.fetchSchemaJson = void 0;
|
|
8
|
+
var _mssql = _interopRequireDefault(require("mssql"));
|
|
9
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
|
10
|
+
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
|
|
11
|
+
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."); }
|
|
12
|
+
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); }
|
|
13
|
+
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
|
|
14
|
+
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
|
|
15
|
+
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; }
|
|
16
|
+
function _regeneratorRuntime() { "use strict"; /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */ _regeneratorRuntime = function _regeneratorRuntime() { return exports; }; var exports = {}, Op = Object.prototype, hasOwn = Op.hasOwnProperty, defineProperty = Object.defineProperty || function (obj, key, desc) { obj[key] = desc.value; }, $Symbol = "function" == typeof Symbol ? Symbol : {}, iteratorSymbol = $Symbol.iterator || "@@iterator", asyncIteratorSymbol = $Symbol.asyncIterator || "@@asyncIterator", toStringTagSymbol = $Symbol.toStringTag || "@@toStringTag"; function define(obj, key, value) { return Object.defineProperty(obj, key, { value: value, enumerable: !0, configurable: !0, writable: !0 }), obj[key]; } try { define({}, ""); } catch (err) { define = function define(obj, key, value) { return obj[key] = value; }; } function wrap(innerFn, outerFn, self, tryLocsList) { var protoGenerator = outerFn && outerFn.prototype instanceof Generator ? outerFn : Generator, generator = Object.create(protoGenerator.prototype), context = new Context(tryLocsList || []); return defineProperty(generator, "_invoke", { value: makeInvokeMethod(innerFn, self, context) }), generator; } function tryCatch(fn, obj, arg) { try { return { type: "normal", arg: fn.call(obj, arg) }; } catch (err) { return { type: "throw", arg: err }; } } exports.wrap = wrap; var ContinueSentinel = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} var IteratorPrototype = {}; define(IteratorPrototype, iteratorSymbol, function () { return this; }); var getProto = Object.getPrototypeOf, NativeIteratorPrototype = getProto && getProto(getProto(values([]))); NativeIteratorPrototype && NativeIteratorPrototype !== Op && hasOwn.call(NativeIteratorPrototype, iteratorSymbol) && (IteratorPrototype = NativeIteratorPrototype); var Gp = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(IteratorPrototype); function defineIteratorMethods(prototype) { ["next", "throw", "return"].forEach(function (method) { define(prototype, method, function (arg) { return this._invoke(method, arg); }); }); } function AsyncIterator(generator, PromiseImpl) { function invoke(method, arg, resolve, reject) { var record = tryCatch(generator[method], generator, arg); if ("throw" !== record.type) { var result = record.arg, value = result.value; return value && "object" == _typeof(value) && hasOwn.call(value, "__await") ? PromiseImpl.resolve(value.__await).then(function (value) { invoke("next", value, resolve, reject); }, function (err) { invoke("throw", err, resolve, reject); }) : PromiseImpl.resolve(value).then(function (unwrapped) { result.value = unwrapped, resolve(result); }, function (error) { return invoke("throw", error, resolve, reject); }); } reject(record.arg); } var previousPromise; defineProperty(this, "_invoke", { value: function value(method, arg) { function callInvokeWithMethodAndArg() { return new PromiseImpl(function (resolve, reject) { invoke(method, arg, resolve, reject); }); } return previousPromise = previousPromise ? previousPromise.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg(); } }); } function makeInvokeMethod(innerFn, self, context) { var state = "suspendedStart"; return function (method, arg) { if ("executing" === state) throw new Error("Generator is already running"); if ("completed" === state) { if ("throw" === method) throw arg; return doneResult(); } for (context.method = method, context.arg = arg;;) { var delegate = context.delegate; if (delegate) { var delegateResult = maybeInvokeDelegate(delegate, context); if (delegateResult) { if (delegateResult === ContinueSentinel) continue; return delegateResult; } } if ("next" === context.method) context.sent = context._sent = context.arg;else if ("throw" === context.method) { if ("suspendedStart" === state) throw state = "completed", context.arg; context.dispatchException(context.arg); } else "return" === context.method && context.abrupt("return", context.arg); state = "executing"; var record = tryCatch(innerFn, self, context); if ("normal" === record.type) { if (state = context.done ? "completed" : "suspendedYield", record.arg === ContinueSentinel) continue; return { value: record.arg, done: context.done }; } "throw" === record.type && (state = "completed", context.method = "throw", context.arg = record.arg); } }; } function maybeInvokeDelegate(delegate, context) { var methodName = context.method, method = delegate.iterator[methodName]; if (undefined === method) return context.delegate = null, "throw" === methodName && delegate.iterator["return"] && (context.method = "return", context.arg = undefined, maybeInvokeDelegate(delegate, context), "throw" === context.method) || "return" !== methodName && (context.method = "throw", context.arg = new TypeError("The iterator does not provide a '" + methodName + "' method")), ContinueSentinel; var record = tryCatch(method, delegate.iterator, context.arg); if ("throw" === record.type) return context.method = "throw", context.arg = record.arg, context.delegate = null, ContinueSentinel; var info = record.arg; return info ? info.done ? (context[delegate.resultName] = info.value, context.next = delegate.nextLoc, "return" !== context.method && (context.method = "next", context.arg = undefined), context.delegate = null, ContinueSentinel) : info : (context.method = "throw", context.arg = new TypeError("iterator result is not an object"), context.delegate = null, ContinueSentinel); } function pushTryEntry(locs) { var entry = { tryLoc: locs[0] }; 1 in locs && (entry.catchLoc = locs[1]), 2 in locs && (entry.finallyLoc = locs[2], entry.afterLoc = locs[3]), this.tryEntries.push(entry); } function resetTryEntry(entry) { var record = entry.completion || {}; record.type = "normal", delete record.arg, entry.completion = record; } function Context(tryLocsList) { this.tryEntries = [{ tryLoc: "root" }], tryLocsList.forEach(pushTryEntry, this), this.reset(!0); } function values(iterable) { if (iterable) { var iteratorMethod = iterable[iteratorSymbol]; if (iteratorMethod) return iteratorMethod.call(iterable); if ("function" == typeof iterable.next) return iterable; if (!isNaN(iterable.length)) { var i = -1, next = function next() { for (; ++i < iterable.length;) if (hasOwn.call(iterable, i)) return next.value = iterable[i], next.done = !1, next; return next.value = undefined, next.done = !0, next; }; return next.next = next; } } return { next: doneResult }; } function doneResult() { return { value: undefined, done: !0 }; } return GeneratorFunction.prototype = GeneratorFunctionPrototype, defineProperty(Gp, "constructor", { value: GeneratorFunctionPrototype, configurable: !0 }), defineProperty(GeneratorFunctionPrototype, "constructor", { value: GeneratorFunction, configurable: !0 }), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, toStringTagSymbol, "GeneratorFunction"), exports.isGeneratorFunction = function (genFun) { var ctor = "function" == typeof genFun && genFun.constructor; return !!ctor && (ctor === GeneratorFunction || "GeneratorFunction" === (ctor.displayName || ctor.name)); }, exports.mark = function (genFun) { return Object.setPrototypeOf ? Object.setPrototypeOf(genFun, GeneratorFunctionPrototype) : (genFun.__proto__ = GeneratorFunctionPrototype, define(genFun, toStringTagSymbol, "GeneratorFunction")), genFun.prototype = Object.create(Gp), genFun; }, exports.awrap = function (arg) { return { __await: arg }; }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, asyncIteratorSymbol, function () { return this; }), exports.AsyncIterator = AsyncIterator, exports.async = function (innerFn, outerFn, self, tryLocsList, PromiseImpl) { void 0 === PromiseImpl && (PromiseImpl = Promise); var iter = new AsyncIterator(wrap(innerFn, outerFn, self, tryLocsList), PromiseImpl); return exports.isGeneratorFunction(outerFn) ? iter : iter.next().then(function (result) { return result.done ? result.value : iter.next(); }); }, defineIteratorMethods(Gp), define(Gp, toStringTagSymbol, "Generator"), define(Gp, iteratorSymbol, function () { return this; }), define(Gp, "toString", function () { return "[object Generator]"; }), exports.keys = function (val) { var object = Object(val), keys = []; for (var key in object) keys.push(key); return keys.reverse(), function next() { for (; keys.length;) { var key = keys.pop(); if (key in object) return next.value = key, next.done = !1, next; } return next.done = !0, next; }; }, exports.values = values, Context.prototype = { constructor: Context, reset: function reset(skipTempReset) { if (this.prev = 0, this.next = 0, this.sent = this._sent = undefined, this.done = !1, this.delegate = null, this.method = "next", this.arg = undefined, this.tryEntries.forEach(resetTryEntry), !skipTempReset) for (var name in this) "t" === name.charAt(0) && hasOwn.call(this, name) && !isNaN(+name.slice(1)) && (this[name] = undefined); }, stop: function stop() { this.done = !0; var rootRecord = this.tryEntries[0].completion; if ("throw" === rootRecord.type) throw rootRecord.arg; return this.rval; }, dispatchException: function dispatchException(exception) { if (this.done) throw exception; var context = this; function handle(loc, caught) { return record.type = "throw", record.arg = exception, context.next = loc, caught && (context.method = "next", context.arg = undefined), !!caught; } for (var i = this.tryEntries.length - 1; i >= 0; --i) { var entry = this.tryEntries[i], record = entry.completion; if ("root" === entry.tryLoc) return handle("end"); if (entry.tryLoc <= this.prev) { var hasCatch = hasOwn.call(entry, "catchLoc"), hasFinally = hasOwn.call(entry, "finallyLoc"); if (hasCatch && hasFinally) { if (this.prev < entry.catchLoc) return handle(entry.catchLoc, !0); if (this.prev < entry.finallyLoc) return handle(entry.finallyLoc); } else if (hasCatch) { if (this.prev < entry.catchLoc) return handle(entry.catchLoc, !0); } else { if (!hasFinally) throw new Error("try statement without catch or finally"); if (this.prev < entry.finallyLoc) return handle(entry.finallyLoc); } } } }, abrupt: function abrupt(type, arg) { for (var i = this.tryEntries.length - 1; i >= 0; --i) { var entry = this.tryEntries[i]; if (entry.tryLoc <= this.prev && hasOwn.call(entry, "finallyLoc") && this.prev < entry.finallyLoc) { var finallyEntry = entry; break; } } finallyEntry && ("break" === type || "continue" === type) && finallyEntry.tryLoc <= arg && arg <= finallyEntry.finallyLoc && (finallyEntry = null); var record = finallyEntry ? finallyEntry.completion : {}; return record.type = type, record.arg = arg, finallyEntry ? (this.method = "next", this.next = finallyEntry.finallyLoc, ContinueSentinel) : this.complete(record); }, complete: function complete(record, afterLoc) { if ("throw" === record.type) throw record.arg; return "break" === record.type || "continue" === record.type ? this.next = record.arg : "return" === record.type ? (this.rval = this.arg = record.arg, this.method = "return", this.next = "end") : "normal" === record.type && afterLoc && (this.next = afterLoc), ContinueSentinel; }, finish: function finish(finallyLoc) { for (var i = this.tryEntries.length - 1; i >= 0; --i) { var entry = this.tryEntries[i]; if (entry.finallyLoc === finallyLoc) return this.complete(entry.completion, entry.afterLoc), resetTryEntry(entry), ContinueSentinel; } }, "catch": function _catch(tryLoc) { for (var i = this.tryEntries.length - 1; i >= 0; --i) { var entry = this.tryEntries[i]; if (entry.tryLoc === tryLoc) { var record = entry.completion; if ("throw" === record.type) { var thrown = record.arg; resetTryEntry(entry); } return thrown; } } throw new Error("illegal catch attempt"); }, delegateYield: function delegateYield(iterable, resultName, nextLoc) { return this.delegate = { iterator: values(iterable), resultName: resultName, nextLoc: nextLoc }, "next" === this.method && (this.arg = undefined), ContinueSentinel; } }, exports; }
|
|
17
|
+
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
|
|
18
|
+
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; } /* eslint-disable camelcase */
|
|
19
|
+
var MSSQL_DATE_TYPES = ['date', 'datetime', 'datetime2', 'smalldatetime', 'datetimeoffset', 'time'];
|
|
20
|
+
var getValidatedClient = /*#__PURE__*/function () {
|
|
21
|
+
var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(connection) {
|
|
22
|
+
var pool;
|
|
23
|
+
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
24
|
+
while (1) switch (_context.prev = _context.next) {
|
|
25
|
+
case 0:
|
|
26
|
+
_context.prev = 0;
|
|
27
|
+
_context.next = 3;
|
|
28
|
+
return _mssql["default"].connect(connection);
|
|
29
|
+
case 3:
|
|
30
|
+
pool = _context.sent;
|
|
31
|
+
_context.next = 6;
|
|
32
|
+
return pool.request().query('SELECT 1');
|
|
33
|
+
case 6:
|
|
34
|
+
return _context.abrupt("return", pool);
|
|
35
|
+
case 9:
|
|
36
|
+
_context.prev = 9;
|
|
37
|
+
_context.t0 = _context["catch"](0);
|
|
38
|
+
// Log the error and handle it as per your application's requirement
|
|
39
|
+
console.error('SQL connection error:', _context.t0);
|
|
40
|
+
|
|
41
|
+
// Ensure to close any open pool in case of failure
|
|
42
|
+
if (!_mssql["default"].connected) {
|
|
43
|
+
_context.next = 15;
|
|
44
|
+
break;
|
|
45
|
+
}
|
|
46
|
+
_context.next = 15;
|
|
47
|
+
return _mssql["default"].close();
|
|
48
|
+
case 15:
|
|
49
|
+
throw _context.t0;
|
|
50
|
+
case 16:
|
|
51
|
+
case "end":
|
|
52
|
+
return _context.stop();
|
|
53
|
+
}
|
|
54
|
+
}, _callee, null, [[0, 9]]);
|
|
55
|
+
}));
|
|
56
|
+
return function getValidatedClient(_x) {
|
|
57
|
+
return _ref.apply(this, arguments);
|
|
58
|
+
};
|
|
59
|
+
}();
|
|
60
|
+
var convertQueryBoolean = function convertQueryBoolean(val) {
|
|
61
|
+
return val === 'YES';
|
|
62
|
+
};
|
|
63
|
+
var getFieldType = function getFieldType(data_type, default_type, character_maximum_length, numeric_precision, numeric_scale) {
|
|
64
|
+
if (MSSQL_DATE_TYPES.includes(data_type)) {
|
|
65
|
+
return data_type;
|
|
66
|
+
}
|
|
67
|
+
if (data_type === 'bit') {
|
|
68
|
+
return data_type;
|
|
69
|
+
}
|
|
70
|
+
if (numeric_precision && numeric_scale && default_type === 'number') {
|
|
71
|
+
return "".concat(data_type, "(").concat(numeric_precision, ",").concat(numeric_scale, ")");
|
|
72
|
+
}
|
|
73
|
+
if (character_maximum_length && character_maximum_length > 0 && default_type === 'string') {
|
|
74
|
+
return "".concat(data_type, "(").concat(character_maximum_length, ")");
|
|
75
|
+
}
|
|
76
|
+
return data_type;
|
|
77
|
+
};
|
|
78
|
+
var getDbdefault = function getDbdefault(data_type, column_default, default_type) {
|
|
79
|
+
// The regex below is used to extract the value from the default value
|
|
80
|
+
// \( and \) are used to escape parentheses
|
|
81
|
+
// [^()]+ is used to match any character except parentheses
|
|
82
|
+
// Example: (1) => 1, ('hello') => hello, getdate()-(1) => getdate()-1
|
|
83
|
+
var value = column_default.slice(1, -1).replace(/\(([^()]+)\)/g, '$1');
|
|
84
|
+
return {
|
|
85
|
+
type: default_type,
|
|
86
|
+
value: default_type === 'string' ? value.slice(1, -1) : value // Remove the quotes for string values
|
|
87
|
+
};
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
var getEnumValues = function getEnumValues(definition) {
|
|
91
|
+
// Use the example below to understand the regex:
|
|
92
|
+
// ([quantity]>(0))
|
|
93
|
+
// ([unit_price]>(0))
|
|
94
|
+
// ([status]='cancelled' OR [status]='delivered' OR [status]='shipped' OR [status]='processing' OR [status]='pending')
|
|
95
|
+
// ([total_amount]>(0))
|
|
96
|
+
// ([price]>(0))
|
|
97
|
+
// ([stock_quantity]>=(0))
|
|
98
|
+
// ([age_start]<=[age_end])
|
|
99
|
+
// ([age_start]<=[age_end])
|
|
100
|
+
// ([gender]='Other' OR [gender]='Female' OR [gender]='Male')
|
|
101
|
+
// ([date_of_birth]<=dateadd(year,(-13),getdate()))
|
|
102
|
+
// ([email] like '%_@_%._%')
|
|
103
|
+
if (!definition) return null;
|
|
104
|
+
var values = definition.match(/\[([^\]]+)\]='([^']+)'/g); // Extracting the enum values when the definition contains ]='
|
|
105
|
+
if (!values) return null;
|
|
106
|
+
var enumValues = values.map(function (value) {
|
|
107
|
+
var enumValue = value.split("]='")[1];
|
|
108
|
+
return {
|
|
109
|
+
name: enumValue.slice(0, -1)
|
|
110
|
+
};
|
|
111
|
+
});
|
|
112
|
+
return enumValues;
|
|
113
|
+
};
|
|
114
|
+
var generateField = function generateField(row) {
|
|
115
|
+
var column_name = row.column_name,
|
|
116
|
+
data_type = row.data_type,
|
|
117
|
+
character_maximum_length = row.character_maximum_length,
|
|
118
|
+
numeric_precision = row.numeric_precision,
|
|
119
|
+
numeric_scale = row.numeric_scale,
|
|
120
|
+
identity_increment = row.identity_increment,
|
|
121
|
+
is_nullable = row.is_nullable,
|
|
122
|
+
column_default = row.column_default,
|
|
123
|
+
default_type = row.default_type,
|
|
124
|
+
column_comment = row.column_comment;
|
|
125
|
+
var dbdefault = column_default && default_type !== 'increment' ? getDbdefault(data_type, column_default, default_type) : null;
|
|
126
|
+
var fieldType = {
|
|
127
|
+
type_name: getFieldType(data_type, default_type, character_maximum_length, numeric_precision, numeric_scale),
|
|
128
|
+
schemaname: null
|
|
129
|
+
};
|
|
130
|
+
return {
|
|
131
|
+
name: column_name,
|
|
132
|
+
type: fieldType,
|
|
133
|
+
dbdefault: dbdefault,
|
|
134
|
+
not_null: !convertQueryBoolean(is_nullable),
|
|
135
|
+
increment: !!identity_increment,
|
|
136
|
+
note: column_comment ? {
|
|
137
|
+
value: column_comment
|
|
138
|
+
} : {
|
|
139
|
+
value: ''
|
|
140
|
+
}
|
|
141
|
+
};
|
|
142
|
+
};
|
|
143
|
+
var generateTablesFieldsAndEnums = /*#__PURE__*/function () {
|
|
144
|
+
var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(client) {
|
|
145
|
+
var fields, enums, tablesAndFieldsSql, tablesAndFieldsResult, tables;
|
|
146
|
+
return _regeneratorRuntime().wrap(function _callee2$(_context2) {
|
|
147
|
+
while (1) switch (_context2.prev = _context2.next) {
|
|
148
|
+
case 0:
|
|
149
|
+
fields = {};
|
|
150
|
+
enums = [];
|
|
151
|
+
tablesAndFieldsSql = "\n WITH tables_and_fields AS (\n SELECT\n s.name AS table_schema,\n t.name AS table_name,\n c.name AS column_name,\n ty.name AS data_type,\n c.max_length AS character_maximum_length,\n c.precision AS numeric_precision,\n c.scale AS numeric_scale,\n c.is_identity AS identity_increment,\n CASE\n WHEN c.is_nullable = 1 THEN 'YES'\n ELSE 'NO'\n END AS is_nullable,\n CASE\n WHEN c.default_object_id = 0 THEN NULL\n ELSE OBJECT_DEFINITION(c.default_object_id)\n END AS column_default,\n -- Fetching table comments\n p.value AS table_comment,\n ep.value AS column_comment\n FROM\n sys.tables t\n JOIN\n sys.schemas s ON t.schema_id = s.schema_id\n JOIN\n sys.columns c ON t.object_id = c.object_id\n JOIN\n sys.types ty ON c.user_type_id = ty.user_type_id\n LEFT JOIN\n sys.extended_properties p ON p.major_id = t.object_id\n AND p.name = 'MS_Description'\n AND p.minor_id = 0 -- Ensure minor_id is 0 for table comments\n LEFT JOIN\n sys.extended_properties ep ON ep.major_id = c.object_id\n AND ep.minor_id = c.column_id\n AND ep.name = 'MS_Description'\n WHERE\n t.type = 'U' -- User-defined tables\n )\n SELECT\n tf.table_schema,\n tf.table_name,\n tf.column_name,\n tf.data_type,\n tf.character_maximum_length,\n tf.numeric_precision,\n tf.numeric_scale,\n tf.identity_increment,\n tf.is_nullable,\n tf.column_default,\n tf.table_comment,\n tf.column_comment,\n cc.name AS check_constraint_name, -- Adding CHECK constraint name\n cc.definition AS check_constraint_definition, -- Adding CHECK constraint definition\n CASE\n WHEN tf.column_default LIKE '((%))' THEN 'number'\n WHEN tf.column_default LIKE '(''%'')' THEN 'string'\n ELSE 'expression'\n END AS default_type\n FROM\n tables_and_fields AS tf\n LEFT JOIN\n sys.check_constraints cc ON cc.parent_object_id = OBJECT_ID(tf.table_schema + '.' + tf.table_name)\n AND cc.definition LIKE '%' + tf.column_name + '%' -- Ensure the constraint references the column\n ORDER BY\n tf.table_schema,\n tf.table_name,\n tf.column_name;\n ";
|
|
152
|
+
_context2.next = 5;
|
|
153
|
+
return client.query(tablesAndFieldsSql);
|
|
154
|
+
case 5:
|
|
155
|
+
tablesAndFieldsResult = _context2.sent;
|
|
156
|
+
tables = tablesAndFieldsResult.recordset.reduce(function (acc, row) {
|
|
157
|
+
var table_schema = row.table_schema,
|
|
158
|
+
table_name = row.table_name,
|
|
159
|
+
table_comment = row.table_comment,
|
|
160
|
+
check_constraint_name = row.check_constraint_name,
|
|
161
|
+
check_constraint_definition = row.check_constraint_definition;
|
|
162
|
+
var key = "".concat(table_schema, ".").concat(table_name);
|
|
163
|
+
if (!acc[key]) {
|
|
164
|
+
acc[key] = {
|
|
165
|
+
name: table_name,
|
|
166
|
+
schemaName: table_schema,
|
|
167
|
+
note: table_comment ? {
|
|
168
|
+
value: table_comment
|
|
169
|
+
} : {
|
|
170
|
+
value: ''
|
|
171
|
+
}
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
var enumValues = getEnumValues(check_constraint_definition);
|
|
175
|
+
if (enumValues) {
|
|
176
|
+
enums.push({
|
|
177
|
+
name: check_constraint_name,
|
|
178
|
+
schemaName: table_schema,
|
|
179
|
+
values: enumValues
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
if (!fields[key]) fields[key] = [];
|
|
183
|
+
var field = generateField(row);
|
|
184
|
+
if (enumValues) {
|
|
185
|
+
field.type = {
|
|
186
|
+
type_name: check_constraint_name,
|
|
187
|
+
schemaName: table_schema
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
fields[key].push(field);
|
|
191
|
+
return acc;
|
|
192
|
+
}, {});
|
|
193
|
+
return _context2.abrupt("return", {
|
|
194
|
+
tables: Object.values(tables),
|
|
195
|
+
fields: fields,
|
|
196
|
+
enums: enums
|
|
197
|
+
});
|
|
198
|
+
case 8:
|
|
199
|
+
case "end":
|
|
200
|
+
return _context2.stop();
|
|
201
|
+
}
|
|
202
|
+
}, _callee2);
|
|
203
|
+
}));
|
|
204
|
+
return function generateTablesFieldsAndEnums(_x2) {
|
|
205
|
+
return _ref2.apply(this, arguments);
|
|
206
|
+
};
|
|
207
|
+
}();
|
|
208
|
+
var generateRefs = /*#__PURE__*/function () {
|
|
209
|
+
var _ref3 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3(client) {
|
|
210
|
+
var refs, refsListSql, refsQueryResult;
|
|
211
|
+
return _regeneratorRuntime().wrap(function _callee3$(_context3) {
|
|
212
|
+
while (1) switch (_context3.prev = _context3.next) {
|
|
213
|
+
case 0:
|
|
214
|
+
refs = [];
|
|
215
|
+
refsListSql = "\n SELECT\n s.name AS table_schema,\n t.name AS table_name,\n fk.name AS fk_constraint_name,\n STUFF((\n SELECT ',' + c1.name\n FROM sys.foreign_key_columns AS fkc\n JOIN sys.columns AS c1 ON fkc.parent_object_id = c1.object_id AND fkc.parent_column_id = c1.column_id\n WHERE fkc.constraint_object_id = fk.object_id\n FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 1, '') AS column_names,\n s2.name AS foreign_table_schema,\n t2.name AS foreign_table_name,\n STUFF((\n SELECT ',' + c2.name\n FROM sys.foreign_key_columns AS fkc\n JOIN sys.columns AS c2 ON fkc.referenced_object_id = c2.object_id AND fkc.referenced_column_id = c2.column_id\n WHERE fkc.constraint_object_id = fk.object_id\n FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 1, '') AS foreign_column_names,\n fk.type_desc AS constraint_type,\n fk.delete_referential_action_desc AS on_delete,\n fk.update_referential_action_desc AS on_update\n FROM sys.foreign_keys AS fk\n JOIN sys.tables AS t ON fk.parent_object_id = t.object_id\n JOIN sys.schemas AS s ON t.schema_id = s.schema_id\n JOIN sys.tables AS t2 ON fk.referenced_object_id = t2.object_id\n JOIN sys.schemas AS s2 ON t2.schema_id = s2.schema_id\n WHERE s.name NOT IN ('sys', 'information_schema')\n ORDER BY\n s.name,\n t.name;\n ";
|
|
216
|
+
_context3.next = 4;
|
|
217
|
+
return client.query(refsListSql);
|
|
218
|
+
case 4:
|
|
219
|
+
refsQueryResult = _context3.sent;
|
|
220
|
+
refsQueryResult.recordset.forEach(function (refRow) {
|
|
221
|
+
var table_schema = refRow.table_schema,
|
|
222
|
+
fk_constraint_name = refRow.fk_constraint_name,
|
|
223
|
+
table_name = refRow.table_name,
|
|
224
|
+
column_names = refRow.column_names,
|
|
225
|
+
foreign_table_schema = refRow.foreign_table_schema,
|
|
226
|
+
foreign_table_name = refRow.foreign_table_name,
|
|
227
|
+
foreign_column_names = refRow.foreign_column_names,
|
|
228
|
+
on_delete = refRow.on_delete,
|
|
229
|
+
on_update = refRow.on_update;
|
|
230
|
+
var ep1 = {
|
|
231
|
+
tableName: table_name,
|
|
232
|
+
schemaName: table_schema,
|
|
233
|
+
fieldNames: column_names.split(','),
|
|
234
|
+
relation: '*'
|
|
235
|
+
};
|
|
236
|
+
var ep2 = {
|
|
237
|
+
tableName: foreign_table_name,
|
|
238
|
+
schemaName: foreign_table_schema,
|
|
239
|
+
fieldNames: foreign_column_names.split(','),
|
|
240
|
+
relation: '1'
|
|
241
|
+
};
|
|
242
|
+
refs.push({
|
|
243
|
+
name: fk_constraint_name,
|
|
244
|
+
endpoints: [ep1, ep2],
|
|
245
|
+
onDelete: on_delete === 'NO_ACTION' ? null : on_delete,
|
|
246
|
+
onUpdate: on_update === 'NO_ACTION' ? null : on_update
|
|
247
|
+
});
|
|
248
|
+
});
|
|
249
|
+
return _context3.abrupt("return", refs);
|
|
250
|
+
case 7:
|
|
251
|
+
case "end":
|
|
252
|
+
return _context3.stop();
|
|
253
|
+
}
|
|
254
|
+
}, _callee3);
|
|
255
|
+
}));
|
|
256
|
+
return function generateRefs(_x3) {
|
|
257
|
+
return _ref3.apply(this, arguments);
|
|
258
|
+
};
|
|
259
|
+
}();
|
|
260
|
+
var generateIndexes = /*#__PURE__*/function () {
|
|
261
|
+
var _ref4 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee4(client) {
|
|
262
|
+
var indexListSql, indexListResult, _indexListResult$reco, outOfLineConstraints, inlineConstraints, indexes, tableConstraints;
|
|
263
|
+
return _regeneratorRuntime().wrap(function _callee4$(_context4) {
|
|
264
|
+
while (1) switch (_context4.prev = _context4.next) {
|
|
265
|
+
case 0:
|
|
266
|
+
indexListSql = "\n WITH user_tables AS (\n SELECT\n TABLE_SCHEMA,\n TABLE_NAME\n FROM\n INFORMATION_SCHEMA.TABLES\n WHERE\n TABLE_TYPE = 'BASE TABLE' -- Ensure we are only getting base tables\n AND TABLE_NAME NOT LIKE 'dt%'\n AND TABLE_NAME NOT LIKE 'syscs%'\n AND TABLE_NAME NOT LIKE 'sysss%'\n AND TABLE_NAME NOT LIKE 'sysrs%'\n AND TABLE_NAME NOT LIKE 'sysxlgc%'\n ),\n index_info AS (\n SELECT\n OBJECT_NAME(i.object_id) AS table_name,\n i.name AS index_name,\n i.is_unique,\n CASE\n WHEN i.type = 1 THEN 1\n ELSE 0\n END AS is_primary,\n i.type_desc AS index_type,\n STUFF((\n SELECT\n ', ' + c.name\n FROM\n sys.index_columns ic\n JOIN sys.columns c ON ic.column_id = c.column_id AND ic.object_id = c.object_id\n WHERE\n ic.index_id = i.index_id\n AND ic.object_id = i.object_id\n AND OBJECT_NAME(ic.object_id) IN (SELECT TABLE_NAME FROM user_tables) -- Filter for user tables\n ORDER BY\n ic.key_ordinal\n FOR XML PATH('')\n ), 1, 2, '') AS columns,\n CASE\n WHEN i.type = 1 THEN 'PRIMARY KEY'\n WHEN i.is_unique = 1 THEN 'UNIQUE'\n ELSE NULL\n END AS constraint_type\n FROM\n sys.indexes i\n JOIN sys.tables t ON i.object_id = t.object_id\n WHERE\n t.is_ms_shipped = 0\n AND i.type <> 0\n )\n SELECT\n ut.TABLE_SCHEMA AS table_schema,\n ut.TABLE_NAME AS table_name,\n ii.index_name,\n ii.is_unique,\n ii.is_primary,\n ii.index_type,\n ii.columns,\n ii.constraint_type\n FROM\n user_tables ut\n LEFT JOIN\n index_info ii ON ut.TABLE_NAME = ii.table_name\n WHERE\n ii.columns IS NOT NULL\n ORDER BY\n ut.TABLE_NAME,\n ii.constraint_type,\n ii.index_name;\n ";
|
|
267
|
+
_context4.next = 3;
|
|
268
|
+
return client.query(indexListSql);
|
|
269
|
+
case 3:
|
|
270
|
+
indexListResult = _context4.sent;
|
|
271
|
+
_indexListResult$reco = indexListResult.recordset.reduce(function (acc, row) {
|
|
272
|
+
var constraint_type = row.constraint_type,
|
|
273
|
+
columns = row.columns;
|
|
274
|
+
if (columns === 'null' || columns.trim() === '') return acc;
|
|
275
|
+
if (constraint_type === 'PRIMARY KEY' || constraint_type === 'UNIQUE') {
|
|
276
|
+
acc.inlineConstraints.push(row);
|
|
277
|
+
} else {
|
|
278
|
+
acc.outOfLineConstraints.push(row);
|
|
279
|
+
}
|
|
280
|
+
return acc;
|
|
281
|
+
}, {
|
|
282
|
+
outOfLineConstraints: [],
|
|
283
|
+
inlineConstraints: []
|
|
284
|
+
}), outOfLineConstraints = _indexListResult$reco.outOfLineConstraints, inlineConstraints = _indexListResult$reco.inlineConstraints;
|
|
285
|
+
indexes = outOfLineConstraints.reduce(function (acc, indexRow) {
|
|
286
|
+
var table_schema = indexRow.table_schema,
|
|
287
|
+
table_name = indexRow.table_name,
|
|
288
|
+
index_name = indexRow.index_name,
|
|
289
|
+
index_type = indexRow.index_type,
|
|
290
|
+
columns = indexRow.columns,
|
|
291
|
+
expressions = indexRow.expressions;
|
|
292
|
+
var indexColumns = columns.split(',').map(function (column) {
|
|
293
|
+
return {
|
|
294
|
+
type: 'column',
|
|
295
|
+
value: column.trim()
|
|
296
|
+
};
|
|
297
|
+
});
|
|
298
|
+
var indexExpressions = expressions ? expressions.split(',').map(function (expression) {
|
|
299
|
+
return {
|
|
300
|
+
type: 'expression',
|
|
301
|
+
value: expression
|
|
302
|
+
};
|
|
303
|
+
}) : [];
|
|
304
|
+
var index = {
|
|
305
|
+
name: index_name,
|
|
306
|
+
type: index_type,
|
|
307
|
+
columns: [].concat(_toConsumableArray(indexColumns), _toConsumableArray(indexExpressions))
|
|
308
|
+
};
|
|
309
|
+
var key = "".concat(table_schema, ".").concat(table_name);
|
|
310
|
+
if (acc[key]) {
|
|
311
|
+
acc[key].push(index);
|
|
312
|
+
} else {
|
|
313
|
+
acc[key] = [index];
|
|
314
|
+
}
|
|
315
|
+
return acc;
|
|
316
|
+
}, {});
|
|
317
|
+
tableConstraints = inlineConstraints.reduce(function (acc, row) {
|
|
318
|
+
var table_schema = row.table_schema,
|
|
319
|
+
table_name = row.table_name,
|
|
320
|
+
columns = row.columns,
|
|
321
|
+
constraint_type = row.constraint_type;
|
|
322
|
+
var key = "".concat(table_schema, ".").concat(table_name);
|
|
323
|
+
if (!acc[key]) acc[key] = {};
|
|
324
|
+
var columnNames = columns.split(',').map(function (column) {
|
|
325
|
+
return column.trim();
|
|
326
|
+
});
|
|
327
|
+
columnNames.forEach(function (columnName) {
|
|
328
|
+
if (!acc[key][columnName]) acc[key][columnName] = {};
|
|
329
|
+
if (constraint_type === 'PRIMARY KEY') {
|
|
330
|
+
acc[key][columnName].pk = true;
|
|
331
|
+
}
|
|
332
|
+
if (constraint_type === 'UNIQUE' && !acc[key][columnName].pk) {
|
|
333
|
+
acc[key][columnName].unique = true;
|
|
334
|
+
}
|
|
335
|
+
});
|
|
336
|
+
return acc;
|
|
337
|
+
}, {});
|
|
338
|
+
return _context4.abrupt("return", {
|
|
339
|
+
indexes: indexes,
|
|
340
|
+
tableConstraints: tableConstraints
|
|
341
|
+
});
|
|
342
|
+
case 8:
|
|
343
|
+
case "end":
|
|
344
|
+
return _context4.stop();
|
|
345
|
+
}
|
|
346
|
+
}, _callee4);
|
|
347
|
+
}));
|
|
348
|
+
return function generateIndexes(_x4) {
|
|
349
|
+
return _ref4.apply(this, arguments);
|
|
350
|
+
};
|
|
351
|
+
}();
|
|
352
|
+
var fetchSchemaJson = /*#__PURE__*/function () {
|
|
353
|
+
var _ref5 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee5(connection) {
|
|
354
|
+
var client, tablesFieldsAndEnumsRes, indexesRes, refsRes, res, _res$, tables, fields, enums, _res$2, indexes, tableConstraints, refs;
|
|
355
|
+
return _regeneratorRuntime().wrap(function _callee5$(_context5) {
|
|
356
|
+
while (1) switch (_context5.prev = _context5.next) {
|
|
357
|
+
case 0:
|
|
358
|
+
_context5.next = 2;
|
|
359
|
+
return getValidatedClient(connection);
|
|
360
|
+
case 2:
|
|
361
|
+
client = _context5.sent;
|
|
362
|
+
tablesFieldsAndEnumsRes = generateTablesFieldsAndEnums(client);
|
|
363
|
+
indexesRes = generateIndexes(client);
|
|
364
|
+
refsRes = generateRefs(client);
|
|
365
|
+
_context5.next = 8;
|
|
366
|
+
return Promise.all([tablesFieldsAndEnumsRes, indexesRes, refsRes]);
|
|
367
|
+
case 8:
|
|
368
|
+
res = _context5.sent;
|
|
369
|
+
client.close();
|
|
370
|
+
_res$ = res[0], tables = _res$.tables, fields = _res$.fields, enums = _res$.enums;
|
|
371
|
+
_res$2 = res[1], indexes = _res$2.indexes, tableConstraints = _res$2.tableConstraints;
|
|
372
|
+
refs = res[2];
|
|
373
|
+
return _context5.abrupt("return", {
|
|
374
|
+
tables: tables,
|
|
375
|
+
fields: fields,
|
|
376
|
+
enums: enums,
|
|
377
|
+
refs: refs,
|
|
378
|
+
indexes: indexes,
|
|
379
|
+
tableConstraints: tableConstraints
|
|
380
|
+
});
|
|
381
|
+
case 14:
|
|
382
|
+
case "end":
|
|
383
|
+
return _context5.stop();
|
|
384
|
+
}
|
|
385
|
+
}, _callee5);
|
|
386
|
+
}));
|
|
387
|
+
return function fetchSchemaJson(_x5) {
|
|
388
|
+
return _ref5.apply(this, arguments);
|
|
389
|
+
};
|
|
390
|
+
}();
|
|
391
|
+
exports.fetchSchemaJson = fetchSchemaJson;
|