@loomcore/api 0.0.59 → 0.1.2
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/LICENSE +201 -0
- package/README.md +50 -0
- package/dist/__tests__/common-test.utils.d.ts +29 -60
- package/dist/__tests__/common-test.utils.js +88 -166
- package/dist/__tests__/index.d.ts +6 -0
- package/dist/__tests__/index.js +6 -0
- package/dist/__tests__/models/category.model.d.ts +8 -0
- package/dist/__tests__/models/category.model.js +6 -0
- package/dist/__tests__/models/product.model.d.ts +17 -0
- package/dist/__tests__/models/product.model.js +10 -0
- package/dist/__tests__/models/test-entity.model.d.ts +11 -0
- package/dist/__tests__/models/test-entity.model.js +10 -0
- package/dist/__tests__/models/test-item.model.d.ts +12 -0
- package/dist/__tests__/models/test-item.model.js +9 -0
- package/dist/__tests__/mongo-db.test-database.d.ts +15 -0
- package/dist/__tests__/mongo-db.test-database.js +74 -0
- package/dist/__tests__/postgres-test-migrations/001-create-test-entities-table.migration.d.ts +11 -0
- package/dist/__tests__/postgres-test-migrations/001-create-test-entities-table.migration.js +59 -0
- package/dist/__tests__/postgres-test-migrations/002-create-categories-table.migration.d.ts +11 -0
- package/dist/__tests__/postgres-test-migrations/002-create-categories-table.migration.js +52 -0
- package/dist/__tests__/postgres-test-migrations/003-create-products-table.migration.d.ts +11 -0
- package/dist/__tests__/postgres-test-migrations/003-create-products-table.migration.js +62 -0
- package/dist/__tests__/postgres-test-migrations/004-create-test-users-table.migration.d.ts +11 -0
- package/dist/__tests__/postgres-test-migrations/004-create-test-users-table.migration.js +66 -0
- package/dist/__tests__/postgres-test-migrations/005-create-test-items-table.migration.d.ts +11 -0
- package/dist/__tests__/postgres-test-migrations/005-create-test-items-table.migration.js +50 -0
- package/dist/__tests__/postgres-test-migrations/run-test-migrations.d.ts +2 -0
- package/dist/__tests__/postgres-test-migrations/run-test-migrations.js +22 -0
- package/dist/__tests__/postgres.test-database.d.ts +13 -0
- package/dist/__tests__/postgres.test-database.js +85 -0
- package/dist/__tests__/test-database.interface.d.ts +7 -0
- package/dist/__tests__/test-express-app.d.ts +9 -7
- package/dist/__tests__/test-express-app.js +38 -48
- package/dist/__tests__/test-objects.d.ts +23 -0
- package/dist/__tests__/test-objects.js +45 -0
- package/dist/config/base-api-config.d.ts +2 -2
- package/dist/config/base-api-config.js +2 -2
- package/dist/controllers/api.controller.d.ts +1 -5
- package/dist/controllers/api.controller.js +4 -11
- package/dist/controllers/auth.controller.d.ts +2 -2
- package/dist/controllers/auth.controller.js +4 -5
- package/dist/controllers/organizations.controller.d.ts +2 -2
- package/dist/controllers/organizations.controller.js +4 -4
- package/dist/controllers/users.controller.d.ts +2 -2
- package/dist/controllers/users.controller.js +2 -2
- package/dist/databases/index.d.ts +1 -0
- package/dist/databases/index.js +1 -0
- package/dist/databases/models/constants.d.ts +1 -0
- package/dist/databases/models/constants.js +1 -0
- package/dist/databases/models/database.d.ts +3 -0
- package/dist/databases/models/database.interface.d.ts +28 -0
- package/dist/databases/models/delete-result.d.ts +5 -0
- package/dist/databases/models/delete-result.js +8 -0
- package/dist/databases/models/index.d.ts +5 -0
- package/dist/databases/models/index.js +5 -0
- package/dist/databases/models/update-result.d.ts +5 -0
- package/dist/databases/models/update-result.js +8 -0
- package/dist/databases/mongo-db/commands/index.d.ts +8 -0
- package/dist/databases/mongo-db/commands/index.js +8 -0
- package/dist/databases/mongo-db/commands/mongo-batch-update.command.d.ts +4 -0
- package/dist/databases/mongo-db/commands/mongo-batch-update.command.js +41 -0
- package/dist/databases/mongo-db/commands/mongo-create-many.command.d.ts +5 -0
- package/dist/databases/mongo-db/commands/mongo-create-many.command.js +17 -0
- package/dist/databases/mongo-db/commands/mongo-create.command.d.ts +5 -0
- package/dist/databases/mongo-db/commands/mongo-create.command.js +17 -0
- package/dist/databases/mongo-db/commands/mongo-delete-by-id.command.d.ts +3 -0
- package/dist/databases/mongo-db/commands/mongo-delete-by-id.command.js +14 -0
- package/dist/databases/mongo-db/commands/mongo-delete-many.command.d.ts +4 -0
- package/dist/databases/mongo-db/commands/mongo-delete-many.command.js +9 -0
- package/dist/databases/mongo-db/commands/mongo-full-updateby-id.command.d.ts +3 -0
- package/dist/databases/mongo-db/commands/mongo-full-updateby-id.command.js +25 -0
- package/dist/databases/mongo-db/commands/mongo-partial-update-by-id.command.d.ts +3 -0
- package/dist/databases/mongo-db/commands/mongo-partial-update-by-id.command.js +25 -0
- package/dist/databases/mongo-db/commands/mongo-update.command.d.ts +4 -0
- package/dist/databases/mongo-db/commands/mongo-update.command.js +19 -0
- package/dist/databases/mongo-db/index.d.ts +4 -0
- package/dist/databases/mongo-db/index.js +4 -0
- package/dist/databases/mongo-db/models/no-sql-pipeline.d.ts +15 -0
- package/dist/databases/mongo-db/models/no-sql-pipeline.interface.d.ts +11 -0
- package/dist/databases/mongo-db/models/no-sql-pipeline.js +43 -0
- package/dist/databases/mongo-db/mongo-db.database.d.ts +32 -0
- package/dist/databases/mongo-db/mongo-db.database.js +65 -0
- package/dist/databases/mongo-db/queries/index.d.ts +6 -0
- package/dist/databases/mongo-db/queries/index.js +6 -0
- package/dist/databases/mongo-db/queries/mongo-find-one.query.d.ts +3 -0
- package/dist/databases/mongo-db/queries/mongo-find-one.query.js +9 -0
- package/dist/databases/mongo-db/queries/mongo-find.query.d.ts +3 -0
- package/dist/databases/mongo-db/queries/mongo-find.query.js +9 -0
- package/dist/databases/mongo-db/queries/mongo-get-all.query.d.ts +3 -0
- package/dist/databases/mongo-db/queries/mongo-get-all.query.js +17 -0
- package/dist/databases/mongo-db/queries/mongo-get-by-id.query.d.ts +4 -0
- package/dist/databases/mongo-db/queries/mongo-get-by-id.query.js +17 -0
- package/dist/databases/mongo-db/queries/mongo-get-count.query.d.ts +2 -0
- package/dist/databases/mongo-db/queries/mongo-get-count.query.js +5 -0
- package/dist/databases/mongo-db/queries/mongo-get.query.d.ts +4 -0
- package/dist/databases/mongo-db/queries/mongo-get.query.js +14 -0
- package/dist/databases/mongo-db/utils/build-find-options.util.d.ts +3 -0
- package/dist/databases/mongo-db/utils/build-find-options.util.js +15 -0
- package/dist/databases/mongo-db/utils/build-no-sql-match.util.d.ts +3 -0
- package/dist/databases/mongo-db/utils/build-no-sql-match.util.js +59 -0
- package/dist/databases/mongo-db/utils/convert-object-ids-to-strings.util.d.ts +1 -0
- package/dist/databases/mongo-db/utils/convert-object-ids-to-strings.util.js +32 -0
- package/dist/databases/mongo-db/utils/convert-operations-to-pipeline.util.d.ts +3 -0
- package/dist/databases/mongo-db/utils/convert-operations-to-pipeline.util.js +68 -0
- package/dist/databases/mongo-db/utils/convert-query-options-to-pipeline.util.d.ts +3 -0
- package/dist/databases/mongo-db/utils/convert-query-options-to-pipeline.util.js +31 -0
- package/dist/databases/mongo-db/utils/convert-strings-to-object-ids.util.d.ts +3 -0
- package/dist/databases/mongo-db/utils/convert-strings-to-object-ids.util.js +72 -0
- package/dist/databases/mongo-db/utils/index.d.ts +7 -0
- package/dist/databases/mongo-db/utils/index.js +7 -0
- package/dist/databases/operations/join.operation.d.ts +7 -0
- package/dist/databases/operations/join.operation.js +12 -0
- package/dist/databases/operations/operation.d.ts +2 -0
- package/dist/databases/postgres/commands/postgres-batch-update.command.d.ts +4 -0
- package/dist/databases/postgres/commands/postgres-batch-update.command.js +56 -0
- package/dist/databases/postgres/commands/postgres-create-many.command.d.ts +6 -0
- package/dist/databases/postgres/commands/postgres-create-many.command.js +63 -0
- package/dist/databases/postgres/commands/postgres-create.command.d.ts +6 -0
- package/dist/databases/postgres/commands/postgres-create.command.js +29 -0
- package/dist/databases/postgres/commands/postgres-delete-by-id.command.d.ts +3 -0
- package/dist/databases/postgres/commands/postgres-delete-by-id.command.js +6 -0
- package/dist/databases/postgres/commands/postgres-delete-many.command.d.ts +4 -0
- package/dist/databases/postgres/commands/postgres-delete-many.command.js +13 -0
- package/dist/databases/postgres/commands/postgres-full-update-by-id.command.d.ts +4 -0
- package/dist/databases/postgres/commands/postgres-full-update-by-id.command.js +72 -0
- package/dist/databases/postgres/commands/postgres-partial-update-by-id.command.d.ts +4 -0
- package/dist/databases/postgres/commands/postgres-partial-update-by-id.command.js +42 -0
- package/dist/databases/postgres/commands/postgres-update.command.d.ts +5 -0
- package/dist/databases/postgres/commands/postgres-update.command.js +48 -0
- package/dist/databases/postgres/migrations/001-create-migrations-table.migration.d.ts +11 -0
- package/dist/databases/postgres/migrations/001-create-migrations-table.migration.js +52 -0
- package/dist/databases/postgres/migrations/002-create-organizations-table.migration.d.ts +11 -0
- package/dist/databases/postgres/migrations/002-create-organizations-table.migration.js +55 -0
- package/dist/databases/postgres/migrations/003-create-users-table.migration.d.ts +11 -0
- package/dist/databases/postgres/migrations/003-create-users-table.migration.js +65 -0
- package/dist/databases/postgres/migrations/004-create-refresh-token-table.migration.d.ts +11 -0
- package/dist/databases/postgres/migrations/004-create-refresh-token-table.migration.js +57 -0
- package/dist/databases/postgres/migrations/index.d.ts +3 -0
- package/dist/databases/postgres/migrations/index.js +3 -0
- package/dist/databases/postgres/migrations/migration.interface.d.ts +6 -0
- package/dist/databases/postgres/migrations/migration.interface.js +1 -0
- package/dist/databases/postgres/migrations/setup-for-auth.migration.d.ts +2 -0
- package/dist/databases/postgres/migrations/setup-for-auth.migration.js +18 -0
- package/dist/databases/postgres/migrations/setup-for-multitenant.migration.d.ts +2 -0
- package/dist/databases/postgres/migrations/setup-for-multitenant.migration.js +16 -0
- package/dist/databases/postgres/postgres.database.d.ts +31 -0
- package/dist/databases/postgres/postgres.database.js +69 -0
- package/dist/databases/postgres/queries/postgres-find-one.query.d.ts +3 -0
- package/dist/databases/postgres/queries/postgres-find-one.query.js +13 -0
- package/dist/databases/postgres/queries/postgres-find.query.d.ts +3 -0
- package/dist/databases/postgres/queries/postgres-find.query.js +11 -0
- package/dist/databases/postgres/queries/postgres-get-all.query.d.ts +3 -0
- package/dist/databases/postgres/queries/postgres-get-all.query.js +14 -0
- package/dist/databases/postgres/queries/postgres-get-by-id.query.d.ts +4 -0
- package/dist/databases/postgres/queries/postgres-get-by-id.query.js +26 -0
- package/dist/databases/postgres/queries/postgres-get-count.query.d.ts +2 -0
- package/dist/databases/postgres/queries/postgres-get-count.query.js +4 -0
- package/dist/databases/postgres/queries/postgres-get.query.d.ts +4 -0
- package/dist/databases/postgres/queries/postgres-get.query.js +26 -0
- package/dist/databases/postgres/utils/build-count-query.d.ts +3 -0
- package/dist/databases/postgres/utils/build-count-query.js +7 -0
- package/dist/databases/postgres/utils/build-join-clauses.d.ts +2 -0
- package/dist/databases/postgres/utils/build-join-clauses.js +12 -0
- package/dist/databases/postgres/utils/build-order-by-clause.d.ts +2 -0
- package/dist/databases/postgres/utils/build-order-by-clause.js +8 -0
- package/dist/databases/postgres/utils/build-pagination-clause.d.ts +2 -0
- package/dist/databases/postgres/utils/build-pagination-clause.js +9 -0
- package/dist/databases/postgres/utils/build-select-clause.d.ts +3 -0
- package/dist/databases/postgres/utils/build-select-clause.js +28 -0
- package/dist/databases/postgres/utils/build-where-clause.d.ts +5 -0
- package/dist/databases/postgres/utils/build-where-clause.js +50 -0
- package/dist/databases/postgres/utils/columns-and-values-from-entity.d.ts +5 -0
- package/dist/databases/postgres/utils/columns-and-values-from-entity.js +9 -0
- package/dist/databases/postgres/utils/convert-null-to-undefined.util.d.ts +2 -0
- package/dist/databases/postgres/utils/convert-null-to-undefined.util.js +70 -0
- package/dist/databases/postgres/utils/transform-join-results.d.ts +2 -0
- package/dist/databases/postgres/utils/transform-join-results.js +33 -0
- package/dist/databases/utils/get-property-schema.util.d.ts +2 -0
- package/dist/databases/utils/get-property-schema.util.js +15 -0
- package/dist/databases/utils/index.d.ts +1 -0
- package/dist/databases/utils/index.js +1 -0
- package/dist/models/base-api-config.interface.d.ts +3 -2
- package/dist/models/index.d.ts +1 -1
- package/dist/models/index.js +1 -1
- package/dist/models/refresh-token.model.d.ts +18 -0
- package/dist/models/refresh-token.model.js +13 -0
- package/dist/services/auth.service.d.ts +11 -18
- package/dist/services/auth.service.js +29 -50
- package/dist/services/generic-api-service/generic-api-service.interface.d.ts +29 -0
- package/dist/services/generic-api-service/generic-api-service.interface.js +1 -0
- package/dist/services/generic-api-service/generic-api.service.d.ts +37 -0
- package/dist/services/generic-api-service/generic-api.service.js +178 -0
- package/dist/services/index.d.ts +2 -2
- package/dist/services/index.js +2 -2
- package/dist/services/multi-tenant-api.service.d.ts +9 -6
- package/dist/services/multi-tenant-api.service.js +10 -18
- package/dist/services/organization.service.d.ts +5 -5
- package/dist/services/organization.service.js +9 -6
- package/dist/services/password-reset-token.service.d.ts +3 -3
- package/dist/services/password-reset-token.service.js +5 -5
- package/dist/services/tenant-query-decorator.d.ts +1 -1
- package/dist/services/tenant-query-decorator.js +1 -1
- package/dist/services/user.service.d.ts +4 -6
- package/dist/services/user.service.js +4 -10
- package/dist/services/utils/audit-for-create.util.d.ts +2 -0
- package/dist/services/utils/audit-for-create.util.js +9 -0
- package/dist/services/utils/audit-for-update.util.d.ts +2 -0
- package/dist/services/utils/audit-for-update.util.js +7 -0
- package/dist/services/utils/strip-sender-provided-system-properties.util.d.ts +2 -0
- package/dist/services/utils/strip-sender-provided-system-properties.util.js +15 -0
- package/dist/utils/api.utils.js +2 -1
- package/dist/utils/index.d.ts +0 -1
- package/dist/utils/index.js +0 -1
- package/package.json +17 -6
- package/dist/services/generic-api-service.interface.d.ts +0 -27
- package/dist/services/generic-api.service.d.ts +0 -50
- package/dist/services/generic-api.service.js +0 -424
- package/dist/utils/db.utils.d.ts +0 -27
- package/dist/utils/db.utils.js +0 -318
- /package/dist/{controllers/api-controller.utils.d.ts → __tests__/test-database.interface.js} +0 -0
- /package/dist/{controllers/api-controller.utils.js → databases/models/database.interface.js} +0 -0
- /package/dist/{models/types/index.d.ts → databases/models/database.js} +0 -0
- /package/dist/{models/types/index.js → databases/mongo-db/models/no-sql-pipeline.interface.js} +0 -0
- /package/dist/{services/generic-api-service.interface.js → databases/operations/operation.js} +0 -0
|
@@ -18,7 +18,7 @@ export function setBaseApiConfig(apiConfig) {
|
|
|
18
18
|
console.warn('BaseApiConfig data has already been set. Ignoring subsequent calls to setBaseApiConfig.');
|
|
19
19
|
}
|
|
20
20
|
}
|
|
21
|
-
export async function initSystemUserContext(
|
|
21
|
+
export async function initSystemUserContext(database) {
|
|
22
22
|
if (!isConfigSet) {
|
|
23
23
|
throw new Error('BaseApiConfig has not been set. Call setBaseApiConfig first.');
|
|
24
24
|
}
|
|
@@ -27,7 +27,7 @@ export async function initSystemUserContext(db) {
|
|
|
27
27
|
let metaOrgId = undefined;
|
|
28
28
|
if (config.app.isMultiTenant) {
|
|
29
29
|
const { OrganizationService } = await import('../services/organization.service.js');
|
|
30
|
-
const organizationService = new OrganizationService(
|
|
30
|
+
const organizationService = new OrganizationService(database);
|
|
31
31
|
const metaOrg = await organizationService.getMetaOrg(EmptyUserContext);
|
|
32
32
|
if (!metaOrg) {
|
|
33
33
|
throw new Error('Meta organization not found. Please create an organization with isMetaOrg=true before starting the API.');
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Application, NextFunction, Request, Response } from 'express';
|
|
2
2
|
import { TSchema } from '@sinclair/typebox';
|
|
3
|
-
import { IEntity, IModelSpec
|
|
3
|
+
import { IEntity, IModelSpec } from '@loomcore/common/models';
|
|
4
4
|
import { IGenericApiService } from '../services/index.js';
|
|
5
5
|
export declare abstract class ApiController<T extends IEntity> {
|
|
6
6
|
protected app: Application;
|
|
@@ -13,10 +13,6 @@ export declare abstract class ApiController<T extends IEntity> {
|
|
|
13
13
|
mapRoutes(app: Application): void;
|
|
14
14
|
protected validate(entity: any, isPartial?: boolean): void;
|
|
15
15
|
protected validateMany(entities: any[], isPartial?: boolean): void;
|
|
16
|
-
protected prepareDataForDb(userContext: IUserContext, entity: T, isCreate?: boolean): Promise<T>;
|
|
17
|
-
protected prepareDataForDb(userContext: IUserContext, entity: Partial<T>, isCreate?: boolean): Promise<Partial<T>>;
|
|
18
|
-
protected prepareDataForDb(userContext: IUserContext, entity: T[], isCreate?: boolean): Promise<T[]>;
|
|
19
|
-
protected prepareDataForDb(userContext: IUserContext, entity: Partial<T>[], isCreate?: boolean): Promise<Partial<T>[]>;
|
|
20
16
|
getAll(req: Request, res: Response, next: NextFunction): Promise<void>;
|
|
21
17
|
get(req: Request, res: Response, next: NextFunction): Promise<void>;
|
|
22
18
|
getById(req: Request, res: Response, next: NextFunction): Promise<void>;
|
|
@@ -37,9 +37,6 @@ export class ApiController {
|
|
|
37
37
|
const validationErrors = this.service.validateMany(entities, isPartial);
|
|
38
38
|
entityUtils.handleValidationResult(validationErrors, `ApiController.validateMany for ${this.slug}`);
|
|
39
39
|
}
|
|
40
|
-
async prepareDataForDb(userContext, entity, isCreate = false) {
|
|
41
|
-
return await this.service.prepareDataForDb(userContext, entity, isCreate);
|
|
42
|
-
}
|
|
43
40
|
async getAll(req, res, next) {
|
|
44
41
|
res.set('Content-Type', 'application/json');
|
|
45
42
|
const entities = await this.service.getAll(req.userContext);
|
|
@@ -65,8 +62,7 @@ export class ApiController {
|
|
|
65
62
|
async create(req, res, next) {
|
|
66
63
|
res.set('Content-Type', 'application/json');
|
|
67
64
|
this.validate(req.body);
|
|
68
|
-
const
|
|
69
|
-
const entity = await this.service.create(req.userContext, preparedEntity);
|
|
65
|
+
const entity = await this.service.create(req.userContext, req.body);
|
|
70
66
|
apiUtils.apiResponse(res, 201, { data: entity || undefined }, this.modelSpec, this.publicSchema);
|
|
71
67
|
}
|
|
72
68
|
async batchUpdate(req, res, next) {
|
|
@@ -76,22 +72,19 @@ export class ApiController {
|
|
|
76
72
|
throw new BadRequestError('Request body must be an array of entities.');
|
|
77
73
|
}
|
|
78
74
|
this.validateMany(entities, true);
|
|
79
|
-
const
|
|
80
|
-
const updatedEntities = await this.service.batchUpdate(req.userContext, preparedEntities);
|
|
75
|
+
const updatedEntities = await this.service.batchUpdate(req.userContext, entities);
|
|
81
76
|
apiUtils.apiResponse(res, 200, { data: updatedEntities }, this.modelSpec, this.publicSchema);
|
|
82
77
|
}
|
|
83
78
|
async fullUpdateById(req, res, next) {
|
|
84
79
|
res.set('Content-Type', 'application/json');
|
|
85
80
|
this.validate(req.body);
|
|
86
|
-
const
|
|
87
|
-
const updateResult = await this.service.fullUpdateById(req.userContext, req.params.id, preparedEntity);
|
|
81
|
+
const updateResult = await this.service.fullUpdateById(req.userContext, req.params.id, req.body);
|
|
88
82
|
apiUtils.apiResponse(res, 200, { data: updateResult }, this.modelSpec, this.publicSchema);
|
|
89
83
|
}
|
|
90
84
|
async partialUpdateById(req, res, next) {
|
|
91
85
|
res.set('Content-Type', 'application/json');
|
|
92
86
|
this.validate(req.body, true);
|
|
93
|
-
const
|
|
94
|
-
const updateResult = await this.service.partialUpdateById(req.userContext, req.params.id, preparedEntity);
|
|
87
|
+
const updateResult = await this.service.partialUpdateById(req.userContext, req.params.id, req.body);
|
|
95
88
|
apiUtils.apiResponse(res, 200, { data: updateResult }, this.modelSpec, this.publicSchema);
|
|
96
89
|
}
|
|
97
90
|
async deleteById(req, res, next) {
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { Application, Request, Response, NextFunction } from 'express';
|
|
2
|
-
import { Db } from 'mongodb';
|
|
3
2
|
import { AuthService } from '../services/index.js';
|
|
3
|
+
import { IDatabase } from '../databases/models/index.js';
|
|
4
4
|
export declare class AuthController {
|
|
5
5
|
authService: AuthService;
|
|
6
|
-
constructor(app: Application,
|
|
6
|
+
constructor(app: Application, database: IDatabase);
|
|
7
7
|
mapRoutes(app: Application): void;
|
|
8
8
|
login(req: Request, res: Response, next: NextFunction): Promise<void>;
|
|
9
9
|
registerUser(req: Request, res: Response): Promise<void>;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { LoginResponseSpec, TokenResponseSpec, UserSpec, PublicUserSchema, UserContextSpec, passwordValidator } from '@loomcore/common/models';
|
|
1
|
+
import { LoginResponseSpec, TokenResponseSpec, UserSpec, PublicUserSchema, UserContextSpec, passwordValidator, } from '@loomcore/common/models';
|
|
2
2
|
import { entityUtils } from '@loomcore/common/utils';
|
|
3
3
|
import { BadRequestError, UnauthenticatedError } from '../errors/index.js';
|
|
4
4
|
import { isAuthenticated } from '../middleware/index.js';
|
|
@@ -6,8 +6,8 @@ import { apiUtils } from '../utils/index.js';
|
|
|
6
6
|
import { AuthService } from '../services/index.js';
|
|
7
7
|
export class AuthController {
|
|
8
8
|
authService;
|
|
9
|
-
constructor(app,
|
|
10
|
-
const authService = new AuthService(
|
|
9
|
+
constructor(app, database) {
|
|
10
|
+
const authService = new AuthService(database);
|
|
11
11
|
this.authService = authService;
|
|
12
12
|
this.mapRoutes(app);
|
|
13
13
|
}
|
|
@@ -31,8 +31,7 @@ export class AuthController {
|
|
|
31
31
|
const body = req.body;
|
|
32
32
|
const validationErrors = this.authService.validate(body);
|
|
33
33
|
entityUtils.handleValidationResult(validationErrors, 'AuthController.registerUser');
|
|
34
|
-
const
|
|
35
|
-
const user = await this.authService.createUser(userContext, preparedUser);
|
|
34
|
+
const user = await this.authService.createUser(userContext, body);
|
|
36
35
|
apiUtils.apiResponse(res, 201, { data: user || undefined }, UserSpec, PublicUserSchema);
|
|
37
36
|
}
|
|
38
37
|
async requestTokenUsingRefreshToken(req, res, next) {
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { Application, NextFunction, Request, Response } from 'express';
|
|
2
|
-
import { Db } from 'mongodb';
|
|
3
2
|
import { IOrganization } from '@loomcore/common/models';
|
|
4
3
|
import { ApiController } from './api.controller.js';
|
|
5
4
|
import { OrganizationService } from '../services/index.js';
|
|
5
|
+
import { IDatabase } from '../databases/models/index.js';
|
|
6
6
|
export declare class OrganizationsController extends ApiController<IOrganization> {
|
|
7
7
|
orgService: OrganizationService;
|
|
8
|
-
constructor(app: Application,
|
|
8
|
+
constructor(app: Application, database: IDatabase);
|
|
9
9
|
mapRoutes(app: Application): void;
|
|
10
10
|
getByName(req: Request, res: Response, next: NextFunction): Promise<void>;
|
|
11
11
|
getByCode(req: Request, res: Response, next: NextFunction): Promise<void>;
|
|
@@ -5,8 +5,8 @@ import { BadRequestError } from '../errors/index.js';
|
|
|
5
5
|
import { OrganizationService } from '../services/index.js';
|
|
6
6
|
export class OrganizationsController extends ApiController {
|
|
7
7
|
orgService;
|
|
8
|
-
constructor(app,
|
|
9
|
-
const orgService = new OrganizationService(
|
|
8
|
+
constructor(app, database) {
|
|
9
|
+
const orgService = new OrganizationService(database);
|
|
10
10
|
super('organizations', app, orgService);
|
|
11
11
|
this.orgService = orgService;
|
|
12
12
|
}
|
|
@@ -20,7 +20,7 @@ export class OrganizationsController extends ApiController {
|
|
|
20
20
|
let name = req.params?.name;
|
|
21
21
|
try {
|
|
22
22
|
res.set('Content-Type', 'application/json');
|
|
23
|
-
const entity = await this.orgService.findOne(req.userContext, { name: {
|
|
23
|
+
const entity = await this.orgService.findOne(req.userContext, { filters: { name: { contains: name } } });
|
|
24
24
|
if (!entity)
|
|
25
25
|
throw new BadRequestError('Name not found');
|
|
26
26
|
apiUtils.apiResponse(res, 200, { data: entity });
|
|
@@ -34,7 +34,7 @@ export class OrganizationsController extends ApiController {
|
|
|
34
34
|
let code = req.params?.code;
|
|
35
35
|
try {
|
|
36
36
|
res.set('Content-Type', 'application/json');
|
|
37
|
-
const entity = await this.orgService.findOne(req.userContext, { code: code });
|
|
37
|
+
const entity = await this.orgService.findOne(req.userContext, { filters: { code: { eq: code } } });
|
|
38
38
|
if (!entity)
|
|
39
39
|
throw new BadRequestError('Code not found');
|
|
40
40
|
apiUtils.apiResponse(res, 200, { data: entity });
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { Db } from 'mongodb';
|
|
2
1
|
import { Application } from 'express';
|
|
3
2
|
import { IUser } from '@loomcore/common/models';
|
|
4
3
|
import { ApiController } from './api.controller.js';
|
|
4
|
+
import { IDatabase } from '../databases/models/index.js';
|
|
5
5
|
export declare class UsersController extends ApiController<IUser> {
|
|
6
6
|
private userService;
|
|
7
|
-
constructor(app: Application,
|
|
7
|
+
constructor(app: Application, database: IDatabase);
|
|
8
8
|
mapRoutes(app: Application): void;
|
|
9
9
|
}
|
|
@@ -4,8 +4,8 @@ import { isAuthenticated } from '../middleware/index.js';
|
|
|
4
4
|
import { UserService } from '../services/index.js';
|
|
5
5
|
export class UsersController extends ApiController {
|
|
6
6
|
userService;
|
|
7
|
-
constructor(app,
|
|
8
|
-
const userService = new UserService(
|
|
7
|
+
constructor(app, database) {
|
|
8
|
+
const userService = new UserService(database);
|
|
9
9
|
super('users', app, userService, 'user', UserSpec, PublicUserSchema);
|
|
10
10
|
this.userService = userService;
|
|
11
11
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './mongo-db/index.js';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './mongo-db/index.js';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const PROPERTIES_THAT_ARE_NOT_OBJECT_IDS: string[];
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const PROPERTIES_THAT_ARE_NOT_OBJECT_IDS = ['_orgId'];
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { IModelSpec, IQueryOptions, IPagedResult, IEntity } from "@loomcore/common/models";
|
|
2
|
+
import { DeleteResult } from "./delete-result.js";
|
|
3
|
+
import { TSchema } from "@sinclair/typebox";
|
|
4
|
+
import { Operation } from "../operations/operation.js";
|
|
5
|
+
export interface IDatabase {
|
|
6
|
+
preprocessEntity<T extends IEntity>(entity: Partial<T>, modelSpec: TSchema): Partial<T>;
|
|
7
|
+
postprocessEntity<T extends IEntity>(entity: T, modelSpec: TSchema): T;
|
|
8
|
+
getAll<T extends IEntity>(operations: Operation[], pluralResourceName: string): Promise<T[]>;
|
|
9
|
+
get<T extends IEntity>(operations: Operation[], queryOptions: IQueryOptions, modelSpec: IModelSpec, pluralResourceName: string): Promise<IPagedResult<T>>;
|
|
10
|
+
getById<T extends IEntity>(operations: Operation[], queryObject: IQueryOptions, id: string, pluralResourceName: string): Promise<T | null>;
|
|
11
|
+
getCount(pluralResourceName: string): Promise<number>;
|
|
12
|
+
create<T extends IEntity>(entity: Partial<T>, pluralResourceName: string): Promise<{
|
|
13
|
+
insertedId: string;
|
|
14
|
+
entity: T;
|
|
15
|
+
}>;
|
|
16
|
+
createMany<T extends IEntity>(entities: Partial<T>[], pluralResourceName: string): Promise<{
|
|
17
|
+
insertedIds: string[];
|
|
18
|
+
entities: T[];
|
|
19
|
+
}>;
|
|
20
|
+
batchUpdate<T extends IEntity>(entities: Partial<T>[], operations: Operation[], queryObject: IQueryOptions, pluralResourceName: string): Promise<T[]>;
|
|
21
|
+
fullUpdateById<T extends IEntity>(operations: Operation[], id: string, entity: Partial<T>, pluralResourceName: string): Promise<T>;
|
|
22
|
+
partialUpdateById<T extends IEntity>(operations: Operation[], id: string, entity: Partial<T>, pluralResourceName: string): Promise<T>;
|
|
23
|
+
update<T extends IEntity>(queryObject: IQueryOptions, entity: Partial<T>, operations: Operation[], pluralResourceName: string): Promise<T[]>;
|
|
24
|
+
deleteById(id: string, pluralResourceName: string): Promise<DeleteResult>;
|
|
25
|
+
deleteMany(queryObject: IQueryOptions, pluralResourceName: string): Promise<DeleteResult>;
|
|
26
|
+
find<T extends IEntity>(queryObject: IQueryOptions, pluralResourceName: string): Promise<T[]>;
|
|
27
|
+
findOne<T extends IEntity>(queryObject: IQueryOptions, pluralResourceName: string): Promise<T | null>;
|
|
28
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export * from './mongo-create.command.js';
|
|
2
|
+
export * from './mongo-create-many.command.js';
|
|
3
|
+
export * from './mongo-batch-update.command.js';
|
|
4
|
+
export * from './mongo-full-updateby-id.command.js';
|
|
5
|
+
export * from './mongo-partial-update-by-id.command.js';
|
|
6
|
+
export * from './mongo-update.command.js';
|
|
7
|
+
export * from './mongo-delete-by-id.command.js';
|
|
8
|
+
export * from './mongo-delete-many.command.js';
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export * from './mongo-create.command.js';
|
|
2
|
+
export * from './mongo-create-many.command.js';
|
|
3
|
+
export * from './mongo-batch-update.command.js';
|
|
4
|
+
export * from './mongo-full-updateby-id.command.js';
|
|
5
|
+
export * from './mongo-partial-update-by-id.command.js';
|
|
6
|
+
export * from './mongo-update.command.js';
|
|
7
|
+
export * from './mongo-delete-by-id.command.js';
|
|
8
|
+
export * from './mongo-delete-many.command.js';
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { Db } from "mongodb";
|
|
2
|
+
import { Operation } from "../../operations/operation.js";
|
|
3
|
+
import { IQueryOptions } from "@loomcore/common/models";
|
|
4
|
+
export declare function batchUpdate<T>(db: Db, entities: Partial<T>[], operations: Operation[], queryObject: IQueryOptions, pluralResourceName: string): Promise<T[]>;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { ObjectId } from "mongodb";
|
|
2
|
+
import { BadRequestError } from "../../../errors/index.js";
|
|
3
|
+
import { convertOperationsToPipeline } from "../utils/index.js";
|
|
4
|
+
export async function batchUpdate(db, entities, operations, queryObject, pluralResourceName) {
|
|
5
|
+
const collection = db.collection(pluralResourceName);
|
|
6
|
+
if (!entities || entities.length === 0) {
|
|
7
|
+
return [];
|
|
8
|
+
}
|
|
9
|
+
const bulkOperations = [];
|
|
10
|
+
const entityIds = [];
|
|
11
|
+
for (const entity of entities) {
|
|
12
|
+
const { _id, ...updateData } = entity;
|
|
13
|
+
if (!_id || !(_id instanceof ObjectId)) {
|
|
14
|
+
throw new BadRequestError('Each entity in a batch update must have a valid _id that has been converted to an ObjectId.');
|
|
15
|
+
}
|
|
16
|
+
entityIds.push(_id);
|
|
17
|
+
bulkOperations.push({
|
|
18
|
+
updateOne: {
|
|
19
|
+
filter: { _id },
|
|
20
|
+
update: { $set: updateData },
|
|
21
|
+
},
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
if (bulkOperations.length > 0) {
|
|
25
|
+
await collection.bulkWrite(bulkOperations);
|
|
26
|
+
}
|
|
27
|
+
const baseQuery = { _id: { $in: entityIds } };
|
|
28
|
+
const operationsDocuments = convertOperationsToPipeline(operations);
|
|
29
|
+
let updatedEntities;
|
|
30
|
+
if (operationsDocuments.length > 0) {
|
|
31
|
+
const pipeline = [
|
|
32
|
+
{ $match: baseQuery },
|
|
33
|
+
...operationsDocuments
|
|
34
|
+
];
|
|
35
|
+
updatedEntities = await collection.aggregate(pipeline).toArray();
|
|
36
|
+
}
|
|
37
|
+
else {
|
|
38
|
+
updatedEntities = await collection.find(baseQuery).toArray();
|
|
39
|
+
}
|
|
40
|
+
return updatedEntities;
|
|
41
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { BadRequestError, DuplicateKeyError } from "../../../errors/index.js";
|
|
2
|
+
export async function createMany(db, pluralResourceName, entities) {
|
|
3
|
+
try {
|
|
4
|
+
const collection = db.collection(pluralResourceName);
|
|
5
|
+
const insertResult = await collection.insertMany(entities);
|
|
6
|
+
return {
|
|
7
|
+
insertedIds: insertResult.insertedIds,
|
|
8
|
+
entities: entities
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
catch (err) {
|
|
12
|
+
if (err.code === 11000) {
|
|
13
|
+
throw new DuplicateKeyError(`One or more ${pluralResourceName} already exist`);
|
|
14
|
+
}
|
|
15
|
+
throw new BadRequestError(`Error creating ${pluralResourceName}`);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { BadRequestError, DuplicateKeyError } from "../../../errors/index.js";
|
|
2
|
+
export async function create(db, pluralResourceName, entity) {
|
|
3
|
+
try {
|
|
4
|
+
const collection = db.collection(pluralResourceName);
|
|
5
|
+
const insertResult = await collection.insertOne(entity);
|
|
6
|
+
return {
|
|
7
|
+
insertedId: insertResult.insertedId,
|
|
8
|
+
entity: entity
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
catch (err) {
|
|
12
|
+
if (err.code === 11000) {
|
|
13
|
+
throw new DuplicateKeyError(`${pluralResourceName} already exists`);
|
|
14
|
+
}
|
|
15
|
+
throw new BadRequestError(`Error creating ${pluralResourceName}`);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { ObjectId } from "mongodb";
|
|
2
|
+
import { DeleteResult as GenericDeleteResult } from "../../models/delete-result.js";
|
|
3
|
+
import { BadRequestError } from "../../../errors/index.js";
|
|
4
|
+
import { entityUtils } from "@loomcore/common/utils";
|
|
5
|
+
export async function deleteById(db, id, pluralResourceName) {
|
|
6
|
+
if (!entityUtils.isValidObjectId(id)) {
|
|
7
|
+
throw new BadRequestError('id is not a valid ObjectId');
|
|
8
|
+
}
|
|
9
|
+
const collection = db.collection(pluralResourceName);
|
|
10
|
+
const objectId = new ObjectId(id);
|
|
11
|
+
const baseQuery = { _id: objectId };
|
|
12
|
+
const deleteResult = await collection.deleteOne(baseQuery);
|
|
13
|
+
return new GenericDeleteResult(deleteResult.acknowledged, deleteResult.deletedCount);
|
|
14
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { Db } from "mongodb";
|
|
2
|
+
import { IQueryOptions } from "@loomcore/common/models";
|
|
3
|
+
import { DeleteResult as GenericDeleteResult } from "../../models/delete-result.js";
|
|
4
|
+
export declare function deleteMany(db: Db, queryObject: IQueryOptions, pluralResourceName: string): Promise<GenericDeleteResult>;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { DeleteResult as GenericDeleteResult } from "../../models/delete-result.js";
|
|
2
|
+
import { buildNoSqlMatch } from "../utils/build-no-sql-match.util.js";
|
|
3
|
+
export async function deleteMany(db, queryObject, pluralResourceName) {
|
|
4
|
+
const collection = db.collection(pluralResourceName);
|
|
5
|
+
const matchDocument = buildNoSqlMatch(queryObject);
|
|
6
|
+
const filter = matchDocument.$match;
|
|
7
|
+
const deleteResult = await collection.deleteMany(filter);
|
|
8
|
+
return new GenericDeleteResult(deleteResult.acknowledged, deleteResult.deletedCount);
|
|
9
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { BadRequestError, IdNotFoundError } from "../../../errors/index.js";
|
|
2
|
+
import NoSqlPipeline from "../models/no-sql-pipeline.js";
|
|
3
|
+
import { buildNoSqlMatch } from "../utils/build-no-sql-match.util.js";
|
|
4
|
+
import { entityUtils } from "@loomcore/common/utils";
|
|
5
|
+
export async function fullUpdateById(db, operations, id, entity, pluralResourceName) {
|
|
6
|
+
if (!entityUtils.isValidObjectId(id)) {
|
|
7
|
+
throw new BadRequestError('id is not a valid ObjectId');
|
|
8
|
+
}
|
|
9
|
+
const collection = db.collection(pluralResourceName);
|
|
10
|
+
const matchDocument = buildNoSqlMatch({ filters: { _id: { eq: id } } });
|
|
11
|
+
const filter = matchDocument.$match;
|
|
12
|
+
const replaceResult = await collection.replaceOne(filter, entity);
|
|
13
|
+
if (replaceResult.matchedCount <= 0) {
|
|
14
|
+
throw new IdNotFoundError();
|
|
15
|
+
}
|
|
16
|
+
const pipeline = new NoSqlPipeline()
|
|
17
|
+
.addMatch({ filters: { _id: { eq: id } } })
|
|
18
|
+
.addOperations(operations)
|
|
19
|
+
.build();
|
|
20
|
+
const updatedEntity = await collection.aggregate(pipeline).next();
|
|
21
|
+
if (!updatedEntity) {
|
|
22
|
+
throw new IdNotFoundError();
|
|
23
|
+
}
|
|
24
|
+
return updatedEntity;
|
|
25
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { BadRequestError, IdNotFoundError } from "../../../errors/index.js";
|
|
2
|
+
import NoSqlPipeline from "../models/no-sql-pipeline.js";
|
|
3
|
+
import { buildNoSqlMatch } from "../utils/build-no-sql-match.util.js";
|
|
4
|
+
import { entityUtils } from "@loomcore/common/utils";
|
|
5
|
+
export async function partialUpdateById(db, operations, id, entity, pluralResourceName) {
|
|
6
|
+
if (!entityUtils.isValidObjectId(id)) {
|
|
7
|
+
throw new BadRequestError('id is not a valid ObjectId');
|
|
8
|
+
}
|
|
9
|
+
const collection = db.collection(pluralResourceName);
|
|
10
|
+
const matchDocument = buildNoSqlMatch({ filters: { _id: { eq: id } } });
|
|
11
|
+
const filter = matchDocument.$match;
|
|
12
|
+
const updatedEntity = await collection.findOneAndUpdate(filter, { $set: entity }, { returnDocument: 'after' });
|
|
13
|
+
if (!updatedEntity) {
|
|
14
|
+
throw new IdNotFoundError();
|
|
15
|
+
}
|
|
16
|
+
const pipeline = new NoSqlPipeline()
|
|
17
|
+
.addMatch({ filters: { _id: { eq: id } } })
|
|
18
|
+
.addOperations(operations)
|
|
19
|
+
.build();
|
|
20
|
+
const updatedEntityWithOperations = await collection.aggregate(pipeline).next();
|
|
21
|
+
if (!updatedEntityWithOperations) {
|
|
22
|
+
throw new IdNotFoundError();
|
|
23
|
+
}
|
|
24
|
+
return updatedEntityWithOperations;
|
|
25
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { Db } from "mongodb";
|
|
2
|
+
import { IQueryOptions } from "@loomcore/common/models";
|
|
3
|
+
import { Operation } from "../../operations/operation.js";
|
|
4
|
+
export declare function update<T>(db: Db, queryObject: IQueryOptions, entity: Partial<any>, operations: Operation[], pluralResourceName: string): Promise<T[]>;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { NotFoundError } from "../../../errors/index.js";
|
|
2
|
+
import NoSqlPipeline from "../models/no-sql-pipeline.js";
|
|
3
|
+
import { buildNoSqlMatch } from "../utils/build-no-sql-match.util.js";
|
|
4
|
+
export async function update(db, queryObject, entity, operations, pluralResourceName) {
|
|
5
|
+
const collection = db.collection(pluralResourceName);
|
|
6
|
+
const matchDocument = buildNoSqlMatch(queryObject);
|
|
7
|
+
const filter = matchDocument.$match;
|
|
8
|
+
const updateResult = await collection.updateMany(filter, { $set: entity });
|
|
9
|
+
if (updateResult.matchedCount <= 0) {
|
|
10
|
+
throw new NotFoundError('No records found matching update query');
|
|
11
|
+
}
|
|
12
|
+
const pipeline = new NoSqlPipeline()
|
|
13
|
+
.addMatch(queryObject)
|
|
14
|
+
.addOperations(operations)
|
|
15
|
+
.addQueryOptions(queryObject, false)
|
|
16
|
+
.build();
|
|
17
|
+
const updatedEntities = await collection.aggregate(pipeline).toArray();
|
|
18
|
+
return updatedEntities;
|
|
19
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Document } from "mongodb";
|
|
2
|
+
import { IQueryOptions, IModelSpec } from "@loomcore/common/models";
|
|
3
|
+
import { Operation } from "../../operations/operation.js";
|
|
4
|
+
import { INoSqlPipeline } from "./no-sql-pipeline.interface.js";
|
|
5
|
+
declare class NoSqlPipeline implements INoSqlPipeline {
|
|
6
|
+
private pipeline;
|
|
7
|
+
constructor(pipeline?: Document[] | null);
|
|
8
|
+
addStage(stage: Document): NoSqlPipeline;
|
|
9
|
+
addStages(stages: Document[]): NoSqlPipeline;
|
|
10
|
+
addMatch(queryOptions: IQueryOptions, modelSpec?: IModelSpec): NoSqlPipeline;
|
|
11
|
+
addOperations(operations: Operation[]): NoSqlPipeline;
|
|
12
|
+
addQueryOptions(queryOptions: IQueryOptions, pagination: boolean): NoSqlPipeline;
|
|
13
|
+
build(): Document[];
|
|
14
|
+
}
|
|
15
|
+
export default NoSqlPipeline;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Document } from "mongodb";
|
|
2
|
+
import { IQueryOptions, IModelSpec } from "@loomcore/common/models";
|
|
3
|
+
import { Operation } from "../../operations/operation.js";
|
|
4
|
+
export interface INoSqlPipeline {
|
|
5
|
+
addStage(stage: Document): INoSqlPipeline;
|
|
6
|
+
addStages(stages: Document[]): INoSqlPipeline;
|
|
7
|
+
addMatch(queryOptions: IQueryOptions, modelSpec?: IModelSpec): INoSqlPipeline;
|
|
8
|
+
addOperations(operations: Operation[]): INoSqlPipeline;
|
|
9
|
+
addQueryOptions(queryOptions: IQueryOptions, pagination: boolean): INoSqlPipeline;
|
|
10
|
+
build(): Document[];
|
|
11
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { buildNoSqlMatch, convertOperationsToPipeline, convertQueryOptionsToPipeline } from "../utils/index.js";
|
|
2
|
+
class NoSqlPipeline {
|
|
3
|
+
pipeline;
|
|
4
|
+
constructor(pipeline = null) {
|
|
5
|
+
if (pipeline) {
|
|
6
|
+
this.pipeline = [...pipeline];
|
|
7
|
+
}
|
|
8
|
+
else {
|
|
9
|
+
this.pipeline = [];
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
addStage(stage) {
|
|
13
|
+
this.pipeline.push(stage);
|
|
14
|
+
return this;
|
|
15
|
+
}
|
|
16
|
+
addStages(stages) {
|
|
17
|
+
this.pipeline = this.pipeline.concat(stages);
|
|
18
|
+
return this;
|
|
19
|
+
}
|
|
20
|
+
addMatch(queryOptions, modelSpec) {
|
|
21
|
+
const matchDocument = buildNoSqlMatch(queryOptions, modelSpec);
|
|
22
|
+
if (matchDocument) {
|
|
23
|
+
this.pipeline.push(matchDocument);
|
|
24
|
+
}
|
|
25
|
+
return this;
|
|
26
|
+
}
|
|
27
|
+
addOperations(operations) {
|
|
28
|
+
const operationsDocuments = convertOperationsToPipeline(operations);
|
|
29
|
+
if (operationsDocuments.length > 0) {
|
|
30
|
+
this.pipeline = this.pipeline.concat(operationsDocuments);
|
|
31
|
+
}
|
|
32
|
+
return this;
|
|
33
|
+
}
|
|
34
|
+
addQueryOptions(queryOptions, pagination) {
|
|
35
|
+
const queryOptionsDocuments = convertQueryOptionsToPipeline(queryOptions, pagination);
|
|
36
|
+
this.pipeline = this.pipeline.concat(queryOptionsDocuments);
|
|
37
|
+
return this;
|
|
38
|
+
}
|
|
39
|
+
build() {
|
|
40
|
+
return this.pipeline;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
export default NoSqlPipeline;
|