@loomcore/api 0.1.100 → 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.
@@ -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,7 +19,7 @@ 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 || operation instanceof LeftJoinMany) {
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";
@@ -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
- joinSelects.push(`${joinMany.as}.aggregated AS "${joinMany.as}"`);
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(', ');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@loomcore/api",
3
- "version": "0.1.100",
3
+ "version": "0.1.101",
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": {