prostgles-server 4.2.611 → 4.2.613

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.
Files changed (68) hide show
  1. package/dist/Audit/AuditTable.d.ts +12 -1
  2. package/dist/Audit/AuditTable.d.ts.map +1 -1
  3. package/dist/Audit/AuditTable.js +12 -1
  4. package/dist/Audit/AuditTable.js.map +1 -1
  5. package/dist/Audit/AuditTypes.d.ts +1 -2
  6. package/dist/Audit/AuditTypes.d.ts.map +1 -1
  7. package/dist/Audit/getAuditTriggerConfig.d.ts.map +1 -1
  8. package/dist/Audit/getAuditTriggerConfig.js +12 -4
  9. package/dist/Audit/getAuditTriggerConfig.js.map +1 -1
  10. package/dist/Audit/parseAuditConfig.d.ts.map +1 -1
  11. package/dist/Audit/parseAuditConfig.js +0 -4
  12. package/dist/Audit/parseAuditConfig.js.map +1 -1
  13. package/dist/DboBuilder/DboBuilder.d.ts +1 -1
  14. package/dist/DboBuilder/DboBuilder.d.ts.map +1 -1
  15. package/dist/DboBuilder/DboBuilder.js +1 -1
  16. package/dist/DboBuilder/DboBuilder.js.map +1 -1
  17. package/dist/DboBuilder/ViewHandler/ViewHandler.d.ts +1 -1
  18. package/dist/DboBuilder/ViewHandler/ViewHandler.d.ts.map +1 -1
  19. package/dist/DboBuilder/joins/getInferredJoins.d.ts +4 -0
  20. package/dist/DboBuilder/joins/getInferredJoins.d.ts.map +1 -0
  21. package/dist/DboBuilder/joins/getInferredJoins.js +55 -0
  22. package/dist/DboBuilder/joins/getInferredJoins.js.map +1 -0
  23. package/dist/DboBuilder/joins/prepareShortestJoinPaths.d.ts +12 -0
  24. package/dist/DboBuilder/joins/prepareShortestJoinPaths.d.ts.map +1 -0
  25. package/dist/DboBuilder/joins/prepareShortestJoinPaths.js +165 -0
  26. package/dist/DboBuilder/joins/prepareShortestJoinPaths.js.map +1 -0
  27. package/dist/DboBuilder/joins/prepareShortestJoinPaths.spec.d.ts.map +1 -0
  28. package/dist/DboBuilder/joins/prepareShortestJoinPaths.spec.js +67 -0
  29. package/dist/DboBuilder/joins/prepareShortestJoinPaths.spec.js.map +1 -0
  30. package/dist/DboBuilder/joins/shortestPath.d.ts.map +1 -0
  31. package/dist/{shortestPath.js → DboBuilder/joins/shortestPath.js} +11 -11
  32. package/dist/DboBuilder/joins/shortestPath.js.map +1 -0
  33. package/dist/ProstglesTypes.d.ts +4 -3
  34. package/dist/ProstglesTypes.d.ts.map +1 -1
  35. package/dist/TableConfig/TableRowFromColumnDefinitions.d.ts +8 -0
  36. package/dist/TableConfig/TableRowFromColumnDefinitions.d.ts.map +1 -1
  37. package/dist/utils/utils.d.ts +1 -0
  38. package/dist/utils/utils.d.ts.map +1 -1
  39. package/dist/utils/utils.js +8 -3
  40. package/dist/utils/utils.js.map +1 -1
  41. package/lib/Audit/AuditTable.ts +12 -1
  42. package/lib/Audit/AuditTypes.ts +1 -2
  43. package/lib/Audit/getAuditTriggerConfig.ts +12 -4
  44. package/lib/Audit/parseAuditConfig.ts +0 -4
  45. package/lib/DboBuilder/DboBuilder.ts +13 -10
  46. package/lib/DboBuilder/ViewHandler/ViewHandler.ts +1 -1
  47. package/lib/DboBuilder/joins/getInferredJoins.ts +59 -0
  48. package/lib/DboBuilder/joins/prepareShortestJoinPaths.spec.ts +72 -0
  49. package/lib/DboBuilder/joins/prepareShortestJoinPaths.ts +205 -0
  50. package/lib/{shortestPath.ts → DboBuilder/joins/shortestPath.ts} +14 -13
  51. package/lib/ProstglesTypes.ts +4 -3
  52. package/lib/TableConfig/TableRowFromColumnDefinitions.ts +18 -12
  53. package/lib/utils/utils.ts +6 -0
  54. package/package.json +1 -1
  55. package/tsconfig.json +1 -1
  56. package/dist/DboBuilder/prepareShortestJoinPaths.d.ts +0 -12
  57. package/dist/DboBuilder/prepareShortestJoinPaths.d.ts.map +0 -1
  58. package/dist/DboBuilder/prepareShortestJoinPaths.js +0 -186
  59. package/dist/DboBuilder/prepareShortestJoinPaths.js.map +0 -1
  60. package/dist/DboBuilder/prepareShortestJoinPaths.spec.d.ts.map +0 -1
  61. package/dist/DboBuilder/prepareShortestJoinPaths.spec.js +0 -30
  62. package/dist/DboBuilder/prepareShortestJoinPaths.spec.js.map +0 -1
  63. package/dist/shortestPath.d.ts.map +0 -1
  64. package/dist/shortestPath.js.map +0 -1
  65. package/lib/DboBuilder/prepareShortestJoinPaths.spec.ts +0 -29
  66. package/lib/DboBuilder/prepareShortestJoinPaths.ts +0 -225
  67. /package/dist/DboBuilder/{prepareShortestJoinPaths.spec.d.ts → joins/prepareShortestJoinPaths.spec.d.ts} +0 -0
  68. /package/dist/{shortestPath.d.ts → DboBuilder/joins/shortestPath.d.ts} +0 -0
