dbgate-tools 6.3.0 → 6.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -27,7 +27,10 @@ export declare class DatabaseAnalyser {
27
27
  getDeletedObjects(snapshot: any): any[];
28
28
  feedback(obj: any): void;
29
29
  getModifications(): Promise<any[]>;
30
- analyserQuery(template: any, typeFields: any, replacements?: {}): Promise<import("dbgate-types").QueryResult>;
30
+ analyserQuery(template: any, typeFields: any, replacements?: {}): Promise<import("dbgate-types").QueryResult | {
31
+ rows: any[];
32
+ isError: boolean;
33
+ }>;
31
34
  static createEmptyStructure(): DatabaseInfo;
32
35
  static byTableFilter(table: any): (x: any) => boolean;
33
36
  static extractPrimaryKeys(table: any, pkColumns: any): {
@@ -333,6 +333,7 @@ class DatabaseAnalyser {
333
333
  logger.error((0, stringTools_1.extractErrorLogData)(err, { template }), 'Error running analyser query');
334
334
  return {
335
335
  rows: [],
336
+ isError: true,
336
337
  };
337
338
  }
338
339
  });
@@ -0,0 +1,4 @@
1
+ import { TableInfo } from 'dbgate-types';
2
+ export declare function chooseTopTables(tables: TableInfo[], count: number, tableFilter: string, omitTablesFilter: string): TableInfo[];
3
+ export declare const DIAGRAM_ZOOMS: number[];
4
+ export declare const DIAGRAM_DEFAULT_WATERMARK = "Powered by [dbgate.io](https://dbgate.io)";
@@ -0,0 +1,62 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.DIAGRAM_DEFAULT_WATERMARK = exports.DIAGRAM_ZOOMS = exports.chooseTopTables = void 0;
7
+ const structureTools_1 = require("./structureTools");
8
+ const sortBy_1 = __importDefault(require("lodash/sortBy"));
9
+ const uniq_1 = __importDefault(require("lodash/uniq"));
10
+ const filterName_1 = require("./filterName");
11
+ function tableWeight(table, maxRowcount) {
12
+ var _a, _b;
13
+ let weight = 0;
14
+ const tableDependenciesCount = (0, uniq_1.default)(((_a = table.dependencies) === null || _a === void 0 ? void 0 : _a.map(x => x.pureName)) || []).length;
15
+ const tableFkCount = (0, uniq_1.default)(((_b = table.foreignKeys) === null || _b === void 0 ? void 0 : _b.map(x => x.refTableName)) || []).length;
16
+ if (table.primaryKey)
17
+ weight += 1;
18
+ if (tableFkCount)
19
+ weight += tableFkCount * 1;
20
+ if (maxRowcount && table.tableRowCount) {
21
+ const rowcount = parseInt(table.tableRowCount);
22
+ if (rowcount > 0)
23
+ weight += Math.log(rowcount) * table.columns.length * (tableFkCount || 1) * (tableDependenciesCount || 1);
24
+ }
25
+ else {
26
+ if (table.columns)
27
+ weight += table.columns.length * 2;
28
+ }
29
+ if (table.dependencies)
30
+ weight += tableDependenciesCount * 10;
31
+ if (maxRowcount)
32
+ return weight;
33
+ }
34
+ function chooseTopTables(tables, count, tableFilter, omitTablesFilter) {
35
+ const filteredTables = tables.filter(table => {
36
+ if (tableFilter) {
37
+ if (!(0, filterName_1.filterName)(tableFilter, table === null || table === void 0 ? void 0 : table.pureName))
38
+ return false;
39
+ }
40
+ if (omitTablesFilter) {
41
+ if ((0, filterName_1.filterName)(omitTablesFilter, table === null || table === void 0 ? void 0 : table.pureName))
42
+ return false;
43
+ }
44
+ return true;
45
+ });
46
+ if (!(count > 0)) {
47
+ return filteredTables;
48
+ }
49
+ const dbinfo = {
50
+ tables: filteredTables,
51
+ };
52
+ const extended = (0, structureTools_1.extendDatabaseInfo)(dbinfo);
53
+ const maxRowcount = Math.max(...extended.tables
54
+ .map(x => x.tableRowCount || 0)
55
+ .map(x => parseInt(x))
56
+ .filter(x => x > 0));
57
+ const sorted = (0, sortBy_1.default)((0, sortBy_1.default)(extended.tables, x => `${x.schemaName}.${x.pureName}`), table => -tableWeight(table, maxRowcount));
58
+ return sorted.slice(0, count);
59
+ }
60
+ exports.chooseTopTables = chooseTopTables;
61
+ exports.DIAGRAM_ZOOMS = [0.1, 0.15, 0.2, 0.3, 0.4, 0.5, 0.6, 0.8, 1, 1.25, 1.5, 1.75, 2];
62
+ exports.DIAGRAM_DEFAULT_WATERMARK = 'Powered by [dbgate.io](https://dbgate.io)';
package/lib/filterName.js CHANGED
@@ -6,9 +6,20 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.tokenizeBySearchFilter = exports.filterNameCompoud = exports.filterName = void 0;
7
7
  const compact_1 = __importDefault(require("lodash/compact"));
