@loomcore/api 0.0.24 → 0.0.25

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,6 +1,6 @@
1
- import { DeleteResult, Document, FindOptions } from 'mongodb';
1
+ import { DeleteResult } from 'mongodb';
2
2
  import { ValueError } from '@sinclair/typebox/errors';
3
- import { IUserContext, IEntity, IPagedResult, QueryOptions } from '@loomcore/common/models';
3
+ import { IUserContext, IEntity, IPagedResult, IQueryOptions } from '@loomcore/common/models';
4
4
  export interface IGenericApiService<T extends IEntity> {
5
5
  validate(doc: any, isPartial?: boolean): ValueError[] | null;
6
6
  validateMany(docs: any[], isPartial?: boolean): ValueError[] | null;
@@ -9,17 +9,17 @@ export interface IGenericApiService<T extends IEntity> {
9
9
  prepareDataForDb(userContext: IUserContext, entity: T[], isCreate?: boolean): Promise<T[]>;
10
10
  prepareDataForDb(userContext: IUserContext, entity: Partial<T>[], isCreate?: boolean): Promise<Partial<T>[]>;
11
11
  getAll(userContext: IUserContext): Promise<T[]>;
12
- get(userContext: IUserContext, queryOptions: QueryOptions): Promise<IPagedResult<T>>;
12
+ get(userContext: IUserContext, queryOptions: IQueryOptions): Promise<IPagedResult<T>>;
13
13
  getById(userContext: IUserContext, id: string): Promise<T>;
14
14
  getCount(userContext: IUserContext): Promise<number>;
15
- create(userContext: IUserContext, item: T): Promise<T | null>;
16
- createMany(userContext: IUserContext, items: T[]): Promise<T[]>;
17
- fullUpdateById(userContext: IUserContext, id: string, item: T): Promise<T>;
18
- partialUpdateById(userContext: IUserContext, id: string, item: Partial<T>): Promise<T>;
19
- partialUpdateByIdWithoutBeforeAndAfter(userContext: IUserContext, id: string, item: Partial<T>): Promise<T>;
20
- update(userContext: IUserContext, queryObject: any, item: Partial<T>): Promise<T[]>;
15
+ create(userContext: IUserContext, entity: T | Partial<T>): Promise<T | null>;
16
+ createMany(userContext: IUserContext, entities: T[]): Promise<T[]>;
17
+ fullUpdateById(userContext: IUserContext, id: string, entity: T): Promise<T>;
18
+ partialUpdateById(userContext: IUserContext, id: string, entity: Partial<T>): Promise<T>;
19
+ partialUpdateByIdWithoutBeforeAndAfter(userContext: IUserContext, id: string, entity: T): Promise<T>;
20
+ update(userContext: IUserContext, queryObject: any, entity: Partial<T>): Promise<T[]>;
21
21
  deleteById(userContext: IUserContext, id: string): Promise<DeleteResult>;
22
22
  deleteMany(userContext: IUserContext, queryObject: any): Promise<DeleteResult>;
23
- find(userContext: IUserContext, mongoQueryObject: any, options?: FindOptions<Document> | undefined): Promise<T[]>;
24
- findOne(userContext: IUserContext, mongoQueryObject: any, options?: FindOptions<Document> | undefined): Promise<T>;
23
+ find(userContext: IUserContext, mongoQueryObject: any, options?: any): Promise<T[]>;
24
+ findOne(userContext: IUserContext, mongoQueryObject: any, options?: any): Promise<T>;
25
25
  }
@@ -1,6 +1,6 @@
1
1
  import { Db, Collection, DeleteResult, Document, FindOptions } from 'mongodb';
2
2
  import { ValueError } from '@sinclair/typebox/errors';
3
- import { IUserContext, IEntity, QueryOptions, IPagedResult, IModelSpec } from '@loomcore/common/models';
3
+ import { IUserContext, IEntity, IQueryOptions, IPagedResult, IModelSpec } from '@loomcore/common/models';
4
4
  import { IGenericApiService } from './generic-api-service.interface.js';
5
5
  export declare class GenericApiService<T extends IEntity> implements IGenericApiService<T> {
6
6
  protected db: Db;
@@ -12,9 +12,9 @@ export declare class GenericApiService<T extends IEntity> implements IGenericApi
12
12
  validate(doc: any, isPartial?: boolean): ValueError[] | null;
13
13
  validateMany(docs: any[], isPartial?: boolean): ValueError[] | null;
14
14
  protected getAdditionalPipelineStages(): any[];
15
- protected createAggregationPipeline(userContext: IUserContext, query: any, queryOptions?: QueryOptions): any[];
15
+ protected createAggregationPipeline(userContext: IUserContext, query: any, queryOptions?: IQueryOptions): any[];
16
16
  getAll(userContext: IUserContext): Promise<T[]>;
17
- get(userContext: IUserContext, queryOptions?: QueryOptions): Promise<IPagedResult<T>>;
17
+ get(userContext: IUserContext, queryOptions?: IQueryOptions): Promise<IPagedResult<T>>;
18
18
  getById(userContext: IUserContext, id: string): Promise<T>;
19
19
  getCount(userContext: IUserContext): Promise<number>;
20
20
  create(userContext: IUserContext, entity: T | Partial<T>): Promise<T | null>;
@@ -44,5 +44,5 @@ export declare class GenericApiService<T extends IEntity> implements IGenericApi
44
44
  prepareDataForDb(userContext: IUserContext, entity: Partial<T>[], isCreate?: boolean): Promise<Partial<T>[]>;
45
45
  protected prepareEntity(userContext: IUserContext, entity: T | Partial<T>, isCreate: boolean): Promise<T | Partial<T>>;
46
46
  protected prepareQuery(userContext: IUserContext | undefined, query: any): any;
47
- protected prepareQueryOptions(userContext: IUserContext | undefined, queryOptions: QueryOptions): QueryOptions;
47
+ protected prepareQueryOptions(userContext: IUserContext | undefined, queryOptions: IQueryOptions): IQueryOptions;
48
48
  }
@@ -1,7 +1,7 @@
1
1
  import { ObjectId } from 'mongodb';
2
2
  import moment from 'moment';
3
3
  import _ from 'lodash';
4
- import { QueryOptions } from '@loomcore/common/models';
4
+ import { DefaultQueryOptions } from '@loomcore/common/models';
5
5
  import { entityUtils } from '@loomcore/common/utils';
6
6
  import { BadRequestError, DuplicateKeyError, IdNotFoundError, NotFoundError, ServerError } from '../errors/index.js';
7
7
  import { apiUtils, dbUtils } from '../utils/index.js';
@@ -43,23 +43,34 @@ export class GenericApiService {
43
43
  return [];
44
44
  }
45
45
  createAggregationPipeline(userContext, query, queryOptions) {
46
- const match = { $match: query };
47
- const additionalStages = this.getAdditionalPipelineStages();
48
- let resultStages = [...additionalStages];
49
- if (queryOptions) {
50
- if (queryOptions.orderBy) {
51
- resultStages.push({
52
- $sort: {
53
- [queryOptions.orderBy]: queryOptions.sortDirection === 'asc' ? 1 : -1
54
- }
55
- });
56
- }
57
- if (queryOptions.page && queryOptions.pageSize) {
58
- resultStages.push({ $skip: (queryOptions.page - 1) * queryOptions.pageSize });
59
- resultStages.push({ $limit: queryOptions.pageSize });
60
- }
61
- }
62
- return [match, ...resultStages];
46
+ const pipeline = [
47
+ { $match: query },
48
+ { $facet: {
49
+ data: (() => {
50
+ const resultStages = [];
51
+ if (queryOptions) {
52
+ if (queryOptions.orderBy) {
53
+ resultStages.push({
54
+ $sort: {
55
+ [queryOptions.orderBy]: queryOptions.sortDirection === 'asc' ? 1 : -1
56
+ }
57
+ });
58
+ }
59
+ if (queryOptions.page && queryOptions.pageSize) {
60
+ resultStages.push({ $skip: (queryOptions.page - 1) * queryOptions.pageSize });
61
+ resultStages.push({ $limit: queryOptions.pageSize });
62
+ }
63
+ }
64
+ return resultStages;
65
+ })(),
66
+ count: [{ $count: 'total' }]
67
+ } },
68
+ { $project: {
69
+ data: 1,
70
+ total: { $arrayElemAt: ['$count.total', 0] }
71
+ } }
72
+ ];
73
+ return pipeline;
63
74
  }
64
75
  async getAll(userContext) {
65
76
  const query = this.prepareQuery(userContext, {});
@@ -74,7 +85,7 @@ export class GenericApiService {
74
85
  }
75
86
  return this.transformList(entities);
76
87
  }
77
- async get(userContext, queryOptions = new QueryOptions()) {
88
+ async get(userContext, queryOptions = { ...DefaultQueryOptions }) {
78
89
  const preparedOptions = this.prepareQueryOptions(userContext, queryOptions);
79
90
  const match = dbUtils.buildMongoMatchFromQueryOptions(preparedOptions);
80
91
  const additionalStages = this.getAdditionalPipelineStages();
@@ -1,10 +1,10 @@
1
1
  import { Db } from 'mongodb';
2
- import { IUserContext, IEntity, QueryOptions, IModelSpec } from '@loomcore/common/models';
2
+ import { IUserContext, IEntity, IQueryOptions, IModelSpec } from '@loomcore/common/models';
3
3
  import { GenericApiService } from './generic-api.service.js';
4
4
  export declare class MultiTenantApiService<T extends IEntity> extends GenericApiService<T> {
5
5
  private tenantDecorator?;
6
6
  constructor(db: Db, pluralResourceName: string, singularResourceName: string, modelSpec?: IModelSpec);
7
7
  protected prepareQuery(userContext: IUserContext, query: any): any;
8
- protected prepareQueryOptions(userContext: IUserContext, queryOptions: QueryOptions): QueryOptions;
8
+ protected prepareQueryOptions(userContext: IUserContext, queryOptions: IQueryOptions): IQueryOptions;
9
9
  protected prepareEntity(userContext: IUserContext, entity: T, isCreate: boolean): Promise<T | Partial<T>>;
10
10
  }
@@ -1,4 +1,4 @@
1
- import { IUserContext, QueryOptions, IEntity } from '@loomcore/common/models';
1
+ import { IUserContext, IQueryOptions, IEntity } from '@loomcore/common/models';
2
2
  export interface ITenantQueryOptions {
3
3
  orgIdField?: string;
4
4
  excludedCollections?: string[];
@@ -8,7 +8,7 @@ export declare class TenantQueryDecorator {
8
8
  private options;
9
9
  constructor(options?: Partial<ITenantQueryOptions>);
10
10
  applyTenantToQuery(userContext: IUserContext, queryObject: any, collectionName: string): any;
11
- applyTenantToQueryOptions(userContext: IUserContext, queryOptions: QueryOptions, collectionName: string): QueryOptions;
11
+ applyTenantToQueryOptions(userContext: IUserContext, queryOptions: IQueryOptions, collectionName: string): IQueryOptions;
12
12
  applyTenantToEntity<T extends IEntity>(userContext: IUserContext, entity: T, collectionName: string): T;
13
13
  getOrgIdField(): string;
14
14
  }
@@ -1,4 +1,3 @@
1
- import { QueryOptions } from '@loomcore/common/models';
2
1
  import { ServerError } from '../errors/index.js';
3
2
  export const DEFAULT_TENANT_OPTIONS = {
4
3
  orgIdField: '_orgId',
@@ -25,20 +24,17 @@ export class TenantQueryDecorator {
25
24
  return result;
26
25
  }
27
26
  applyTenantToQueryOptions(userContext, queryOptions, collectionName) {
28
- let result = queryOptions;
29
- const shouldApplyTenantFilter = !this.options.excludedCollections?.includes(collectionName) &&
30
- userContext?._orgId;
27
+ const result = { ...queryOptions };
28
+ const shouldApplyTenantFilter = !this.options.excludedCollections?.includes(collectionName);
31
29
  if (shouldApplyTenantFilter) {
32
- const modifiedQueryOptions = new QueryOptions(queryOptions);
33
- if (!modifiedQueryOptions.filters) {
34
- modifiedQueryOptions.filters = {};
30
+ if (!userContext._orgId) {
31
+ throw new ServerError('userContext must have an _orgId property to apply tenant filtering');
35
32
  }
36
- const orgIdField = this.options.orgIdField || '_orgId';
37
- modifiedQueryOptions.filters[orgIdField] = { eq: userContext._orgId };
38
- result = modifiedQueryOptions;
39
- }
40
- else if (!userContext?._orgId) {
41
- throw new ServerError('No _orgId found in userContext');
33
+ if (!result.filters) {
34
+ result.filters = {};
35
+ }
36
+ const orgIdField = this.getOrgIdField();
37
+ result.filters[orgIdField] = { eq: userContext._orgId };
42
38
  }
43
39
  return result;
44
40
  }
@@ -1,14 +1,14 @@
1
1
  import { Request, Response } from 'express';
2
2
  import { TSchema } from '@sinclair/typebox';
3
- import { IError, QueryOptions, IPagedResult, IModelSpec } from '@loomcore/common/models';
3
+ import { IQueryOptions, IError, IPagedResult, IModelSpec } from '@loomcore/common/models';
4
4
  export interface IApiResponseOptions<T> {
5
5
  messages?: string[];
6
6
  errors?: IError[];
7
7
  data?: T;
8
8
  }
9
9
  declare function apiResponse<T>(response: Response, status: number, options?: IApiResponseOptions<T>, modelSpec?: IModelSpec, publicSchema?: TSchema): Response;
10
- declare function getQueryOptionsFromRequest(request: Request): QueryOptions;
11
- declare function getPagedResult<T>(entities: T[], totalRows: number, queryOptions: QueryOptions): IPagedResult<T>;
10
+ declare function getQueryOptionsFromRequest(request: Request): IQueryOptions;
11
+ declare function getPagedResult<T>(entities: T[], totalRows: number, queryOptions: IQueryOptions): IPagedResult<T>;
12
12
  export declare const apiUtils: {
13
13
  apiResponse: typeof apiResponse;
14
14
  getQueryOptionsFromRequest: typeof getQueryOptionsFromRequest;
@@ -1,4 +1,4 @@
1
- import { QueryOptions } from '@loomcore/common/models';
1
+ import { DefaultQueryOptions } from '@loomcore/common/models';
2
2
  function apiResponse(response, status, options = {}, modelSpec, publicSchema) {
3
3
  const success = status >= 200 && status < 300;
4
4
  let apiResponse;
@@ -35,21 +35,22 @@ function apiResponse(response, status, options = {}, modelSpec, publicSchema) {
35
35
  }
36
36
  function getQueryOptionsFromRequest(request) {
37
37
  const queryOptions = {
38
+ ...DefaultQueryOptions,
38
39
  orderBy: request.query.orderBy,
39
40
  sortDirection: request.query.sortDirection,
40
- page: request.query.page ? parseInt(request.query.page) : 1,
41
- pageSize: request.query.pageSize ? parseInt(request.query.pageSize) : 100,
41
+ page: request.query.page ? parseInt(request.query.page) : DefaultQueryOptions.page,
42
+ pageSize: request.query.pageSize ? parseInt(request.query.pageSize) : DefaultQueryOptions.pageSize,
42
43
  filters: request.query.filters
43
44
  };
44
- return new QueryOptions(queryOptions);
45
+ return queryOptions;
45
46
  }
46
47
  function getPagedResult(entities, totalRows, queryOptions) {
47
48
  const pagedResult = {
48
49
  entities,
49
50
  total: totalRows,
50
- page: queryOptions.page,
51
- pageSize: queryOptions.pageSize,
52
- totalPages: Math.ceil(totalRows / queryOptions.pageSize),
51
+ page: queryOptions.page || DefaultQueryOptions.page,
52
+ pageSize: queryOptions.pageSize || DefaultQueryOptions.pageSize,
53
+ totalPages: Math.ceil(totalRows / (queryOptions.pageSize || DefaultQueryOptions.pageSize)),
53
54
  };
54
55
  return pagedResult;
55
56
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@loomcore/api",
3
- "version": "0.0.24",
3
+ "version": "0.0.25",
4
4
  "private": false,
5
5
  "description": "Loom Core Api - An opinionated Node.js api using Typescript, Express, and MongoDb",
6
6
  "scripts": {
@@ -44,7 +44,7 @@
44
44
  "jsonwebtoken": "^9.0.2"
45
45
  },
46
46
  "peerDependencies": {
47
- "@loomcore/common": "^0.0.12",
47
+ "@loomcore/common": "^0.0.13",
48
48
  "@sinclair/typebox": "^0.34.31",
49
49
  "cookie-parser": "^1.4.6",
50
50
  "cors": "^2.8.5",