@loomcore/api 0.1.99 → 0.1.101
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/dist/databases/postgres/utils/build-join-clauses.js +2 -12
- package/dist/databases/postgres/utils/build-select-clause.js +23 -1
- package/dist/databases/postgres/utils/transform-join-results.d.ts +1 -1
- package/dist/databases/postgres/utils/transform-join-results.js +32 -142
- package/package.json +1 -1
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { LeftJoin } from "../../operations/left-join.operation.js";
|
|
2
2
|
import { InnerJoin } from "../../operations/inner-join.operation.js";
|
|
3
|
-
import { LeftJoinMany } from "../../operations/left-join-many.operation.js";
|
|
4
3
|
import { toSnakeCase } from "./convert-keys.util.js";
|
|
5
4
|
function convertFieldToSnakeCase(field) {
|
|
6
5
|
if (field.startsWith("_")) {
|
|
@@ -20,20 +19,11 @@ function resolveLocalField(localField, mainTableName) {
|
|
|
20
19
|
export function buildJoinClauses(operations, mainTableName) {
|
|
21
20
|
let joinClause = "";
|
|
22
21
|
for (const operation of operations) {
|
|
23
|
-
if (operation instanceof LeftJoin || operation instanceof InnerJoin
|
|
22
|
+
if (operation instanceof LeftJoin || operation instanceof InnerJoin) {
|
|
24
23
|
const localRef = resolveLocalField(operation.localField, mainTableName);
|
|
25
24
|
const foreignSnake = convertFieldToSnakeCase(operation.foreignField);
|
|
26
25
|
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
|
-
}
|
|
26
|
+
joinClause = `${joinClause} ${joinType} "${operation.from}" AS ${operation.as} ON ${localRef} = ${operation.as}."${foreignSnake}"`;
|
|
37
27
|
}
|
|
38
28
|
}
|
|
39
29
|
return joinClause;
|
|
@@ -1,6 +1,22 @@
|
|
|
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
|
+
import { toSnakeCase } from './convert-keys.util.js';
|
|
5
|
+
function convertFieldToSnakeCase(field) {
|
|
6
|
+
if (field.startsWith('_')) {
|
|
7
|
+
return field;
|
|
8
|
+
}
|
|
9
|
+
return toSnakeCase(field);
|
|
10
|
+
}
|
|
11
|
+
function resolveLocalRef(localField, mainTableName) {
|
|
12
|
+
if (!localField.includes('.')) {
|
|
13
|
+
const snake = convertFieldToSnakeCase(localField);
|
|
14
|
+
return `"${mainTableName}"."${snake}"`;
|
|
15
|
+
}
|
|
16
|
+
const [alias, field] = localField.split('.');
|
|
17
|
+
const snake = convertFieldToSnakeCase(field);
|
|
18
|
+
return `${alias}."${snake}"`;
|
|
19
|
+
}
|
|
4
20
|
async function getTableColumns(client, tableName) {
|
|
5
21
|
const result = await client.query(`
|
|
6
22
|
SELECT column_name
|
|
@@ -40,7 +56,13 @@ export async function buildSelectClause(client, mainTableName, mainTableAlias, o
|
|
|
40
56
|
if (enrichment) {
|
|
41
57
|
continue;
|
|
42
58
|
}
|
|
43
|
-
|
|
59
|
+
const manyColumns = await getTableColumns(client, joinMany.from);
|
|
60
|
+
const foreignSnake = convertFieldToSnakeCase(joinMany.foreignField);
|
|
61
|
+
const localRef = resolveLocalRef(joinMany.localField, mainTableName);
|
|
62
|
+
const subAlias = `_sub_${joinMany.as}`;
|
|
63
|
+
const objParts = manyColumns.map(c => `'${c.replace(/'/g, "''")}', ${subAlias}."${c}"`).join(', ');
|
|
64
|
+
const subquery = `(SELECT COALESCE(jsonb_agg(jsonb_build_object(${objParts})), '[]'::jsonb) FROM "${joinMany.from}" AS ${subAlias} WHERE ${subAlias}."${foreignSnake}" = ${localRef})`;
|
|
65
|
+
joinSelects.push(`${subquery} AS "${joinMany.as}"`);
|
|
44
66
|
}
|
|
45
67
|
const allSelects = [...mainSelects, ...joinSelects];
|
|
46
68
|
return allSelects.join(', ');
|
|
@@ -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) {
|