@loomcore/api 0.2.19 → 0.2.21

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,4 +1,4 @@
1
- import { EmptyUserContext, passwordValidator, TokenResponseSpec, } from "@loomcore/common/models";
1
+ import { EmptyUserContext, passwordValidator, TokenResponseSpec, UserSpec, } from "@loomcore/common/models";
2
2
  import { entityUtils } from "@loomcore/common/utils";
3
3
  import { config } from "../config/base-api-config.js";
4
4
  import { BadRequestError, UnauthenticatedError } from "../errors/index.js";
@@ -16,8 +16,12 @@ export class AuthController {
16
16
  loginResponseSpec;
17
17
  constructor(app, database, options = {}) {
18
18
  this.database = database;
19
+ const userSpecFromService = options.userService?.getModelSpec();
20
+ const inheritedUserSpec = userSpecFromService && userSpecFromService !== UserSpec
21
+ ? userSpecFromService
22
+ : undefined;
19
23
  const resolved = resolveAuthUserSpecs({
20
- userSpec: options.userSpec,
24
+ userSpec: options.userSpec ?? inheritedUserSpec,
21
25
  publicUserSpec: options.publicUserSpec,
22
26
  });
23
27
  this.userSpec = resolved.userSpec;
@@ -27,8 +31,9 @@ export class AuthController {
27
31
  this.userService =
28
32
  options.userService ?? new UserService(database, this.userSpec);
29
33
  this.organizationService = new OrganizationService(database);
30
- if (options.userSpec) {
31
- setAuthUserContextSpec(createUserContextSpec(this.userSpec));
34
+ const jwtUserModelSpec = options.userSpec ?? inheritedUserSpec ?? options.publicUserSpec;
35
+ if (jwtUserModelSpec) {
36
+ setAuthUserContextSpec(createUserContextSpec(jwtUserModelSpec));
32
37
  }
33
38
  this.mapRoutes(app);
34
39
  }
@@ -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) {
@@ -1,10 +1,11 @@
1
- import type { IEntity, IPagedResult, IQueryOptions, IUserContext } from "@loomcore/common/models";
1
+ import type { IEntity, IModelSpec, IPagedResult, IQueryOptions, IUserContext } from "@loomcore/common/models";
2
2
  import type { AppIdType } from "@loomcore/common/types";
3
3
  import type { ValueError } from "@sinclair/typebox/errors";
4
4
  import type { PostProcessEntityCustomFunction, PrepareQueryCustomFunction } from "../../controllers/types.js";
5
5
  import type { DeleteResult } from "../../databases/models/delete-result.js";
6
6
  import type { Operation } from "../../databases/operations/operation.js";
7
7
  export interface IGenericApiService<T extends IEntity> {
8
+ getModelSpec(): IModelSpec;
8
9
  validate(doc: any, isPartial?: boolean): ValueError[] | null;
9
10
  validateMany(docs: any[], isPartial?: boolean): ValueError[] | null;
10
11
  prepareQuery(userContext: IUserContext | undefined, queryObject: IQueryOptions, operations: Operation[]): {
@@ -12,6 +12,7 @@ export declare class GenericApiService<T extends IEntity> implements IGenericApi
12
12
  protected singularResourceName: string;
13
13
  protected modelSpec: IModelSpec;
14
14
  constructor(database: IDatabase, pluralResourceName: string, singularResourceName: string, modelSpec: IModelSpec);
15
+ getModelSpec(): IModelSpec;
15
16
  getAll(userContext: IUserContext): Promise<T[]>;
16
17
  getAll<TCustom extends IEntity>(userContext: IUserContext, prepareQueryCustom: PrepareQueryCustomFunction, postProcessEntityCustom: PostProcessEntityCustomFunction<T, TCustom>): Promise<TCustom[]>;
17
18
  prepareQuery(userContext: IUserContext | undefined, queryObject: IQueryOptions, operations: Operation[]): {
@@ -16,6 +16,9 @@ export class GenericApiService {
16
16
  this.modelSpec = modelSpec;
17
17
  this.database = database;
18
18
  }
19
+ getModelSpec() {
20
+ return this.modelSpec;
21
+ }
19
22
  async getAll(userContext, prepareQueryCustom, postProcessEntityCustom) {
20
23
  let operations = [];
21
24
  if (prepareQueryCustom) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@loomcore/api",
3
- "version": "0.2.19",
3
+ "version": "0.2.21",
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": {