@loomcore/api 0.2.19 → 0.2.20

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,5 +1,9 @@
1
+ import { LeftJoin } from "../../operations/left-join.operation.js";
2
+ import { InnerJoin } from "../../operations/inner-join.operation.js";
3
+ import { LeftJoinMany } from "../../operations/left-join-many.operation.js";
1
4
  import { BadRequestError, IdNotFoundError } from "../../../errors/index.js";
2
5
  import { buildJoinClauses } from '../utils/build-join-clauses.js';
6
+ import { buildSelectClause } from '../utils/build-select-clause.js';
3
7
  export async function fullUpdateById(client, operations, id, entity, pluralResourceName) {
4
8
  try {
5
9
  const tableColumns = await client.query(`
@@ -49,16 +53,25 @@ export async function fullUpdateById(client, operations, id, entity, pluralResou
49
53
  if (result.rowCount === 0) {
50
54
  throw new IdNotFoundError();
51
55
  }
52
- const joinClauses = buildJoinClauses(operations, pluralResourceName);
56
+ const hasJoins = operations.some(op => op instanceof LeftJoin || op instanceof InnerJoin || op instanceof LeftJoinMany);
57
+ const joinClauses = hasJoins
58
+ ? buildJoinClauses(operations, pluralResourceName, { oneToOneOnly: true })
59
+ : buildJoinClauses(operations, pluralResourceName);
60
+ const selectClause = hasJoins
61
+ ? await buildSelectClause(client, pluralResourceName, operations)
62
+ : '*';
63
+ const idColumn = hasJoins ? `"${pluralResourceName}"."_id"` : '"_id"';
53
64
  const selectQuery = `
54
- SELECT * FROM "${pluralResourceName}" ${joinClauses}
55
- WHERE "_id" = $1 LIMIT 1
65
+ SELECT ${selectClause} FROM "${pluralResourceName}" ${joinClauses}
66
+ WHERE ${idColumn} = $1 LIMIT 1
56
67
  `;
57
68
  const selectResult = await client.query(selectQuery, [id]);
58
69
  if (selectResult.rows.length === 0) {
59
70
  throw new IdNotFoundError();
60
71
  }
61
- return selectResult.rows[0];
72
+ return hasJoins
73
+ ? selectResult.rows[0].entity
74
+ : selectResult.rows[0];
62
75
  }
63
76
  catch (err) {
64
77
  if (err instanceof IdNotFoundError) {
@@ -1,5 +1,9 @@
1
+ import { LeftJoin } from "../../operations/left-join.operation.js";
2
+ import { InnerJoin } from "../../operations/inner-join.operation.js";
3
+ import { LeftJoinMany } from "../../operations/left-join-many.operation.js";
1
4
  import { BadRequestError, IdNotFoundError } from "../../../errors/index.js";
2
5
  import { buildJoinClauses } from '../utils/build-join-clauses.js';
6
+ import { buildSelectClause } from '../utils/build-select-clause.js';
3
7
  import { columnsAndValuesFromEntity } from '../utils/columns-and-values-from-entity.js';
4
8
  export async function partialUpdateById(client, operations, id, entity, pluralResourceName) {
5
9
  try {
@@ -19,16 +23,25 @@ export async function partialUpdateById(client, operations, id, entity, pluralRe
19
23
  if (result.rowCount === 0) {
20
24
  throw new IdNotFoundError();
21
25
  }
22
- const joinClauses = buildJoinClauses(operations, pluralResourceName);
26
+ const hasJoins = operations.some(op => op instanceof LeftJoin || op instanceof InnerJoin || op instanceof LeftJoinMany);
27
+ const joinClauses = hasJoins
28
+ ? buildJoinClauses(operations, pluralResourceName, { oneToOneOnly: true })
29
+ : buildJoinClauses(operations, pluralResourceName);
30
+ const selectClause = hasJoins
31
+ ? await buildSelectClause(client, pluralResourceName, operations)
32
+ : '*';
33
+ const idColumn = hasJoins ? `"${pluralResourceName}"."_id"` : '"_id"';
23
34
  const selectQuery = `
24
- SELECT * FROM "${pluralResourceName}" ${joinClauses}
25
- WHERE "_id" = $1 LIMIT 1
35
+ SELECT ${selectClause} FROM "${pluralResourceName}" ${joinClauses}
36
+ WHERE ${idColumn} = $1 LIMIT 1
26
37
  `;
27
38
  const selectResult = await client.query(selectQuery, [id]);
28
39
  if (selectResult.rows.length === 0) {
29
40
  throw new IdNotFoundError();
30
41
  }
31
- return selectResult.rows[0];
42
+ return hasJoins
43
+ ? selectResult.rows[0].entity
44
+ : selectResult.rows[0];
32
45
  }
33
46
  catch (err) {
34
47
  if (err instanceof IdNotFoundError) {
@@ -1,7 +1,11 @@
1
+ import { LeftJoin } from "../../operations/left-join.operation.js";
2
+ import { InnerJoin } from "../../operations/inner-join.operation.js";
3
+ import { LeftJoinMany } from "../../operations/left-join-many.operation.js";
1
4
  import { BadRequestError, NotFoundError } from "../../../errors/index.js";
2
5
  import { buildWhereClause } from '../utils/build-where-clause.js';
3
6
  import { buildJoinClauses } from '../utils/build-join-clauses.js';
4
7
  import { buildOrderByClause } from '../utils/build-order-by-clause.js';
8
+ import { buildSelectClause } from '../utils/build-select-clause.js';
5
9
  import { columnsAndValuesFromEntity } from '../utils/columns-and-values-from-entity.js';
6
10
  export async function update(client, queryObject, entity, operations, pluralResourceName) {
7
11
  try {
@@ -27,14 +31,24 @@ export async function update(client, queryObject, entity, operations, pluralReso
27
31
  if (result.rowCount === 0) {
28
32
  throw new NotFoundError('No records found matching update query');
29
33
  }
30
- const joinClauses = buildJoinClauses(operations, pluralResourceName);
31
- const orderByClause = buildOrderByClause(queryObject);
34
+ const hasJoins = operations.some(op => op instanceof LeftJoin || op instanceof InnerJoin || op instanceof LeftJoinMany);
35
+ const joinClauses = hasJoins
36
+ ? buildJoinClauses(operations, pluralResourceName, { oneToOneOnly: true })
37
+ : buildJoinClauses(operations, pluralResourceName);
38
+ const orderByClause = buildOrderByClause(queryObject, hasJoins ? { tablePrefix: pluralResourceName } : undefined);
39
+ const tablePrefix = hasJoins ? pluralResourceName : undefined;
40
+ const { whereClause: selectWhereClause, values: selectWhereValues } = buildWhereClause(queryObject, [], tablePrefix);
41
+ const selectClause = hasJoins
42
+ ? await buildSelectClause(client, pluralResourceName, operations)
43
+ : '*';
32
44
  const selectQuery = `
33
- SELECT * FROM "${pluralResourceName}" ${joinClauses}
34
- ${whereClause} ${orderByClause}
45
+ SELECT ${selectClause} FROM "${pluralResourceName}" ${joinClauses}
46
+ ${selectWhereClause} ${orderByClause}
35
47
  `.trim();
36
- const selectResult = await client.query(selectQuery, whereValues);
37
- return selectResult.rows;
48
+ const selectResult = await client.query(selectQuery, selectWhereValues);
49
+ return hasJoins
50
+ ? selectResult.rows.map(r => r.entity)
51
+ : selectResult.rows;
38
52
  }
39
53
  catch (err) {
40
54
  if (err instanceof NotFoundError) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@loomcore/api",
3
- "version": "0.2.19",
3
+ "version": "0.2.20",
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": {