@loomcore/api 0.1.97 → 0.1.99

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.
Files changed (37) hide show
  1. package/LICENSE +201 -201
  2. package/README.md +77 -77
  3. package/dist/__tests__/common-test.utils.js +3 -5
  4. package/dist/__tests__/postgres-test-migrations/postgres-test-schema.js +266 -266
  5. package/dist/__tests__/postgres.test-database.js +8 -8
  6. package/dist/databases/migrations/migration-runner.js +21 -21
  7. package/dist/databases/mongo-db/utils/convert-operations-to-pipeline.util.js +65 -473
  8. package/dist/databases/operations/index.d.ts +3 -4
  9. package/dist/databases/operations/index.js +3 -4
  10. package/dist/databases/operations/{join.operation.d.ts → inner-join.operation.d.ts} +1 -1
  11. package/dist/databases/operations/{join.operation.js → inner-join.operation.js} +2 -2
  12. package/dist/databases/operations/left-join-many.operation.d.ts +7 -0
  13. package/dist/databases/operations/left-join-many.operation.js +15 -0
  14. package/dist/databases/operations/{join-many.operation.d.ts → left-join.operation.d.ts} +1 -1
  15. package/dist/databases/operations/{join-many.operation.js → left-join.operation.js} +2 -2
  16. package/dist/databases/operations/operation.d.ts +4 -5
  17. package/dist/databases/postgres/commands/postgres-batch-update.command.js +11 -12
  18. package/dist/databases/postgres/commands/postgres-create-many.command.js +4 -4
  19. package/dist/databases/postgres/commands/postgres-create.command.js +4 -4
  20. package/dist/databases/postgres/commands/postgres-full-update-by-id.command.js +14 -14
  21. package/dist/databases/postgres/commands/postgres-partial-update-by-id.command.js +8 -8
  22. package/dist/databases/postgres/commands/postgres-update.command.js +8 -8
  23. package/dist/databases/postgres/migrations/postgres-initial-schema.js +224 -224
  24. package/dist/databases/postgres/postgres.database.js +17 -17
  25. package/dist/databases/postgres/queries/postgres-get-all.query.js +4 -5
  26. package/dist/databases/postgres/queries/postgres-get-by-id.query.js +4 -5
  27. package/dist/databases/postgres/queries/postgres-get.query.js +4 -5
  28. package/dist/databases/postgres/utils/build-join-clauses.d.ts +1 -1
  29. package/dist/databases/postgres/utils/build-join-clauses.js +26 -360
  30. package/dist/databases/postgres/utils/build-select-clause.js +15 -27
  31. package/dist/databases/postgres/utils/does-table-exist.util.js +4 -4
  32. package/dist/databases/postgres/utils/transform-join-results.js +19 -120
  33. package/package.json +92 -92
  34. package/dist/databases/operations/join-through-many.operation.d.ts +0 -10
  35. package/dist/databases/operations/join-through-many.operation.js +0 -21
  36. package/dist/databases/operations/join-through.operation.d.ts +0 -10
  37. package/dist/databases/operations/join-through.operation.js +0 -21
