@loomcore/api 0.1.103 → 0.1.104
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.
|
@@ -8,13 +8,19 @@ function convertFieldToSnakeCase(field) {
|
|
|
8
8
|
}
|
|
9
9
|
return toSnakeCase(field);
|
|
10
10
|
}
|
|
11
|
-
function resolveLocalRef(localField, mainTableName) {
|
|
11
|
+
function resolveLocalRef(localField, mainTableName, operations, currentIndex) {
|
|
12
12
|
if (!localField.includes('.')) {
|
|
13
13
|
const snake = convertFieldToSnakeCase(localField);
|
|
14
14
|
return `"${mainTableName}"."${snake}"`;
|
|
15
15
|
}
|
|
16
16
|
const [alias, field] = localField.split('.');
|
|
17
17
|
const snake = convertFieldToSnakeCase(field);
|
|
18
|
+
const priorOps = operations.slice(0, currentIndex);
|
|
19
|
+
const leftJoinMany = priorOps.find((op) => op instanceof LeftJoinMany && op.as === alias);
|
|
20
|
+
if (leftJoinMany) {
|
|
21
|
+
const castType = field === '_id' || snake === '_id' ? '::int' : '::text';
|
|
22
|
+
return `(SELECT jsonb_array_elements("${alias}")->>'${snake}'${castType})`;
|
|
23
|
+
}
|
|
18
24
|
return `${alias}."${snake}"`;
|
|
19
25
|
}
|
|
20
26
|
async function getTableColumns(client, tableName) {
|
|
@@ -51,17 +57,28 @@ export async function buildSelectClause(client, mainTableName, mainTableAlias, o
|
|
|
51
57
|
joinSelects.push(`${join.as}."${col}" AS "${join.as}__${col}"`);
|
|
52
58
|
}
|
|
53
59
|
}
|
|
54
|
-
for (
|
|
60
|
+
for (let i = 0; i < leftJoinManyOperations.length; i++) {
|
|
61
|
+
const joinMany = leftJoinManyOperations[i];
|
|
55
62
|
const enrichment = findEnrichmentTarget(joinMany, operations);
|
|
56
63
|
if (enrichment) {
|
|
57
64
|
continue;
|
|
58
65
|
}
|
|
59
66
|
const manyColumns = await getTableColumns(client, joinMany.from);
|
|
60
67
|
const foreignSnake = convertFieldToSnakeCase(joinMany.foreignField);
|
|
61
|
-
const
|
|
68
|
+
const currentOpIndex = operations.indexOf(joinMany);
|
|
69
|
+
const localRef = resolveLocalRef(joinMany.localField, mainTableName, operations, currentOpIndex);
|
|
62
70
|
const subAlias = `_sub_${joinMany.as}`;
|
|
63
71
|
const objParts = manyColumns.map(c => `'${c.replace(/'/g, "''")}', ${subAlias}."${c}"`).join(', ');
|
|
64
|
-
const
|
|
72
|
+
const isArrayRef = joinMany.localField.includes('.') &&
|
|
73
|
+
operations.slice(0, currentOpIndex).some(op => op instanceof LeftJoinMany && op.as === joinMany.localField.split('.')[0]);
|
|
74
|
+
let whereClause;
|
|
75
|
+
if (isArrayRef) {
|
|
76
|
+
whereClause = `${subAlias}."${foreignSnake}" IN ${localRef}`;
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
whereClause = `${subAlias}."${foreignSnake}" = ${localRef}`;
|
|
80
|
+
}
|
|
81
|
+
const subquery = `(SELECT COALESCE(jsonb_agg(jsonb_build_object(${objParts})), '[]'::jsonb) FROM "${joinMany.from}" AS ${subAlias} WHERE ${whereClause})`;
|
|
65
82
|
joinSelects.push(`${subquery} AS "${joinMany.as}"`);
|
|
66
83
|
}
|
|
67
84
|
const allSelects = [...mainSelects, ...joinSelects];
|