@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.
- package/dist/services/generic-api-service.interface.d.ts +11 -11
- package/dist/services/generic-api.service.d.ts +4 -4
- package/dist/services/generic-api.service.js +30 -19
- package/dist/services/multi-tenant-api.service.d.ts +2 -2
- package/dist/services/tenant-query-decorator.d.ts +2 -2
- package/dist/services/tenant-query-decorator.js +9 -13
- package/dist/utils/api.utils.d.ts +3 -3
- package/dist/utils/api.utils.js +8 -7
- package/package.json +2 -2
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { DeleteResult
|
|
1
|
+
import { DeleteResult } from 'mongodb';
|
|
2
2
|
import { ValueError } from '@sinclair/typebox/errors';
|
|
3
|
-
import { IUserContext, IEntity, IPagedResult,
|
|
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:
|
|
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,
|
|
16
|
-
createMany(userContext: IUserContext,
|
|
17
|
-
fullUpdateById(userContext: IUserContext, id: string,
|
|
18
|
-
partialUpdateById(userContext: IUserContext, id: string,
|
|
19
|
-
partialUpdateByIdWithoutBeforeAndAfter(userContext: IUserContext, id: string,
|
|
20
|
-
update(userContext: IUserContext, queryObject: any,
|
|
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?:
|
|
24
|
-
findOne(userContext: IUserContext, mongoQueryObject: any, options?:
|
|
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,
|
|
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?:
|
|
15
|
+
protected createAggregationPipeline(userContext: IUserContext, query: any, queryOptions?: IQueryOptions): any[];
|
|
16
16
|
getAll(userContext: IUserContext): Promise<T[]>;
|
|
17
|
-
get(userContext: IUserContext, queryOptions?:
|
|
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:
|
|
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 {
|
|
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
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
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 =
|
|
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,
|
|
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:
|
|
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,
|
|
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:
|
|
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
|
-
|
|
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
|
-
|
|
33
|
-
|
|
34
|
-
modifiedQueryOptions.filters = {};
|
|
30
|
+
if (!userContext._orgId) {
|
|
31
|
+
throw new ServerError('userContext must have an _orgId property to apply tenant filtering');
|
|
35
32
|
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
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 {
|
|
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):
|
|
11
|
-
declare function getPagedResult<T>(entities: T[], totalRows: number, queryOptions:
|
|
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;
|
package/dist/utils/api.utils.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
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) :
|
|
41
|
-
pageSize: request.query.pageSize ? parseInt(request.query.pageSize) :
|
|
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
|
|
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.
|
|
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.
|
|
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",
|