@loomcore/api 0.1.104 → 0.1.106

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.
@@ -19,7 +19,8 @@ function resolveLocalRef(localField, mainTableName, operations, currentIndex) {
19
19
  const leftJoinMany = priorOps.find((op) => op instanceof LeftJoinMany && op.as === alias);
20
20
  if (leftJoinMany) {
21
21
  const castType = field === '_id' || snake === '_id' ? '::int' : '::text';
22
- return `(SELECT jsonb_array_elements("${alias}")->>'${snake}'${castType})`;
22
+ const elemAlias = `_elem_${alias}`;
23
+ return `(SELECT (${elemAlias}->>'${snake}')${castType} FROM jsonb_array_elements("${alias}") AS ${elemAlias})`;
23
24
  }
24
25
  return `${alias}."${snake}"`;
25
26
  }
@@ -59,21 +60,23 @@ export async function buildSelectClause(client, mainTableName, mainTableAlias, o
59
60
  }
60
61
  for (let i = 0; i < leftJoinManyOperations.length; i++) {
61
62
  const joinMany = leftJoinManyOperations[i];
62
- const enrichment = findEnrichmentTarget(joinMany, operations);
63
- if (enrichment) {
64
- continue;
65
- }
66
63
  const manyColumns = await getTableColumns(client, joinMany.from);
67
64
  const foreignSnake = convertFieldToSnakeCase(joinMany.foreignField);
68
65
  const currentOpIndex = operations.indexOf(joinMany);
69
66
  const localRef = resolveLocalRef(joinMany.localField, mainTableName, operations, currentOpIndex);
70
67
  const subAlias = `_sub_${joinMany.as}`;
71
68
  const objParts = manyColumns.map(c => `'${c.replace(/'/g, "''")}', ${subAlias}."${c}"`).join(', ');
72
- const isArrayRef = joinMany.localField.includes('.') &&
73
- operations.slice(0, currentOpIndex).some(op => op instanceof LeftJoinMany && op.as === joinMany.localField.split('.')[0]);
69
+ const priorOps = operations.slice(0, currentOpIndex);
70
+ const referencedLeftJoinMany = joinMany.localField.includes('.')
71
+ ? priorOps.find((op) => op instanceof LeftJoinMany && op.as === joinMany.localField.split('.')[0])
72
+ : null;
74
73
  let whereClause;
75
- if (isArrayRef) {
76
- whereClause = `${subAlias}."${foreignSnake}" IN ${localRef}`;
74
+ if (referencedLeftJoinMany) {
75
+ const [prevAlias, fieldName] = joinMany.localField.split('.');
76
+ const prevFieldSnake = convertFieldToSnakeCase(fieldName);
77
+ const prevLocalRef = resolveLocalRef(referencedLeftJoinMany.localField, mainTableName, operations, operations.indexOf(referencedLeftJoinMany));
78
+ const prevForeignSnake = convertFieldToSnakeCase(referencedLeftJoinMany.foreignField);
79
+ whereClause = `${subAlias}."${foreignSnake}" IN (SELECT "${prevFieldSnake}" FROM "${referencedLeftJoinMany.from}" WHERE "${referencedLeftJoinMany.from}"."${prevForeignSnake}" = ${prevLocalRef})`;
77
80
  }
78
81
  else {
79
82
  whereClause = `${subAlias}."${foreignSnake}" = ${localRef}`;
@@ -28,15 +28,23 @@ function getJoinAliasesAndParents(operations) {
28
28
  return { aliases, parentByAlias };
29
29
  }
30
30
  const PREFIX_SEP = '__';
31
- function setJoinValue(joinData, alias, value, parentByAlias) {
31
+ function setJoinValue(joinData, alias, value, parentByAlias, operations) {
32
32
  const parent = parentByAlias.get(alias) ?? null;
33
33
  if (parent) {
34
- let parentObj = joinData[parent];
35
- if (parentObj == null || typeof parentObj !== 'object' || Array.isArray(parentObj)) {
36
- parentObj = {};
37
- joinData[parent] = parentObj;
34
+ const parentValue = joinData[parent];
35
+ const parentOp = operations.find(op => (op instanceof LeftJoinMany || op instanceof LeftJoin || op instanceof InnerJoin) && op.as === parent);
36
+ const isParentArray = parentOp instanceof LeftJoinMany;
37
+ if (isParentArray && Array.isArray(parentValue)) {
38
+ joinData[alias] = value;
39
+ }
40
+ else {
41
+ let parentObj = parentValue;
42
+ if (parentObj == null || typeof parentObj !== 'object' || Array.isArray(parentObj)) {
43
+ parentObj = {};
44
+ joinData[parent] = parentObj;
45
+ }
46
+ parentObj[alias] = value;
38
47
  }
39
- parentObj[alias] = value;
40
48
  }
41
49
  else {
42
50
  joinData[alias] = value;
@@ -87,7 +95,7 @@ export function transformJoinResults(rows, operations) {
87
95
  const value = flatJoinValues[alias];
88
96
  if (value === undefined)
89
97
  continue;
90
- setJoinValue(joinData, alias, value, parentByAlias);
98
+ setJoinValue(joinData, alias, value, parentByAlias, operations);
91
99
  }
92
100
  if (Object.keys(joinData).length > 0) {
93
101
  transformed._joinData = joinData;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@loomcore/api",
3
- "version": "0.1.104",
3
+ "version": "0.1.106",
4
4
  "private": false,
5
5
  "description": "Loom Core Api - An opinionated Node.js api using Typescript, Express, and MongoDb or PostgreSQL",
6
6
  "scripts": {