@loomcore/api 0.1.104 → 0.1.105

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}`;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@loomcore/api",
3
- "version": "0.1.104",
3
+ "version": "0.1.105",
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": {