@@ -1,225 +0,0 @@
1
- import type { DboBuilder } from "../DboBuilder/DboBuilder";
2
- import type { Join } from "../ProstglesTypes";
3
- import { JOIN_TYPES } from "../ProstglesTypes";
4
- import type { Graph} from "../shortestPath";
5
- import { findShortestPath } from "../shortestPath";
6
- import type { TableSchema } from "./DboBuilderTypes";
7
- import type { JoinPaths } from "./ViewHandler/ViewHandler";
8
-
9
- type Result = {
10
- joinGraph?: Graph | undefined;
11
- joins: Join[];
12
- shortestJoinPaths: JoinPaths;
13
- };
14
- export async function prepareShortestJoinPaths(dboBuilder: DboBuilder): Promise<Result> {
15
- if (dboBuilder.prostgles.opts.joins) {
16
- let joinConfig = await dboBuilder.prostgles.opts.joins;
17
- if (!dboBuilder.tablesOrViews) {
18
- throw new Error("Could not create join config. this.tablesOrViews missing");
19
- }
20
-
21
- // The initial DBO is needed by tableConfig before its tables/columns exist.
22
- // Defer only joins that cannot be resolved yet; the final rebuild validates all.
23
- if (Array.isArray(joinConfig) && dboBuilder.prostgles.preparingTableConfig) {
24
- const tables = dboBuilder.tablesOrViews;
25
- joinConfig = joinConfig.filter((join) => join.tables.every((name, index) => {
26
- const table = tables.find((candidate) => candidate.name === name);
27
- return table && join.on.every((on) =>
28
- (index === 0 ? Object.keys(on) : Object.values(on)).every((column) =>
29
- table.columns.some((candidate) => candidate.name === column)
30
- )
31
- );
32
- }));
33
- }
34
- const inferredJoins = await getInferredJoins2(dboBuilder.tablesOrViews);
35
- if (joinConfig === "inferred") {
36
- joinConfig = inferredJoins;
37
- /* If joins are specified then include inferred joins except the explicit tables */
38
- } else if (Array.isArray(joinConfig)) {
39
- const joinTables = joinConfig.map((j) => j.tables).flat();
40
- joinConfig = joinConfig.concat(
41
- inferredJoins.filter((j) => !j.tables.find((t) => joinTables.includes(t)))
42
- );
43
- // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
44
- } else if (joinConfig) {
45
- throw new Error(
46
- "Unexpected joins init param. Expecting 'inferred' OR joinConfig but got: " +
47
- JSON.stringify(joinConfig)
48
- );
49
- }
50
- const joins = JSON.parse(JSON.stringify(joinConfig)) as Join[];
51
-
52
- // Validate joins
53
- try {
54
- const tovNames = dboBuilder.tablesOrViews.map((t) => t.name);
55
-
56
- // 2 find incorrect tables
57
- const missing = joins.flatMap((j) => j.tables).find((t) => !tovNames.includes(t));
58
- if (missing) {
59
- throw "Table not found: " + missing;
60
- }
61
-
62
- // 3 find incorrect fields
63
- joins.map(({ tables, on }) => {
64
- const t1 = tables[0],
65
- t2 = tables[1];
66
- on.map((cond) => {
67
- const f1s = Object.keys(cond),
68
- f2s = Object.values(cond);
69
- [
70
- [t1, f1s],
71
- [t2, f2s],
72
- ].map((v) => {
73
- const t = <string>v[0],
74
- f = <string[]>v[1];
75
-
76
- const tov = dboBuilder.tablesOrViews!.find((_t) => _t.name === t);
77
- if (!tov) throw "Table not found: " + t;
78
- const m1 = f.filter((k) => !tov.columns.map((c) => c.name).includes(k));
79
- if (m1.length) {
80
- throw `Table ${t}(${tov.columns.map((c) => c.name).join()}) has no fields named: ${m1.join()}`;
81
- }
82
- });
83
- });
84
- });
85
-
86
- // 4 find incorrect/missing join types
87
- const expected_types =
88
- " \n\n-> Expecting: " + JOIN_TYPES.map((t) => JSON.stringify(t)).join(` | `);
89
- // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
90
- const mt = joins.find((j) => !j.type);
91
- if (mt) throw "Join type missing for: " + JSON.stringify(mt, null, 2) + expected_types;
92
-
93
- const it = joins.find((j) => !JOIN_TYPES.includes(j.type));
94
- if (it) throw "Incorrect join type for: " + JSON.stringify(it, null, 2) + expected_types;
95
- } catch (e: any) {
96
- const errMsg =
97
- ((joinConfig as any) === "inferred" ? "INFERRED " : "") +
98
- "JOINS VALIDATION ERROR \n-> " +
99
- e;
100
- throw errMsg;
101
- }
102
-
103
- // Make joins graph
104
- const joinGraph: Graph = {};
105
- joins.forEach(({ tables }) => {
106
- const _t = tables.slice().sort(),
107
- t1 = _t[0]!,
108
- t2 = _t[1]!;
109
-
110
- if (t1 === t2) return;
111
-
112
- joinGraph[t1] ??= {};
113
- joinGraph[t1][t2] = 1;
114
-
115
- joinGraph[t2] ??= {};
116
- joinGraph[t2][t1] = 1;
117
- });
118
- const tables = Array.from(new Set(joins.flatMap((t) => t.tables)));
119
- const shortestJoinPaths: JoinPaths = [];
120
- tables.forEach((t1) => {
121
- tables.forEach((t2) => {
122
- /** Prevent recursion */
123
- if (
124
- t1 === t2 ||
125
- shortestJoinPaths.some((jp) => {
126
- if (arrayValuesMatch([jp.t1, jp.t2], [t1, t2])) {
127
- const spath = findShortestPath(joinGraph, t1, t2);
128
- if (arrayValuesMatch(spath.path, jp.path)) {
129
- return true;
130
- }
131
- }
132
- })
133
- ) {
134
- return;
135
- }
136
-
137
- const spath = findShortestPath(joinGraph, t1, t2);
138
- if (!(spath.distance < Infinity)) return;
139
-
140
- const existing1 = shortestJoinPaths.find((j) => j.t1 === t1 && j.t2 === t2);
141
- if (!existing1) {
142
- shortestJoinPaths.push({ t1, t2, path: spath.path.slice() });
143
- }
144
-
145
- const existing2 = shortestJoinPaths.find((j) => j.t2 === t1 && j.t1 === t2);
146
- if (!existing2) {
147
- shortestJoinPaths.push({
148
- t1: t2,
149
- t2: t1,
150
- path: spath.path.slice().reverse(),
151
- });
152
- }
153
- });
154
- });
155
- return {
156
- joins,
157
- shortestJoinPaths,
158
- joinGraph,
159
- };
160
- }
161
-
162
- return {
163
- joins: [],
164
- shortestJoinPaths: [],
165
- };
166
- }
167
-
168
- const arrayValuesMatch = <T>(arr1: T[], arr2: T[]): boolean => {
169
- return arr1.slice().sort().join() === arr2.slice().sort().join();
170
- };
171
-
172
- function getInferredJoins2(schema: TableSchema[]): Join[] {
173
- const joins: Join[] = [];
174
- const upsertJoin = (
175
- t1: string,
176
- t2: string,
177
- cols: { col1: string; col2: string }[],
178
- type: Join["type"]
179
- ) => {
180
- const existingIdx = joins.findIndex((j) => arrayValuesMatch(j.tables.slice(0), [t1, t2]));
181
- const existing = joins[existingIdx];
182
- const normalCond = cols.reduce((a, v) => ({ ...a, [v.col1]: v.col2 }), {});
183
- const revertedCond = cols.reduce((a, v) => ({ ...a, [v.col2]: v.col1 }), {});
184
- if (existing) {
185
- const isLTR = existing.tables[0] === t1;
186
- const cond = isLTR ? normalCond : revertedCond;
187
-
188
- /** At some point we should add relationship type to EACH JOIN CONDITION GROUP */
189
- // const fixedType = isLTR? type : type.split("").reverse().join("") as Join["type"];
190
-
191
- /** Avoid duplicates */
192
- if (!existing.on.some((_cond) => JSON.stringify(_cond) === JSON.stringify(cond))) {
193
- existing.on.push(cond);
194
- joins[existingIdx] = existing;
195
- }
196
- } else {
197
- joins.push({
198
- tables: [t1, t2],
199
- on: [normalCond],
200
- type,
201
- });
202
- }
203
- };
204
- schema.map((tov) => {
205
- tov.columns.map((col) => {
206
- if (col.references) {
207
- col.references.forEach((r) => {
208
- const joinCols = r.cols.map((c, i) => ({
209
- col1: c,
210
- col2: r.fcols[i]!,
211
- }));
212
- let type: Join["type"] = "one-many";
213
- const ftablePkeys = schema
214
- .find((_tov) => _tov.name === r.ftable)
215
- ?.columns.filter((fcol) => fcol.is_pkey);
216
- if (ftablePkeys?.length && ftablePkeys.every((fkey) => r.fcols.includes(fkey.name))) {
217
- type = "one-one";
218
- }
219
- upsertJoin(tov.name, r.ftable, joinCols, type);
220
- });
221
- }
222
- });
223
- });
224
- return joins;
225
- }