@loomcore/api 0.1.99 → 0.1.100
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.
|
@@ -24,16 +24,7 @@ export function buildJoinClauses(operations, mainTableName) {
|
|
|
24
24
|
const localRef = resolveLocalField(operation.localField, mainTableName);
|
|
25
25
|
const foreignSnake = convertFieldToSnakeCase(operation.foreignField);
|
|
26
26
|
const joinType = operation instanceof InnerJoin ? "INNER JOIN" : "LEFT JOIN";
|
|
27
|
-
|
|
28
|
-
joinClause += ` ${joinType} (
|
|
29
|
-
SELECT "${foreignSnake}", json_agg(row_to_json(_many.*)) AS aggregated
|
|
30
|
-
FROM "${operation.from}" _many
|
|
31
|
-
GROUP BY "${foreignSnake}"
|
|
32
|
-
) AS ${operation.as} ON ${localRef} = ${operation.as}."${foreignSnake}"`;
|
|
33
|
-
}
|
|
34
|
-
else {
|
|
35
|
-
joinClause += ` ${joinType} "${operation.from}" AS ${operation.as} ON ${localRef} = ${operation.as}."${foreignSnake}"`;
|
|
36
|
-
}
|
|
27
|
+
joinClause = `${joinClause} ${joinType} "${operation.from}" AS ${operation.as} ON ${localRef} = ${operation.as}."${foreignSnake}"`;
|
|
37
28
|
}
|
|
38
29
|
}
|
|
39
30
|
return joinClause;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import { Operation } from '../../operations/operation.js';
|
|
2
|
-
export declare function transformJoinResults<T>(rows:
|
|
2
|
+
export declare function transformJoinResults<T>(rows: Record<string, unknown>[], operations: Operation[]): T[];
|
|
@@ -1,30 +1,6 @@
|
|
|
1
1
|
import { LeftJoin } from '../../operations/left-join.operation.js';
|
|
2
2
|
import { InnerJoin } from '../../operations/inner-join.operation.js';
|
|
3
3
|
import { LeftJoinMany } from '../../operations/left-join-many.operation.js';
|
|
4
|
-
function findNestedObject(obj, alias, path = []) {
|
|
5
|
-
if (obj[alias] !== undefined && obj[alias] !== null) {
|
|
6
|
-
return { obj: obj[alias], path: [...path, alias] };
|
|
7
|
-
}
|
|
8
|
-
for (const key in obj) {
|
|
9
|
-
if (obj[key] && typeof obj[key] === 'object' && !Array.isArray(obj[key])) {
|
|
10
|
-
const found = findNestedObject(obj[key], alias, [...path, key]);
|
|
11
|
-
if (found !== null) {
|
|
12
|
-
return found;
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
else if (Array.isArray(obj[key])) {
|
|
16
|
-
for (const item of obj[key]) {
|
|
17
|
-
if (item && typeof item === 'object') {
|
|
18
|
-
const found = findNestedObject(item, alias, [...path, key]);
|
|
19
|
-
if (found !== null) {
|
|
20
|
-
return found;
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
return null;
|
|
27
|
-
}
|
|
28
4
|
function parseJsonValue(value) {
|
|
29
5
|
if (value === null || value === undefined) {
|
|
30
6
|
return null;
|
|
@@ -39,137 +15,51 @@ function parseJsonValue(value) {
|
|
|
39
15
|
}
|
|
40
16
|
return value;
|
|
41
17
|
}
|
|
42
|
-
function
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
if (target && operations.indexOf(target) < operations.indexOf(operation)) {
|
|
49
|
-
return target;
|
|
50
|
-
}
|
|
51
|
-
return null;
|
|
52
|
-
}
|
|
53
|
-
function mapEnrichmentFieldName(fieldName, targetAlias) {
|
|
54
|
-
if (fieldName === 'policy_agents' && (targetAlias === 'client_policies' || targetAlias === 'policies')) {
|
|
55
|
-
return 'agents';
|
|
18
|
+
function getJoinAliases(operations) {
|
|
19
|
+
const aliases = new Set();
|
|
20
|
+
for (const op of operations) {
|
|
21
|
+
if (op instanceof LeftJoin || op instanceof InnerJoin || op instanceof LeftJoinMany) {
|
|
22
|
+
aliases.add(op.as);
|
|
23
|
+
}
|
|
56
24
|
}
|
|
57
|
-
return
|
|
25
|
+
return aliases;
|
|
58
26
|
}
|
|
27
|
+
const PREFIX_SEP = '__';
|
|
59
28
|
export function transformJoinResults(rows, operations) {
|
|
60
|
-
const
|
|
61
|
-
|
|
62
|
-
const leftJoinManyOperations = operations.filter(op => op instanceof LeftJoinMany);
|
|
63
|
-
const allJoinOperations = [...leftJoinOperations, ...innerJoinOperations];
|
|
64
|
-
if (allJoinOperations.length === 0 &&
|
|
65
|
-
leftJoinManyOperations.length === 0) {
|
|
29
|
+
const joinAliases = getJoinAliases(operations);
|
|
30
|
+
if (joinAliases.size === 0) {
|
|
66
31
|
return rows;
|
|
67
32
|
}
|
|
68
|
-
|
|
69
|
-
...allJoinOperations.map(j => j.as),
|
|
70
|
-
...leftJoinManyOperations.map(j => j.as)
|
|
71
|
-
];
|
|
72
|
-
const enrichmentMap = new Map();
|
|
73
|
-
for (const op of leftJoinManyOperations) {
|
|
74
|
-
const target = findEnrichmentTarget(op, operations);
|
|
75
|
-
if (target) {
|
|
76
|
-
enrichmentMap.set(op, target);
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
return rows.map(row => {
|
|
33
|
+
return rows.map((row) => {
|
|
80
34
|
const transformed = {};
|
|
81
35
|
const joinData = {};
|
|
36
|
+
const prefixedByAlias = {};
|
|
82
37
|
for (const key of Object.keys(row)) {
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
if (!hasJoinPrefix && !isJoinAlias) {
|
|
86
|
-
transformed[key] = row[key];
|
|
38
|
+
if (joinAliases.has(key)) {
|
|
39
|
+
joinData[key] = parseJsonValue(row[key]);
|
|
87
40
|
}
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
let hasAnyData = false;
|
|
97
|
-
for (const key of Object.keys(row)) {
|
|
98
|
-
if (key.startsWith(prefix)) {
|
|
99
|
-
const columnName = key.substring(prefix.length);
|
|
100
|
-
const value = row[key];
|
|
101
|
-
joinedData[columnName] = value;
|
|
102
|
-
if (value !== null && value !== undefined) {
|
|
103
|
-
hasAnyData = true;
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
if (operation.localField.includes('.')) {
|
|
108
|
-
const [tableAlias] = operation.localField.split('.');
|
|
109
|
-
const relatedJoin = allJoinOperations.find(j => j.as === tableAlias);
|
|
110
|
-
let targetObject = null;
|
|
111
|
-
if (relatedJoin && joinData[relatedJoin.as]) {
|
|
112
|
-
targetObject = joinData[relatedJoin.as];
|
|
113
|
-
}
|
|
114
|
-
else {
|
|
115
|
-
const found = findNestedObject(joinData, tableAlias);
|
|
116
|
-
if (found) {
|
|
117
|
-
targetObject = found.obj;
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
if (targetObject) {
|
|
121
|
-
targetObject[operation.as] = hasAnyData ? joinedData : null;
|
|
122
|
-
}
|
|
123
|
-
else {
|
|
124
|
-
joinData[operation.as] = hasAnyData ? joinedData : null;
|
|
125
|
-
}
|
|
41
|
+
else if (key.includes(PREFIX_SEP)) {
|
|
42
|
+
const i = key.indexOf(PREFIX_SEP);
|
|
43
|
+
const alias = key.slice(0, i);
|
|
44
|
+
const column = key.slice(i + PREFIX_SEP.length);
|
|
45
|
+
if (joinAliases.has(alias)) {
|
|
46
|
+
if (!prefixedByAlias[alias])
|
|
47
|
+
prefixedByAlias[alias] = {};
|
|
48
|
+
prefixedByAlias[alias][column] = row[key];
|
|
126
49
|
}
|
|
127
50
|
else {
|
|
128
|
-
|
|
51
|
+
transformed[key] = row[key];
|
|
129
52
|
}
|
|
130
53
|
}
|
|
131
|
-
else
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
for (const enrichment of enrichments) {
|
|
141
|
-
if (item[enrichment.as] !== undefined) {
|
|
142
|
-
const mappedName = mapEnrichmentFieldName(enrichment.as, operation.as);
|
|
143
|
-
if (mappedName !== enrichment.as) {
|
|
144
|
-
item[mappedName] = item[enrichment.as];
|
|
145
|
-
delete item[enrichment.as];
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
if (operation.localField.includes('.')) {
|
|
153
|
-
const [tableAlias] = operation.localField.split('.');
|
|
154
|
-
const relatedJoin = allJoinOperations.find(j => j.as === tableAlias);
|
|
155
|
-
const relatedJoinMany = leftJoinManyOperations.find(j => j.as === tableAlias);
|
|
156
|
-
let targetObject = null;
|
|
157
|
-
if (relatedJoin && joinData[relatedJoin.as]) {
|
|
158
|
-
targetObject = joinData[relatedJoin.as];
|
|
159
|
-
}
|
|
160
|
-
else if (relatedJoinMany && joinData[relatedJoinMany.as]) {
|
|
161
|
-
targetObject = joinData[relatedJoinMany.as];
|
|
162
|
-
}
|
|
163
|
-
if (targetObject) {
|
|
164
|
-
targetObject[operation.as] = parsedValue;
|
|
165
|
-
}
|
|
166
|
-
else {
|
|
167
|
-
joinData[operation.as] = parsedValue;
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
else {
|
|
171
|
-
joinData[operation.as] = parsedValue;
|
|
172
|
-
}
|
|
54
|
+
else {
|
|
55
|
+
transformed[key] = row[key];
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
for (const alias of Object.keys(prefixedByAlias)) {
|
|
59
|
+
const obj = prefixedByAlias[alias];
|
|
60
|
+
const hasAny = Object.values(obj).some(v => v !== null && v !== undefined);
|
|
61
|
+
if (!(alias in joinData)) {
|
|
62
|
+
joinData[alias] = hasAny ? obj : null;
|
|
173
63
|
}
|
|
174
64
|
}
|
|
175
65
|
if (Object.keys(joinData).length > 0) {
|