@loomcore/api 0.0.14 → 0.0.17
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.
|
@@ -2,7 +2,7 @@ import { Db } from 'mongodb';
|
|
|
2
2
|
import { IUserContext, IEntity, QueryOptions, 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
|
-
private tenantDecorator
|
|
5
|
+
private tenantDecorator?;
|
|
6
6
|
constructor(db: Db, pluralResourceName: string, singularResourceName: string, modelSpec?: IModelSpec);
|
|
7
7
|
protected prepareQuery(userContext: IUserContext, query: any): any;
|
|
8
8
|
protected prepareQueryOptions(userContext: IUserContext, queryOptions: QueryOptions): QueryOptions;
|
|
@@ -1,25 +1,37 @@
|
|
|
1
1
|
import { GenericApiService } from './generic-api.service.js';
|
|
2
2
|
import { TenantQueryDecorator } from './tenant-query-decorator.js';
|
|
3
3
|
import { BadRequestError } from '../errors/bad-request.error.js';
|
|
4
|
+
import { config } from '../config/base-api-config.js';
|
|
4
5
|
export class MultiTenantApiService extends GenericApiService {
|
|
5
6
|
tenantDecorator;
|
|
6
7
|
constructor(db, pluralResourceName, singularResourceName, modelSpec) {
|
|
7
8
|
super(db, pluralResourceName, singularResourceName, modelSpec);
|
|
8
|
-
|
|
9
|
+
if (config?.app?.isMultiTenant) {
|
|
10
|
+
this.tenantDecorator = new TenantQueryDecorator();
|
|
11
|
+
}
|
|
9
12
|
}
|
|
10
13
|
prepareQuery(userContext, query) {
|
|
14
|
+
if (!config?.app?.isMultiTenant) {
|
|
15
|
+
return super.prepareQuery(userContext, query);
|
|
16
|
+
}
|
|
11
17
|
if (!userContext || !userContext._orgId) {
|
|
12
18
|
throw new BadRequestError('A valid userContext was not provided to MultiTenantApiService.prepareQuery');
|
|
13
19
|
}
|
|
14
20
|
return this.tenantDecorator.applyTenantToQuery(userContext, query, this.pluralResourceName);
|
|
15
21
|
}
|
|
16
22
|
prepareQueryOptions(userContext, queryOptions) {
|
|
23
|
+
if (!config?.app?.isMultiTenant) {
|
|
24
|
+
return super.prepareQueryOptions(userContext, queryOptions);
|
|
25
|
+
}
|
|
17
26
|
if (!userContext || !userContext._orgId) {
|
|
18
27
|
throw new BadRequestError('A valid userContext was not provided to MultiTenantApiService.prepareQueryOptions');
|
|
19
28
|
}
|
|
20
29
|
return this.tenantDecorator.applyTenantToQueryOptions(userContext, queryOptions, this.pluralResourceName);
|
|
21
30
|
}
|
|
22
31
|
async prepareEntity(userContext, entity, isCreate) {
|
|
32
|
+
if (!config?.app?.isMultiTenant) {
|
|
33
|
+
return super.prepareEntity(userContext, entity, isCreate);
|
|
34
|
+
}
|
|
23
35
|
if (!userContext || !userContext._orgId) {
|
|
24
36
|
throw new BadRequestError('A valid userContext was not provided to MultiTenantApiService.prepareEntity');
|
|
25
37
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@loomcore/api",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.17",
|
|
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.9",
|
|
48
48
|
"@sinclair/typebox": "^0.34.31",
|
|
49
49
|
"cookie-parser": "^1.4.6",
|
|
50
50
|
"cors": "^2.8.5",
|