@loomcore/api 0.1.99 → 0.1.100

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.
@@ -24,16 +24,7 @@ export function buildJoinClauses(operations, mainTableName) {
24
24
  const localRef = resolveLocalField(operation.localField, mainTableName);
25
25
  const foreignSnake = convertFieldToSnakeCase(operation.foreignField);
26
26
  const joinType = operation instanceof InnerJoin ? "INNER JOIN" : "LEFT JOIN";
27
- if (operation instanceof LeftJoinMany) {
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
- }
27
+ joinClause = `${joinClause} ${joinType} "${operation.from}" AS ${operation.as} ON ${localRef} = ${operation.as}."${foreignSnake}"`;
37
28
  }
38
29
  }
39
30
  return joinClause;
@@ -1,2 +1,2 @@
1
1
  import { Operation } from '../../operations/operation.js';
2
- export declare function transformJoinResults<T>(rows: any[], operations: Operation[]): T[];
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 findEnrichmentTarget(operation, operations) {
43
- if (!operation.localField.includes('.')) {
44
- return null;
45
- }
46
- const [alias] = operation.localField.split('.');
47
- const target = operations.find(op => op instanceof LeftJoinMany && op.as === alias);
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 fieldName;
25
+ return aliases;
58
26
  }
27
+ const PREFIX_SEP = '__';
59
28
  export function transformJoinResults(rows, operations) {
60
- const leftJoinOperations = operations.filter(op => op instanceof LeftJoin);
61
- const innerJoinOperations = operations.filter(op => op instanceof InnerJoin);
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
- const allJoinAliases = [
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
- const hasJoinPrefix = allJoinOperations.some(join => key.startsWith(`${join.as}__`));
84
- const isJoinAlias = allJoinAliases.includes(key);
85
- if (!hasJoinPrefix && !isJoinAlias) {
86
- transformed[key] = row[key];
38
+ if (joinAliases.has(key)) {
39
+ joinData[key] = parseJsonValue(row[key]);
87
40
  }
88
- }
89
- for (const operation of operations) {
90
- if (enrichmentMap.has(operation)) {
91
- continue;
92
- }
93
- if (operation instanceof LeftJoin || operation instanceof InnerJoin) {
94
- const prefix = `${operation.as}__`;
95
- const joinedData = {};
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
- joinData[operation.as] = hasAnyData ? joinedData : null;
51
+ transformed[key] = row[key];
129
52
  }
130
53
  }
131
- else if (operation instanceof LeftJoinMany) {
132
- const jsonValue = parseJsonValue(row[operation.as]);
133
- let parsedValue = Array.isArray(jsonValue) ? jsonValue : (jsonValue ? [jsonValue] : []);
134
- const enrichments = Array.from(enrichmentMap.entries())
135
- .filter(([_, target]) => target === operation)
136
- .map(([enrichOp]) => enrichOp);
137
- if (enrichments.length > 0 && Array.isArray(parsedValue)) {
138
- for (const item of parsedValue) {
139
- if (item && typeof item === 'object') {
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) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@loomcore/api",
3
- "version": "0.1.99",
3
+ "version": "0.1.100",
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": {