@loomcore/api 0.1.112 → 0.1.114

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.
@@ -44,7 +44,7 @@ export async function batchUpdate(client, entities, operations, queryObject, plu
44
44
  queryObject.filters._id = { in: entityIds };
45
45
  const { whereClause, values } = buildWhereClause(queryObject, [], tablePrefix);
46
46
  const selectClause = hasJoins
47
- ? await buildSelectClause(client, pluralResourceName, pluralResourceName, operations)
47
+ ? await buildSelectClause(client, pluralResourceName, operations)
48
48
  : '*';
49
49
  const selectQuery = `
50
50
  SELECT ${selectClause} FROM "${pluralResourceName}" ${joinClauses}
@@ -8,7 +8,7 @@ export async function getAll(client, operations, pluralResourceName) {
8
8
  const joinClauses = buildJoinClauses(operations, pluralResourceName);
9
9
  const hasJoins = operations.some(op => op instanceof LeftJoin || op instanceof InnerJoin || op instanceof LeftJoinMany);
10
10
  const selectClause = hasJoins
11
- ? await buildSelectClause(client, pluralResourceName, pluralResourceName, operations)
11
+ ? await buildSelectClause(client, pluralResourceName, operations)
12
12
  : '*';
13
13
  const query = `SELECT ${selectClause} FROM "${pluralResourceName}" ${joinClauses}`;
14
14
  const result = await client.query(query);
@@ -12,7 +12,7 @@ export async function getById(client, operations, queryObject, id, pluralResourc
12
12
  const joinClauses = buildJoinClauses(operations, pluralResourceName);
13
13
  const hasJoins = operations.some(op => op instanceof LeftJoin || op instanceof InnerJoin || op instanceof LeftJoinMany);
14
14
  const selectClause = hasJoins
15
- ? await buildSelectClause(client, pluralResourceName, pluralResourceName, operations)
15
+ ? await buildSelectClause(client, pluralResourceName, operations)
16
16
  : '*';
17
17
  queryObject.filters || (queryObject.filters = {});
18
18
  queryObject.filters._id = { eq: id };
@@ -15,7 +15,7 @@ export async function get(client, operations, queryOptions, pluralResourceName)
15
15
  const paginationClause = buildPaginationClause(queryOptions);
16
16
  const hasJoins = operations.some(op => op instanceof LeftJoin || op instanceof InnerJoin || op instanceof LeftJoinMany);
17
17
  const selectClause = hasJoins
18
- ? await buildSelectClause(client, pluralResourceName, pluralResourceName, operations)
18
+ ? await buildSelectClause(client, pluralResourceName, operations)
19
19
  : '*';
20
20
  const tablePrefix = hasJoins ? pluralResourceName : undefined;
21
21
  const { whereClause, values } = buildWhereClause(queryOptions, [], tablePrefix);
@@ -1,2 +1,2 @@
1
1
  import { Operation } from "../../operations/operation.js";
2
- export declare function buildJoinClauses(operations: Operation[], mainTableName: string): string;
2
+ export declare function buildJoinClauses(operations: Operation[], mainTableName?: string): string;
@@ -7,24 +7,17 @@ function convertFieldToSnakeCase(field) {
7
7
  }
8
8
  return toSnakeCase(field);
9
9
  }
10
- function resolveLocalField(localField, mainTableName) {
11
- if (!localField.includes(".")) {
12
- const snake = convertFieldToSnakeCase(localField);
13
- return `"${mainTableName}"."${snake}"`;
14
- }
15
- const [alias, field] = localField.split(".");
16
- const snake = convertFieldToSnakeCase(field);
17
- return `${alias}."${snake}"`;
18
- }
19
10
  export function buildJoinClauses(operations, mainTableName) {
20
- let joinClause = "";
11
+ let joinClauses = [];
21
12
  for (const operation of operations) {
22
13
  if (operation instanceof LeftJoin || operation instanceof InnerJoin) {
23
- const localRef = resolveLocalField(operation.localField, mainTableName);
14
+ const localRef = convertFieldToSnakeCase(operation.localField);
24
15
  const foreignSnake = convertFieldToSnakeCase(operation.foreignField);
25
16
  const joinType = operation instanceof InnerJoin ? "INNER JOIN" : "LEFT JOIN";
26
- joinClause = `${joinClause} ${joinType} "${operation.from}" AS ${operation.as} ON ${localRef} = ${operation.as}."${foreignSnake}"`;
17
+ const joinTable = `"${operation.from}" AS "${operation.as}"`;
18
+ const leftSide = mainTableName ? `"${mainTableName}"."${localRef}"` : localRef;
19
+ joinClauses.push(`${joinType} ${joinTable} ON ${leftSide} = ${operation.as}."${foreignSnake}"`);
27
20
  }
28
21
  }
29
- return joinClause;
22
+ return joinClauses.join(' ');
30
23
  }
@@ -1,3 +1,3 @@
1
1
  import { Client } from 'pg';
2
2
  import { Operation } from '../../operations/operation.js';
3
- export declare function buildSelectClause(client: Client, mainTableName: string, mainTableAlias: string, operations: Operation[]): Promise<string>;
3
+ export declare function buildSelectClause(client: Client, mainTableName: string, operations: Operation[]): Promise<string>;
@@ -22,7 +22,7 @@ function findEnrichmentTarget(operation, operations) {
22
22
  }
23
23
  return null;
24
24
  }
25
- export async function buildSelectClause(client, mainTableName, mainTableAlias, operations) {
25
+ export async function buildSelectClause(client, mainTableName, operations) {
26
26
  const leftJoinOperations = operations.filter(op => op instanceof LeftJoin);
27
27
  const innerJoinOperations = operations.filter(op => op instanceof InnerJoin);
28
28
  const leftJoinManyOperations = operations.filter(op => op instanceof LeftJoinMany);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@loomcore/api",
3
- "version": "0.1.112",
3
+ "version": "0.1.114",
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": {