8
8
  const startCase_1 = __importDefault(require("lodash/startCase"));
9
- // export interface FilterNameDefinition {
10
- // childName: string;
11
- // }
9
+ function parseTokenTree(filter) {
10
+ const factors = filter
11
+ .split(',')
12
+ .map(x => x.trim())
13
+ .filter(x => x.length > 0);
14
+ return {
15
+ factors: factors.map(x => ({
16
+ tokens: x
17
+ .split(' ')
18
+ .map(x => x.trim())
19
+ .filter(x => x.length > 0),
20
+ })),
21
+ };
22
+ }
12
23
  function camelMatch(filter, text) {
13
24
  if (!text)
14
25
  return false;
@@ -28,23 +39,25 @@ function filterName(filter, ...names) {
28
39
  if (!filter)
29
40
  return true;
30
41
  // const camelVariants = [name.replace(/[^A-Z]/g, '')]
31
- const tokens = filter.split(' ').map(x => x.trim());
42
+ const tree = parseTokenTree(filter);
43
+ if (tree.factors.length == 0)
44
+ return true;
32
45
  const namesCompacted = (0, compact_1.default)(names);
33
- for (const token of tokens) {
34
- const found = namesCompacted.find(name => camelMatch(token, name));
35
- if (!found)
36
- return false;
46
+ for (const factor of tree.factors) {
47
+ let factorOk = true;
48
+ for (const token of factor.tokens) {
49
+ const found = namesCompacted.find(name => camelMatch(token, name));
50
+ if (!found)
51
+ factorOk = false;
52
+ }
53
+ if (factorOk) {
54
+ return true;
55
+ }
37
56
  }
38
- return true;
57
+ return false;
39
58
  }
40
59
  exports.filterName = filterName;
41
- function filterNameCompoud(filter, namesMain, namesChild) {
42
- if (!filter)
43
- return 'both';
44
- // const camelVariants = [name.replace(/[^A-Z]/g, '')]
45
- const tokens = filter.split(' ').map(x => x.trim());
46
- const namesCompactedMain = (0, compact_1.default)(namesMain);
47
- const namesCompactedChild = (0, compact_1.default)(namesChild);
60
+ function clasifyCompoudCategory(tokens, namesCompactedMain, namesCompactedChild) {
48
61
  let isMainOnly = true;
49
62
  let isChildOnly = true;
50
63
  for (const token of tokens) {
@@ -65,12 +78,39 @@ function filterNameCompoud(filter, namesMain, namesChild) {
65
78
  return 'child';
66
79
  return 'none';
67
80
  }
81
+ function filterNameCompoud(filter, namesMain, namesChild) {
82
+ if (!filter)
83
+ return 'both';
84
+ // const camelVariants = [name.replace(/[^A-Z]/g, '')]
85
+ const tree = parseTokenTree(filter);
86
+ const namesCompactedMain = (0, compact_1.default)(namesMain);
87
+ const namesCompactedChild = (0, compact_1.default)(namesChild);
88
+ if (tree.factors.length == 0)
89
+ return 'both';
90
+ const factorRes = [];
91
+ for (const factor of tree.factors) {
92
+ const category = clasifyCompoudCategory(factor.tokens, namesCompactedMain, namesCompactedChild);
93
+ factorRes.push(category);
94
+ }
95
+ if (factorRes.includes('both'))
96
+ return 'both';
97
+ if (factorRes.includes('main') && factorRes.includes('child'))
98
+ return 'both';
99
+ if (factorRes.includes('main'))
100
+ return 'main';
101
+ if (factorRes.includes('child'))
102
+ return 'child';
103
+ return 'none';
104
+ }
68
105
  exports.filterNameCompoud = filterNameCompoud;
69
106
  function tokenizeBySearchFilter(text, filter) {
70
107
  var _a, _b;
71
108
  const camelTokens = [];
72
109
  const stdTokens = [];
73
- for (const token of filter.split(' ').map(x => x.trim())) {
110
+ for (const token of filter
111
+ .split(/[ ,]/)
112
+ .map(x => x.trim())
113
+ .filter(x => x.length > 0)) {
74
114
  if (token.replace(/[A-Z]/g, '').length == 0) {
75
115
  camelTokens.push(token);
76
116
  }
@@ -0,0 +1 @@
1
+ declare const tokenizeBySearchFilter: any;
@@ -0,0 +1,18 @@
1
+ const { tokenizeBySearchFilter } = require('./filterName');
2
+ test('tokenize single token', () => {
3
+ const tokenized = tokenizeBySearchFilter('Album', 'al');
4
+ // console.log(JSON.stringify(tokenized, null, 2));
5
+ expect(tokenized).toEqual([
6
+ { text: 'Al', isMatch: true },
7
+ { text: 'bum', isMatch: false },
8
+ ]);
9
+ });
10
+ test('tokenize two tokens', () => {
11
+ const tokenized = tokenizeBySearchFilter('Album', 'al,um');
12
+ // console.log(JSON.stringify(tokenized, null, 2));
13
+ expect(tokenized).toEqual([
14
+ { text: 'Al', isMatch: true },
15
+ { text: 'b', isMatch: false },
16
+ { text: 'um', isMatch: true },
17
+ ]);
18
+ });
package/lib/index.d.ts CHANGED
@@ -26,3 +26,4 @@ export * from './filterBehaviours';
26
26
  export * from './schemaInfoTools';
27
27
  export * from './dbKeysLoader';
28
28
  export * from './rowProgressReporter';
29
+ export * from './diagramTools';
package/lib/index.js CHANGED
@@ -42,3 +42,4 @@ __exportStar(require("./filterBehaviours"), exports);
42
42
  __exportStar(require("./schemaInfoTools"), exports);
43
43
  __exportStar(require("./dbKeysLoader"), exports);
44
44
  __exportStar(require("./rowProgressReporter"), exports);
45
+ __exportStar(require("./diagramTools"), exports);
@@ -7,12 +7,13 @@ exports.skipDbGateInternalObjects = exports.removePreloadedRowsFromStructure = e
7
7
  const flatten_1 = __importDefault(require("lodash/flatten"));
8
8
  const uniq_1 = __importDefault(require("lodash/uniq"));
9
9
  const keys_1 = __importDefault(require("lodash/keys"));
10
+ const compact_1 = __importDefault(require("lodash/compact"));
10
11
  function addTableDependencies(db) {
11
12
  if (!db.tables) {
12
13
  return db;
13
14
  }
14
- const allForeignKeys = (0, flatten_1.default)(db.tables.map(x => x.foreignKeys || []));
15
- return Object.assign(Object.assign({}, db), { tables: db.tables.map(table => (Object.assign(Object.assign({}, table), { dependencies: allForeignKeys.filter(x => x.refSchemaName == table.schemaName && x.refTableName == table.pureName) }))) });
15
+ const allForeignKeys = (0, flatten_1.default)(db.tables.map(x => (x === null || x === void 0 ? void 0 : x.foreignKeys) || []));
16
+ return Object.assign(Object.assign({}, db), { tables: (0, compact_1.default)(db.tables).map(table => (Object.assign(Object.assign({}, table), { dependencies: allForeignKeys.filter(x => x.refSchemaName == table.schemaName && x.refTableName == table.pureName) }))) });
16
17
  }
17
18
  exports.addTableDependencies = addTableDependencies;
18
19
  function extendTableInfo(table) {
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "6.3.0",
2
+ "version": "6.3.3",
3
3
  "name": "dbgate-tools",
4
4
  "main": "lib/index.js",
5
5
  "typings": "lib/index.d.ts",
@@ -17,22 +17,23 @@
17
17
  "scripts": {
18
18
  "build": "tsc",
19
19
  "start": "tsc --watch",
20
+ "prepublishOnly": "yarn build",
20
21
  "test": "jest",
21
- "prepublishOnly": "yarn build"
22
+ "test:ci": "jest --json --outputFile=result.json --testLocationInResults"
22
23
  },
23
24
  "files": [
24
25
  "lib"
25
26
  ],
26
27
  "devDependencies": {
27
28
  "@types/node": "^13.7.0",
28
- "dbgate-types": "^6.3.0",
29
- "jest": "^24.9.0",
30
- "ts-jest": "^25.2.1",
29
+ "dbgate-types": "^6.3.3",
30
+ "jest": "^28.1.3",
31
+ "ts-jest": "^28.0.7",
31
32
  "typescript": "^4.4.3"
32
33
  },
33
34
  "dependencies": {
34
35
  "dbgate-query-splitter": "^4.11.3",
35
- "dbgate-sqltree": "^6.3.0",
36
+ "dbgate-sqltree": "^6.3.3",
36
37
  "debug": "^4.3.4",
37
38
  "json-stable-stringify": "^1.0.1",
38
39
  "lodash": "^4.17.21",