@@ -0,0 +1,15 @@
1
+ export class LeftJoinMany {
2
+ from;
3
+ localField;
4
+ foreignField;
5
+ as;
6
+ constructor(from, localField, foreignField, as) {
7
+ if (from === as) {
8
+ throw new Error(`LeftJoinMany alias "${as}" must be different from table name "${from}". The alias is used to identify the join result and must be unique.`);
9
+ }
10
+ this.from = from;
11
+ this.localField = localField;
12
+ this.foreignField = foreignField;
13
+ this.as = as;
14
+ }
15
+ }
@@ -1,4 +1,4 @@
1
- export declare class JoinMany {
1
+ export declare class LeftJoin {
2
2
  from: string;
3
3
  localField: string;
4
4
  foreignField: string;
@@ -1,11 +1,11 @@
1
- export class JoinMany {
1
+ export class LeftJoin {
2
2
  from;
3
3
  localField;
4
4
  foreignField;
5
5
  as;
6
6
  constructor(from, localField, foreignField, as) {
7
7
  if (from === as) {
8
- throw new Error(`JoinMany alias "${as}" must be different from table name "${from}". The alias is used to identify the join result and must be unique.`);
8
+ throw new Error(`LeftJoin alias "${as}" must be different from table name "${from}". The alias is used to identify the join result and must be unique.`);
9
9
  }
10
10
  this.from = from;
11
11
  this.localField = localField;
@@ -1,5 +1,4 @@
1
- import { Join } from "./join.operation.js";
2
- import { JoinMany } from "./join-many.operation.js";
3
- import { JoinThrough } from "./join-through.operation.js";
4
- import { JoinThroughMany } from "./join-through-many.operation.js";
5
- export type Operation = Join | JoinMany | JoinThrough | JoinThroughMany;
1
+ import { LeftJoin } from "./left-join.operation.js";
2
+ import { InnerJoin } from "./inner-join.operation.js";
3
+ import { LeftJoinMany } from "./left-join-many.operation.js";
4
+ export type Operation = LeftJoin | InnerJoin | LeftJoinMany;
@@ -1,7 +1,6 @@
1
- import { Join } from "../../operations/join.operation.js";
2
- import { JoinMany } from "../../operations/join-many.operation.js";
3
- import { JoinThrough } from "../../operations/join-through.operation.js";
4
- import { JoinThroughMany } from "../../operations/join-through-many.operation.js";
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";
5
4
  import { BadRequestError } from "../../../errors/index.js";
6
5
  import { buildJoinClauses } from '../utils/build-join-clauses.js';
7
6
  import { buildSelectClause } from '../utils/build-select-clause.js';
@@ -31,25 +30,25 @@ export async function batchUpdate(client, entities, operations, queryObject, plu
31
30
  const setClause = columns.map((col, index) => `${col} = $${index + 1}`).join(', ');
32
31
  queryObject.filters._id = { eq: _id };
33
32
  const { whereClause } = buildWhereClause(queryObject, values);
34
- const query = `
35
- UPDATE "${pluralResourceName}"
36
- SET ${setClause}
37
- ${whereClause}
33
+ const query = `
34
+ UPDATE "${pluralResourceName}"
35
+ SET ${setClause}
36
+ ${whereClause}
38
37
  `;
39
38
  await client.query(query, values);
40
39
  }
41
40
  await client.query('COMMIT');
42
41
  const joinClauses = buildJoinClauses(operations, pluralResourceName);
43
- const hasJoins = operations.some(op => op instanceof Join || op instanceof JoinMany || op instanceof JoinThrough || op instanceof JoinThroughMany);
42
+ const hasJoins = operations.some(op => op instanceof LeftJoin || op instanceof InnerJoin || op instanceof LeftJoinMany);
44
43
  const tablePrefix = hasJoins ? pluralResourceName : undefined;
45
44
  queryObject.filters._id = { in: entityIds };
46
45
  const { whereClause, values } = buildWhereClause(queryObject, [], tablePrefix);
47
46
  const selectClause = hasJoins
48
47
  ? await buildSelectClause(client, pluralResourceName, pluralResourceName, operations)
49
48
  : '*';
50
- const selectQuery = `
51
- SELECT ${selectClause} FROM "${pluralResourceName}" ${joinClauses}
52
- ${whereClause}
49
+ const selectQuery = `
50
+ SELECT ${selectClause} FROM "${pluralResourceName}" ${joinClauses}
51
+ ${whereClause}
53
52
  `;
54
53
  const result = await client.query(selectQuery, values);
55
54
  return hasJoins
@@ -33,10 +33,10 @@ export async function createMany(client, pluralResourceName, entities) {
33
33
  valueClauses.push(`(${placeholders})`);
34
34
  allValues.push(...values);
35
35
  });
36
- const query = `
37
- INSERT INTO "${pluralResourceName}" (${columns.map(col => `"${col}"`).join(', ')})
38
- VALUES ${valueClauses.join(', ')}
39
- RETURNING *
36
+ const query = `
37
+ INSERT INTO "${pluralResourceName}" (${columns.map(col => `"${col}"`).join(', ')})
38
+ VALUES ${valueClauses.join(', ')}
39
+ RETURNING *
40
40
  `;
41
41
  const result = await client.query(query, allValues);
42
42
  if (result.rows.length !== entities.length) {
@@ -5,10 +5,10 @@ export async function create(client, pluralResourceName, entity) {
5
5
  delete entity._id;
6
6
  const { columns, values } = columnsAndValuesFromEntity(entity);
7
7
  const placeholders = columns.map((_, index) => `$${index + 1}`).join(', ');
8
- const query = `
9
- INSERT INTO "${pluralResourceName}" (${columns.join(', ')})
10
- VALUES (${placeholders})
11
- RETURNING *
8
+ const query = `
9
+ INSERT INTO "${pluralResourceName}" (${columns.join(', ')})
10
+ VALUES (${placeholders})
11
+ RETURNING *
12
12
  `;
13
13
  const result = await client.query(query, values);
14
14
  if (result.rows.length === 0) {
@@ -2,12 +2,12 @@ import { BadRequestError, IdNotFoundError } from "../../../errors/index.js";
2
2
  import { buildJoinClauses } from '../utils/build-join-clauses.js';
3
3
  export async function fullUpdateById(client, operations, id, entity, pluralResourceName) {
4
4
  try {
5
- const tableColumns = await client.query(`
6
- SELECT column_name, column_default
7
- FROM information_schema.columns
8
- WHERE table_schema = current_schema()
9
- AND table_name = $1
10
- ORDER BY ordinal_position
5
+ const tableColumns = await client.query(`
6
+ SELECT column_name, column_default
7
+ FROM information_schema.columns
8
+ WHERE table_schema = current_schema()
9
+ AND table_name = $1
10
+ ORDER BY ordinal_position
11
11
  `, [pluralResourceName]);
12
12
  if (tableColumns.rows.length === 0) {
13
13
  throw new BadRequestError(`Unable to resolve columns for ${pluralResourceName}`);
@@ -40,19 +40,19 @@ export async function fullUpdateById(client, operations, id, entity, pluralResou
40
40
  throw new BadRequestError('Cannot perform full update with no fields to update');
41
41
  }
42
42
  const setClause = updateColumns.join(', ');
43
- const query = `
44
- UPDATE "${pluralResourceName}"
45
- SET ${setClause}
46
- WHERE "_id" = $${paramIndex}
43
+ const query = `
44
+ UPDATE "${pluralResourceName}"
45
+ SET ${setClause}
46
+ WHERE "_id" = $${paramIndex}
47
47
  `;
48
48
  const result = await client.query(query, [...updateValues, id]);
49
49
  if (result.rowCount === 0) {
50
50
  throw new IdNotFoundError();
51
51
  }
52
- const joinClauses = buildJoinClauses(operations);
53
- const selectQuery = `
54
- SELECT * FROM "${pluralResourceName}" ${joinClauses}
55
- WHERE "_id" = $1 LIMIT 1
52
+ const joinClauses = buildJoinClauses(operations, pluralResourceName);
53
+ const selectQuery = `
54
+ SELECT * FROM "${pluralResourceName}" ${joinClauses}
55
+ WHERE "_id" = $1 LIMIT 1
56
56
  `;
57
57
  const selectResult = await client.query(selectQuery, [id]);
58
58
  if (selectResult.rows.length === 0) {
@@ -10,19 +10,19 @@ export async function partialUpdateById(client, operations, id, entity, pluralRe
10
10
  throw new BadRequestError('Cannot perform partial update with no fields to update');
11
11
  }
12
12
  const setClause = updateColumns.map((col, index) => `${col} = $${index + 1}`).join(', ');
13
- const query = `
14
- UPDATE "${pluralResourceName}"
15
- SET ${setClause}
16
- WHERE "_id" = $${updateValues.length + 1}
13
+ const query = `
14
+ UPDATE "${pluralResourceName}"
15
+ SET ${setClause}
16
+ WHERE "_id" = $${updateValues.length + 1}
17
17
  `;
18
18
  const result = await client.query(query, [...updateValues, id]);
19
19
  if (result.rowCount === 0) {
20
20
  throw new IdNotFoundError();
21
21
  }
22
- const joinClauses = buildJoinClauses(operations);
23
- const selectQuery = `
24
- SELECT * FROM "${pluralResourceName}" ${joinClauses}
25
- WHERE "_id" = $1 LIMIT 1
22
+ const joinClauses = buildJoinClauses(operations, pluralResourceName);
23
+ const selectQuery = `
24
+ SELECT * FROM "${pluralResourceName}" ${joinClauses}
25
+ WHERE "_id" = $1 LIMIT 1
26
26
  `;
27
27
  const selectResult = await client.query(selectQuery, [id]);
28
28
  if (selectResult.rows.length === 0) {
@@ -17,21 +17,21 @@ export async function update(client, queryObject, entity, operations, pluralReso
17
17
  const originalIndex = parseInt(num, 10);
18
18
  return `$${originalIndex + updateValues.length}`;
19
19
  });
20
- const updateQuery = `
21
- UPDATE "${pluralResourceName}"
22
- SET ${setClause}
23
- ${whereClauseWithAdjustedParams}
20
+ const updateQuery = `
21
+ UPDATE "${pluralResourceName}"
22
+ SET ${setClause}
23
+ ${whereClauseWithAdjustedParams}
24
24
  `;
25
25
  const allUpdateValues = [...updateValues, ...whereValues];
26
26
  const result = await client.query(updateQuery, allUpdateValues);
27
27
  if (result.rowCount === 0) {
28
28
  throw new NotFoundError('No records found matching update query');
29
29
  }
30
- const joinClauses = buildJoinClauses(operations);
30
+ const joinClauses = buildJoinClauses(operations, pluralResourceName);
31
31
  const orderByClause = buildOrderByClause(queryObject);
32
- const selectQuery = `
33
- SELECT * FROM "${pluralResourceName}" ${joinClauses}
34
- ${whereClause} ${orderByClause}
32
+ const selectQuery = `
33
+ SELECT * FROM "${pluralResourceName}" ${joinClauses}
34
+ ${whereClause} ${orderByClause}
35
35
  `.trim();
36
36
  const selectResult = await client.query(selectQuery, whereValues);
37
37
  return selectResult.rows;