@dbml/core 3.7.0 → 3.7.2
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/import/index.js +4 -28
- package/lib/model_structure/tableGroup.js +9 -2
- package/lib/parse/Parser.js +27 -23
- package/lib/parse/dbml/parser.pegjs +2 -0
- package/package.json +6 -6
- package/types/import/index.d.ts +1 -1
- package/types/model_structure/tableGroup.d.ts +17 -7
- package/types/parse/Parser.d.ts +1 -1
- package/lib/connectors/ utils.js +0 -11
- package/lib/connectors/connector.js +0 -40
- package/lib/connectors/dbml.js +0 -170
- package/lib/connectors/mssqlConnector.js +0 -391
- package/lib/connectors/mysqlConnector.js +0 -490
- package/lib/connectors/postgresConnector.js +0 -400
- /package/lib/parse/{DatabaseGenerator.js → databaseGenerator.js} +0 -0
|
@@ -1,400 +0,0 @@
|
|
|
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 _pg = require("pg");
|
|
9
|
-
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
|
|
10
|
-
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."); }
|
|
11
|
-
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); }
|
|
12
|
-
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
|
|
13
|
-
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
|
|
14
|
-
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; }
|
|
15
|
-
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; }
|
|
16
|
-
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); } }
|
|
17
|
-
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 */
|
|
18
|
-
var getValidatedClient = /*#__PURE__*/function () {
|
|
19
|
-
var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(connection) {
|
|
20
|
-
var client;
|
|
21
|
-
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
22
|
-
while (1) switch (_context.prev = _context.next) {
|
|
23
|
-
case 0:
|
|
24
|
-
client = new _pg.Client(connection);
|
|
25
|
-
_context.prev = 1;
|
|
26
|
-
_context.next = 4;
|
|
27
|
-
return client.connect();
|
|
28
|
-
case 4:
|
|
29
|
-
_context.next = 6;
|
|
30
|
-
return client.query('SELECT 1');
|
|
31
|
-
case 6:
|
|
32
|
-
return _context.abrupt("return", client);
|
|
33
|
-
case 9:
|
|
34
|
-
_context.prev = 9;
|
|
35
|
-
_context.t0 = _context["catch"](1);
|
|
36
|
-
// Log the error and handle it as per your application's requirement
|
|
37
|
-
console.error('PostgreSQL connection error:', _context.t0);
|
|
38
|
-
|
|
39
|
-
// Ensure to close the client in case of failure
|
|
40
|
-
_context.next = 14;
|
|
41
|
-
return client.end();
|
|
42
|
-
case 14:
|
|
43
|
-
throw _context.t0;
|
|
44
|
-
case 15:
|
|
45
|
-
case "end":
|
|
46
|
-
return _context.stop();
|
|
47
|
-
}
|
|
48
|
-
}, _callee, null, [[1, 9]]);
|
|
49
|
-
}));
|
|
50
|
-
return function getValidatedClient(_x) {
|
|
51
|
-
return _ref.apply(this, arguments);
|
|
52
|
-
};
|
|
53
|
-
}();
|
|
54
|
-
var convertQueryBoolean = function convertQueryBoolean(val) {
|
|
55
|
-
return val === 'YES';
|
|
56
|
-
};
|
|
57
|
-
var getFieldType = function getFieldType(data_type, udt_name, character_maximum_length, numeric_precision, numeric_scale) {
|
|
58
|
-
if (data_type === 'ARRAY') {
|
|
59
|
-
return "".concat(udt_name.slice(1, udt_name.length), "[]");
|
|
60
|
-
}
|
|
61
|
-
if (character_maximum_length) {
|
|
62
|
-
return "".concat(udt_name, "(").concat(character_maximum_length, ")");
|
|
63
|
-
}
|
|
64
|
-
if (numeric_precision && numeric_scale) {
|
|
65
|
-
return "".concat(udt_name, "(").concat(numeric_precision, ",").concat(numeric_scale, ")");
|
|
66
|
-
}
|
|
67
|
-
return udt_name;
|
|
68
|
-
};
|
|
69
|
-
var getDbdefault = function getDbdefault(data_type, column_default, default_type) {
|
|
70
|
-
if (data_type === 'ARRAY') {
|
|
71
|
-
var values = column_default.slice(6, -1).split(',').map(function (value) {
|
|
72
|
-
return value.split('::')[0];
|
|
73
|
-
});
|
|
74
|
-
return {
|
|
75
|
-
type: default_type,
|
|
76
|
-
value: "ARRAY[".concat(values.join(', '), "]")
|
|
77
|
-
};
|
|
78
|
-
}
|
|
79
|
-
if (default_type === 'string') {
|
|
80
|
-
var defaultValues = column_default.split('::')[0];
|
|
81
|
-
var isJson = data_type === 'json' || data_type === 'jsonb';
|
|
82
|
-
var type = isJson ? 'expression' : 'string';
|
|
83
|
-
return {
|
|
84
|
-
type: type,
|
|
85
|
-
value: defaultValues.slice(1, -1)
|
|
86
|
-
};
|
|
87
|
-
}
|
|
88
|
-
return {
|
|
89
|
-
type: default_type,
|
|
90
|
-
value: column_default
|
|
91
|
-
};
|
|
92
|
-
};
|
|
93
|
-
var generateField = function generateField(row) {
|
|
94
|
-
var column_name = row.column_name,
|
|
95
|
-
data_type = row.data_type,
|
|
96
|
-
character_maximum_length = row.character_maximum_length,
|
|
97
|
-
numeric_precision = row.numeric_precision,
|
|
98
|
-
numeric_scale = row.numeric_scale,
|
|
99
|
-
udt_schema = row.udt_schema,
|
|
100
|
-
udt_name = row.udt_name,
|
|
101
|
-
identity_increment = row.identity_increment,
|
|
102
|
-
is_nullable = row.is_nullable,
|
|
103
|
-
column_default = row.column_default,
|
|
104
|
-
default_type = row.default_type,
|
|
105
|
-
column_comment = row.column_comment;
|
|
106
|
-
var dbdefault = column_default && default_type !== 'increment' ? getDbdefault(data_type, column_default, default_type) : null;
|
|
107
|
-
var fieldType = data_type === 'USER-DEFINED' ? {
|
|
108
|
-
type_name: udt_name,
|
|
109
|
-
schemaName: udt_schema
|
|
110
|
-
} : {
|
|
111
|
-
type_name: getFieldType(data_type, udt_name, character_maximum_length, numeric_precision, numeric_scale),
|
|
112
|
-
schemaname: null
|
|
113
|
-
};
|
|
114
|
-
return {
|
|
115
|
-
name: column_name,
|
|
116
|
-
type: fieldType,
|
|
117
|
-
dbdefault: dbdefault,
|
|
118
|
-
not_null: !convertQueryBoolean(is_nullable),
|
|
119
|
-
increment: !!identity_increment || default_type === 'increment',
|
|
120
|
-
note: column_comment ? {
|
|
121
|
-
value: column_comment
|
|
122
|
-
} : {
|
|
123
|
-
value: ''
|
|
124
|
-
}
|
|
125
|
-
};
|
|
126
|
-
};
|
|
127
|
-
var generateTablesAndFields = /*#__PURE__*/function () {
|
|
128
|
-
var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(client) {
|
|
129
|
-
var fields, tablesAndFieldsSql, tablesAndFieldsResult, tables;
|
|
130
|
-
return _regeneratorRuntime().wrap(function _callee2$(_context2) {
|
|
131
|
-
while (1) switch (_context2.prev = _context2.next) {
|
|
132
|
-
case 0:
|
|
133
|
-
fields = {};
|
|
134
|
-
tablesAndFieldsSql = "\n WITH comments AS (\n SELECT\n pc.relname AS table_name,\n pn.nspname AS table_schema,\n pa.attname AS column_name,\n pd.description\n FROM\n pg_description pd\n JOIN\n pg_class pc ON pd.objoid = pc.oid\n JOIN\n pg_namespace pn ON pc.relnamespace = pn.oid\n LEFT JOIN\n pg_attribute pa ON pd.objoid = pa.attrelid AND pd.objsubid = pa.attnum\n WHERE\n pc.relkind = 'r'\n AND pn.nspname NOT IN ('pg_catalog', 'information_schema')\n )\n SELECT\n t.table_schema,\n t.table_name,\n c.column_name,\n c.data_type,\n c.character_maximum_length,\n c.numeric_precision,\n c.numeric_scale,\n c.udt_schema,\n c.udt_name,\n c.identity_increment,\n c.is_nullable,\n c.column_default,\n CASE\n WHEN c.column_default IS NULL THEN NULL\n WHEN c.column_default LIKE 'nextval(%' THEN 'increment'\n WHEN c.column_default LIKE '''%' THEN 'string'\n WHEN c.column_default = 'true' OR c.column_default = 'false' THEN 'boolean'\n WHEN c.column_default ~ '^-?[0-9]+(.[0-9]+)?$' THEN 'number'\n ELSE 'expression'\n END AS default_type,\n (SELECT description FROM comments WHERE table_name = t.table_name AND table_schema = t.table_schema AND column_name IS NULL) AS table_comment,\n (SELECT description FROM comments WHERE table_name = t.table_name AND table_schema = t.table_schema AND column_name = c.column_name) AS column_comment\n FROM\n information_schema.columns c\n JOIN\n information_schema.tables t ON c.table_name = t.table_name AND c.table_schema = t.table_schema\n WHERE\n t.table_type = 'BASE TABLE'\n AND t.table_schema NOT IN ('pg_catalog', 'information_schema')\n ORDER BY\n t.table_schema,\n t.table_name,\n c.ordinal_position\n ;\n ";
|
|
135
|
-
_context2.next = 4;
|
|
136
|
-
return client.query(tablesAndFieldsSql);
|
|
137
|
-
case 4:
|
|
138
|
-
tablesAndFieldsResult = _context2.sent;
|
|
139
|
-
tables = tablesAndFieldsResult.rows.reduce(function (acc, row) {
|
|
140
|
-
var table_schema = row.table_schema,
|
|
141
|
-
table_name = row.table_name,
|
|
142
|
-
table_comment = row.table_comment;
|
|
143
|
-
var key = "".concat(table_schema, ".").concat(table_name);
|
|
144
|
-
if (!acc[key]) {
|
|
145
|
-
acc[key] = {
|
|
146
|
-
name: table_name,
|
|
147
|
-
schemaName: table_schema,
|
|
148
|
-
note: table_comment ? {
|
|
149
|
-
value: table_comment
|
|
150
|
-
} : {
|
|
151
|
-
value: ''
|
|
152
|
-
}
|
|
153
|
-
};
|
|
154
|
-
}
|
|
155
|
-
if (!fields[key]) fields[key] = [];
|
|
156
|
-
var field = generateField(row);
|
|
157
|
-
fields[key].push(field);
|
|
158
|
-
return acc;
|
|
159
|
-
}, {});
|
|
160
|
-
return _context2.abrupt("return", {
|
|
161
|
-
tables: Object.values(tables),
|
|
162
|
-
fields: fields
|
|
163
|
-
});
|
|
164
|
-
case 7:
|
|
165
|
-
case "end":
|
|
166
|
-
return _context2.stop();
|
|
167
|
-
}
|
|
168
|
-
}, _callee2);
|
|
169
|
-
}));
|
|
170
|
-
return function generateTablesAndFields(_x2) {
|
|
171
|
-
return _ref2.apply(this, arguments);
|
|
172
|
-
};
|
|
173
|
-
}();
|
|
174
|
-
var generateRawRefs = /*#__PURE__*/function () {
|
|
175
|
-
var _ref3 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3(client) {
|
|
176
|
-
var refs, refsListSql, refsQueryResult;
|
|
177
|
-
return _regeneratorRuntime().wrap(function _callee3$(_context3) {
|
|
178
|
-
while (1) switch (_context3.prev = _context3.next) {
|
|
179
|
-
case 0:
|
|
180
|
-
refs = [];
|
|
181
|
-
refsListSql = "\n SELECT\n tc.table_schema,\n tc.table_name,\n tc.constraint_name as fk_constraint_name,\n STRING_AGG(DISTINCT kcu.column_name, ',') AS column_names,\n ccu.table_schema AS foreign_table_schema,\n ccu.table_name AS foreign_table_name,\n STRING_AGG(DISTINCT ccu.column_name, ',') AS foreign_column_names,\n tc.constraint_type,\n rc.delete_rule AS on_delete,\n rc.update_rule AS on_update\n FROM information_schema.table_constraints AS tc\n JOIN information_schema.key_column_usage AS kcu\n ON tc.constraint_name = kcu.constraint_name\n AND tc.table_schema = kcu.table_schema\n JOIN information_schema.constraint_column_usage AS ccu\n ON ccu.constraint_name = tc.constraint_name\n JOIN information_schema.referential_constraints AS rc\n ON tc.constraint_name = rc.constraint_name\n AND tc.table_schema = rc.constraint_schema\n WHERE tc.constraint_type = 'FOREIGN KEY'\n AND tc.table_schema NOT IN ('pg_catalog', 'information_schema')\n GROUP BY\n tc.table_schema,\n tc.table_name,\n tc.constraint_name,\n ccu.table_schema,\n ccu.table_name,\n tc.constraint_type,\n rc.delete_rule,\n rc.update_rule\n ORDER BY\n tc.table_schema,\n tc.table_name;\n ";
|
|
182
|
-
_context3.next = 4;
|
|
183
|
-
return client.query(refsListSql);
|
|
184
|
-
case 4:
|
|
185
|
-
refsQueryResult = _context3.sent;
|
|
186
|
-
refsQueryResult.rows.forEach(function (refRow) {
|
|
187
|
-
var table_schema = refRow.table_schema,
|
|
188
|
-
fk_constraint_name = refRow.fk_constraint_name,
|
|
189
|
-
table_name = refRow.table_name,
|
|
190
|
-
column_names = refRow.column_names,
|
|
191
|
-
foreign_table_schema = refRow.foreign_table_schema,
|
|
192
|
-
foreign_table_name = refRow.foreign_table_name,
|
|
193
|
-
foreign_column_names = refRow.foreign_column_names,
|
|
194
|
-
on_delete = refRow.on_delete,
|
|
195
|
-
on_update = refRow.on_update;
|
|
196
|
-
var ep1 = {
|
|
197
|
-
tableName: table_name,
|
|
198
|
-
schemaName: table_schema,
|
|
199
|
-
fieldNames: column_names.split(','),
|
|
200
|
-
relation: '*'
|
|
201
|
-
};
|
|
202
|
-
var ep2 = {
|
|
203
|
-
tableName: foreign_table_name,
|
|
204
|
-
schemaName: foreign_table_schema,
|
|
205
|
-
fieldNames: foreign_column_names.split(','),
|
|
206
|
-
relation: '1'
|
|
207
|
-
};
|
|
208
|
-
refs.push({
|
|
209
|
-
name: fk_constraint_name,
|
|
210
|
-
endpoints: [ep1, ep2],
|
|
211
|
-
onDelete: on_delete === 'NO ACTION' ? null : on_delete,
|
|
212
|
-
onUpdate: on_update === 'NO ACTION' ? null : on_update
|
|
213
|
-
});
|
|
214
|
-
});
|
|
215
|
-
return _context3.abrupt("return", refs);
|
|
216
|
-
case 7:
|
|
217
|
-
case "end":
|
|
218
|
-
return _context3.stop();
|
|
219
|
-
}
|
|
220
|
-
}, _callee3);
|
|
221
|
-
}));
|
|
222
|
-
return function generateRawRefs(_x3) {
|
|
223
|
-
return _ref3.apply(this, arguments);
|
|
224
|
-
};
|
|
225
|
-
}();
|
|
226
|
-
var generateIndexes = /*#__PURE__*/function () {
|
|
227
|
-
var _ref4 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee4(client) {
|
|
228
|
-
var indexListSql, indexListResult, _indexListResult$rows, outOfLineConstraints, inlineConstraints, indexes, tableConstraints;
|
|
229
|
-
return _regeneratorRuntime().wrap(function _callee4$(_context4) {
|
|
230
|
-
while (1) switch (_context4.prev = _context4.next) {
|
|
231
|
-
case 0:
|
|
232
|
-
// const tableConstraints = {};
|
|
233
|
-
indexListSql = "\n WITH user_tables AS (\n SELECT\n schemaname AS tableschema,\n tablename\n FROM pg_tables\n WHERE schemaname NOT IN ('pg_catalog', 'information_schema') -- Exclude system schemas\n AND tablename NOT LIKE 'pg_%' -- Exclude PostgreSQL system tables\n AND tablename NOT LIKE 'sql_%' -- Exclude SQL standard tables\n ),\n index_info AS (\n SELECT\n t.relname AS table_name,\n i.relname AS index_name,\n ix.indisunique AS is_unique,\n ix.indisprimary AS is_primary,\n am.amname AS index_type,\n array_to_string(array_agg(a.attname ORDER BY x.n), ', ') AS columns,\n pg_get_expr(ix.indexprs, ix.indrelid) AS expressions,\n CASE\n WHEN ix.indisprimary THEN 'PRIMARY KEY'\n WHEN ix.indisunique THEN 'UNIQUE'\n ELSE NULL\n END AS constraint_type\n FROM\n pg_class t\n JOIN pg_index ix ON t.oid = ix.indrelid\n JOIN pg_class i ON i.oid = ix.indexrelid\n LEFT JOIN pg_attribute a ON a.attrelid = t.oid AND a.attnum = ANY(ix.indkey)\n JOIN pg_am am ON i.relam = am.oid\n LEFT JOIN generate_subscripts(ix.indkey, 1) AS x(n) ON a.attnum = ix.indkey[x.n]\n WHERE\n t.relkind = 'r'\n AND t.relname NOT LIKE 'pg_%'\n AND t.relname NOT LIKE 'sql_%'\n GROUP BY\n t.relname, i.relname, ix.indisunique, ix.indisprimary, am.amname, ix.indexprs, ix.indrelid\n )\n SELECT\n ut.tableschema AS table_schema,\n ut.tablename AS table_name,\n ii.index_name,\n ii.is_unique,\n ii.is_primary,\n ii.index_type,\n ii.columns,\n ii.expressions,\n ii.constraint_type -- Added constraint type\n FROM\n user_tables ut\n LEFT JOIN\n index_info ii ON ut.tablename = ii.table_name\n WHERE ii.columns IS NOT NULL\n ORDER BY\n ut.tablename,\n ii.constraint_type,\n ii.index_name\n ;\n ";
|
|
234
|
-
_context4.next = 3;
|
|
235
|
-
return client.query(indexListSql);
|
|
236
|
-
case 3:
|
|
237
|
-
indexListResult = _context4.sent;
|
|
238
|
-
_indexListResult$rows = indexListResult.rows.reduce(function (acc, row) {
|
|
239
|
-
var constraint_type = row.constraint_type,
|
|
240
|
-
columns = row.columns;
|
|
241
|
-
if (columns === 'null' || columns.trim() === '') return acc;
|
|
242
|
-
var isSingleColumn = columns.split(',').length === 1;
|
|
243
|
-
var isInlineConstraint = isSingleColumn && (constraint_type === 'PRIMARY KEY' || constraint_type === 'UNIQUE');
|
|
244
|
-
if (isInlineConstraint) {
|
|
245
|
-
acc.inlineConstraints.push(row);
|
|
246
|
-
} else {
|
|
247
|
-
acc.outOfLineConstraints.push(row);
|
|
248
|
-
}
|
|
249
|
-
return acc;
|
|
250
|
-
}, {
|
|
251
|
-
outOfLineConstraints: [],
|
|
252
|
-
inlineConstraints: []
|
|
253
|
-
}), outOfLineConstraints = _indexListResult$rows.outOfLineConstraints, inlineConstraints = _indexListResult$rows.inlineConstraints;
|
|
254
|
-
indexes = outOfLineConstraints.reduce(function (acc, indexRow) {
|
|
255
|
-
var table_schema = indexRow.table_schema,
|
|
256
|
-
table_name = indexRow.table_name,
|
|
257
|
-
index_name = indexRow.index_name,
|
|
258
|
-
index_type = indexRow.index_type,
|
|
259
|
-
columns = indexRow.columns,
|
|
260
|
-
expressions = indexRow.expressions;
|
|
261
|
-
var indexColumns = columns.split(',').map(function (column) {
|
|
262
|
-
return {
|
|
263
|
-
type: 'column',
|
|
264
|
-
value: column.trim()
|
|
265
|
-
};
|
|
266
|
-
});
|
|
267
|
-
var indexExpressions = expressions ? expressions.split(',').map(function (expression) {
|
|
268
|
-
return {
|
|
269
|
-
type: 'expression',
|
|
270
|
-
value: expression
|
|
271
|
-
};
|
|
272
|
-
}) : [];
|
|
273
|
-
var index = {
|
|
274
|
-
name: index_name,
|
|
275
|
-
type: index_type,
|
|
276
|
-
columns: [].concat(_toConsumableArray(indexColumns), _toConsumableArray(indexExpressions))
|
|
277
|
-
};
|
|
278
|
-
var key = "".concat(table_schema, ".").concat(table_name);
|
|
279
|
-
if (acc[key]) {
|
|
280
|
-
acc[key].push(index);
|
|
281
|
-
} else {
|
|
282
|
-
acc[key] = [index];
|
|
283
|
-
}
|
|
284
|
-
return acc;
|
|
285
|
-
}, {});
|
|
286
|
-
tableConstraints = inlineConstraints.reduce(function (acc, row) {
|
|
287
|
-
var table_schema = row.table_schema,
|
|
288
|
-
table_name = row.table_name,
|
|
289
|
-
columns = row.columns,
|
|
290
|
-
constraint_type = row.constraint_type;
|
|
291
|
-
var key = "".concat(table_schema, ".").concat(table_name);
|
|
292
|
-
if (!acc[key]) acc[key] = {};
|
|
293
|
-
var columnNames = columns.split(',').map(function (column) {
|
|
294
|
-
return column.trim();
|
|
295
|
-
});
|
|
296
|
-
columnNames.forEach(function (columnName) {
|
|
297
|
-
if (!acc[key][columnName]) acc[key][columnName] = {};
|
|
298
|
-
if (constraint_type === 'PRIMARY KEY') {
|
|
299
|
-
acc[key][columnName].pk = true;
|
|
300
|
-
}
|
|
301
|
-
if (constraint_type === 'UNIQUE' && !acc[key][columnName].pk) {
|
|
302
|
-
acc[key][columnName].unique = true;
|
|
303
|
-
}
|
|
304
|
-
});
|
|
305
|
-
return acc;
|
|
306
|
-
}, {});
|
|
307
|
-
return _context4.abrupt("return", {
|
|
308
|
-
indexes: indexes,
|
|
309
|
-
tableConstraints: tableConstraints
|
|
310
|
-
});
|
|
311
|
-
case 8:
|
|
312
|
-
case "end":
|
|
313
|
-
return _context4.stop();
|
|
314
|
-
}
|
|
315
|
-
}, _callee4);
|
|
316
|
-
}));
|
|
317
|
-
return function generateIndexes(_x4) {
|
|
318
|
-
return _ref4.apply(this, arguments);
|
|
319
|
-
};
|
|
320
|
-
}();
|
|
321
|
-
var generateRawEnums = /*#__PURE__*/function () {
|
|
322
|
-
var _ref5 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee5(client) {
|
|
323
|
-
var enumListSql, enumListResult, enums;
|
|
324
|
-
return _regeneratorRuntime().wrap(function _callee5$(_context5) {
|
|
325
|
-
while (1) switch (_context5.prev = _context5.next) {
|
|
326
|
-
case 0:
|
|
327
|
-
enumListSql = "\n SELECT\n n.nspname AS schema_name,\n t.typname AS enum_type,\n e.enumlabel AS enum_value,\n e.enumsortorder AS sort_order\n FROM\n pg_enum e\n JOIN\n pg_type t ON e.enumtypid = t.oid\n JOIN\n pg_namespace n ON t.typnamespace = n.oid\n ORDER BY\n schema_name,\n enum_type,\n sort_order;\n ;\n ";
|
|
328
|
-
_context5.next = 3;
|
|
329
|
-
return client.query(enumListSql);
|
|
330
|
-
case 3:
|
|
331
|
-
enumListResult = _context5.sent;
|
|
332
|
-
enums = enumListResult.rows.reduce(function (acc, row) {
|
|
333
|
-
var schema_name = row.schema_name,
|
|
334
|
-
enum_type = row.enum_type,
|
|
335
|
-
enum_value = row.enum_value;
|
|
336
|
-
if (!acc[enum_type]) {
|
|
337
|
-
acc[enum_type] = {
|
|
338
|
-
name: enum_type,
|
|
339
|
-
schemaName: schema_name,
|
|
340
|
-
values: []
|
|
341
|
-
};
|
|
342
|
-
}
|
|
343
|
-
acc[enum_type].values.push({
|
|
344
|
-
name: enum_value
|
|
345
|
-
});
|
|
346
|
-
return acc;
|
|
347
|
-
}, {});
|
|
348
|
-
return _context5.abrupt("return", Object.values(enums));
|
|
349
|
-
case 6:
|
|
350
|
-
case "end":
|
|
351
|
-
return _context5.stop();
|
|
352
|
-
}
|
|
353
|
-
}, _callee5);
|
|
354
|
-
}));
|
|
355
|
-
return function generateRawEnums(_x5) {
|
|
356
|
-
return _ref5.apply(this, arguments);
|
|
357
|
-
};
|
|
358
|
-
}();
|
|
359
|
-
var fetchSchemaJson = /*#__PURE__*/function () {
|
|
360
|
-
var _ref6 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee6(connection) {
|
|
361
|
-
var client, tablesAndFieldsRes, indexesRes, refsRes, enumsRes, res, _res$, tables, fields, _res$2, indexes, tableConstraints, refs, enums;
|
|
362
|
-
return _regeneratorRuntime().wrap(function _callee6$(_context6) {
|
|
363
|
-
while (1) switch (_context6.prev = _context6.next) {
|
|
364
|
-
case 0:
|
|
365
|
-
_context6.next = 2;
|
|
366
|
-
return getValidatedClient(connection);
|
|
367
|
-
case 2:
|
|
368
|
-
client = _context6.sent;
|
|
369
|
-
tablesAndFieldsRes = generateTablesAndFields(client);
|
|
370
|
-
indexesRes = generateIndexes(client);
|
|
371
|
-
refsRes = generateRawRefs(client);
|
|
372
|
-
enumsRes = generateRawEnums(client);
|
|
373
|
-
_context6.next = 9;
|
|
374
|
-
return Promise.all([tablesAndFieldsRes, indexesRes, refsRes, enumsRes]);
|
|
375
|
-
case 9:
|
|
376
|
-
res = _context6.sent;
|
|
377
|
-
client.end();
|
|
378
|
-
_res$ = res[0], tables = _res$.tables, fields = _res$.fields;
|
|
379
|
-
_res$2 = res[1], indexes = _res$2.indexes, tableConstraints = _res$2.tableConstraints;
|
|
380
|
-
refs = res[2];
|
|
381
|
-
enums = res[3];
|
|
382
|
-
return _context6.abrupt("return", {
|
|
383
|
-
tables: tables,
|
|
384
|
-
fields: fields,
|
|
385
|
-
refs: refs,
|
|
386
|
-
enums: enums,
|
|
387
|
-
indexes: indexes,
|
|
388
|
-
tableConstraints: tableConstraints
|
|
389
|
-
});
|
|
390
|
-
case 16:
|
|
391
|
-
case "end":
|
|
392
|
-
return _context6.stop();
|
|
393
|
-
}
|
|
394
|
-
}, _callee6);
|
|
395
|
-
}));
|
|
396
|
-
return function fetchSchemaJson(_x6) {
|
|
397
|
-
return _ref6.apply(this, arguments);
|
|
398
|
-
};
|
|
399
|
-
}();
|
|
400
|
-
exports.fetchSchemaJson = fetchSchemaJson;
|
|
File without changes
|