@loomcore/api 0.0.59 → 0.1.0
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/mongo-test-entity.model.d.ts +11 -0
- package/dist/__tests__/models/mongo-test-entity.model.js +13 -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-mongo-db.d.ts +14 -0
- package/dist/__tests__/test-mongo-db.js +81 -0
- package/dist/__tests__/test-objects.d.ts +23 -0
- package/dist/__tests__/test-objects.js +45 -0
- package/dist/__tests__/test-user.d.ts +3 -0
- package/dist/__tests__/test-user.js +16 -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/batch-update.command.d.ts +3 -0
- package/dist/databases/mongo-db/commands/batch-update.command.js +41 -0
- package/dist/databases/mongo-db/commands/create-many.command.d.ts +5 -0
- package/dist/databases/mongo-db/commands/create-many.command.js +17 -0
- package/dist/databases/mongo-db/commands/create.command.d.ts +5 -0
- package/dist/databases/mongo-db/commands/create.command.js +17 -0
- package/dist/databases/mongo-db/commands/delete-by-id.command.d.ts +3 -0
- package/dist/databases/mongo-db/commands/delete-by-id.command.js +9 -0
- package/dist/databases/mongo-db/commands/delete-many.command.d.ts +4 -0
- package/dist/databases/mongo-db/commands/delete-many.command.js +9 -0
- package/dist/databases/mongo-db/commands/full-updateby-id.command.d.ts +3 -0
- package/dist/databases/mongo-db/commands/full-updateby-id.command.js +21 -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/commands/partial-update-by-id.command.d.ts +3 -0
- package/dist/databases/mongo-db/commands/partial-update-by-id.command.js +21 -0
- package/dist/databases/mongo-db/commands/update.command.d.ts +4 -0
- package/dist/databases/mongo-db/commands/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/find-one.query.d.ts +3 -0
- package/dist/databases/mongo-db/queries/find-one.query.js +9 -0
- package/dist/databases/mongo-db/queries/find.query.d.ts +3 -0
- package/dist/databases/mongo-db/queries/find.query.js +9 -0
- package/dist/databases/mongo-db/queries/get-all.query.d.ts +3 -0
- package/dist/databases/mongo-db/queries/get-all.query.js +17 -0
- package/dist/databases/mongo-db/queries/get-by-id.query.d.ts +3 -0
- package/dist/databases/mongo-db/queries/get-by-id.query.js +20 -0
- package/dist/databases/mongo-db/queries/get-count.query.d.ts +2 -0
- package/dist/databases/mongo-db/queries/get-count.query.js +5 -0
- package/dist/databases/mongo-db/queries/get.query.d.ts +4 -0
- package/dist/databases/mongo-db/queries/get.query.js +14 -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.d.ts +6 -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/migration.js +14 -0
- package/dist/databases/postgres/migrations/runMigrations.d.ts +2 -0
- package/dist/databases/postgres/migrations/runMigrations.js +20 -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/database-to-idatabase.util.d.ts +3 -0
- package/dist/databases/utils/database-to-idatabase.util.js +14 -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.d.ts +9 -0
- package/dist/models/refresh-token.js +2 -0
- package/dist/models/refresh-token.model.d.ts +18 -0
- package/dist/models/refresh-token.model.js +13 -0
- package/dist/models/refresh-token.spec.d.ts +1 -0
- package/dist/models/refresh-token.spec.js +12 -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/tsconfig.tsbuildinfo +1 -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/dist/utils/sql.db.utils.d.ts +14 -0
- package/dist/utils/sql.db.utils.js +94 -0
- package/package.json +4 -2
- 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
|
@@ -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,21 @@
|
|
|
1
|
+
import { 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
|
+
export async function partialUpdateById(db, operations, id, entity, pluralResourceName) {
|
|
5
|
+
const collection = db.collection(pluralResourceName);
|
|
6
|
+
const matchDocument = buildNoSqlMatch({ filters: { _id: { eq: id } } });
|
|
7
|
+
const filter = matchDocument.$match;
|
|
8
|
+
const updatedEntity = await collection.findOneAndUpdate(filter, { $set: entity }, { returnDocument: 'after' });
|
|
9
|
+
if (!updatedEntity) {
|
|
10
|
+
throw new IdNotFoundError();
|
|
11
|
+
}
|
|
12
|
+
const pipeline = new NoSqlPipeline()
|
|
13
|
+
.addMatch({ filters: { _id: { eq: id } } })
|
|
14
|
+
.addOperations(operations)
|
|
15
|
+
.build();
|
|
16
|
+
const updatedEntityWithOperations = await collection.aggregate(pipeline).next();
|
|
17
|
+
if (!updatedEntityWithOperations) {
|
|
18
|
+
throw new IdNotFoundError();
|
|
19
|
+
}
|
|
20
|
+
return updatedEntityWithOperations;
|
|
21
|
+
}
|
|
@@ -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;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { Db } from "mongodb";
|
|
2
|
+
import { IModelSpec, IQueryOptions, IPagedResult, IEntity } from "@loomcore/common/models";
|
|
3
|
+
import { Operation } from "../operations/operation.js";
|
|
4
|
+
import { DeleteResult as GenericDeleteResult } from "../models/delete-result.js";
|
|
5
|
+
import { TSchema } from "@sinclair/typebox";
|
|
6
|
+
import { IDatabase } from "../models/database.interface.js";
|
|
7
|
+
export declare class MongoDBDatabase implements IDatabase {
|
|
8
|
+
private db;
|
|
9
|
+
constructor(db: Db);
|
|
10
|
+
preprocessEntity<T extends IEntity>(entity: Partial<T>, schema: TSchema): Partial<T>;
|
|
11
|
+
postprocessEntity<T extends IEntity>(single: T, schema: TSchema): T;
|
|
12
|
+
getAll<T extends IEntity>(operations: Operation[], pluralResourceName: string): Promise<T[]>;
|
|
13
|
+
get<T extends IEntity>(operations: Operation[], queryOptions: IQueryOptions, modelSpec: IModelSpec, pluralResourceName: string): Promise<IPagedResult<T>>;
|
|
14
|
+
getById<T extends IEntity>(operations: Operation[], queryObject: IQueryOptions, id: string, pluralResourceName: string): Promise<T | null>;
|
|
15
|
+
getCount(pluralResourceName: string): Promise<number>;
|
|
16
|
+
create<T extends IEntity>(entity: Partial<T>, pluralResourceName: string): Promise<{
|
|
17
|
+
insertedId: string;
|
|
18
|
+
entity: T;
|
|
19
|
+
}>;
|
|
20
|
+
createMany<T extends IEntity>(entities: Partial<T>[], pluralResourceName: string): Promise<{
|
|
21
|
+
insertedIds: string[];
|
|
22
|
+
entities: T[];
|
|
23
|
+
}>;
|
|
24
|
+
batchUpdate<T extends IEntity>(entities: Partial<T>[], operations: Operation[], queryObject: IQueryOptions, pluralResourceName: string): Promise<T[]>;
|
|
25
|
+
fullUpdateById<T extends IEntity>(operations: Operation[], id: string, entity: Partial<T>, pluralResourceName: string): Promise<T>;
|
|
26
|
+
partialUpdateById<T extends IEntity>(operations: Operation[], id: string, entity: Partial<T>, pluralResourceName: string): Promise<T>;
|
|
27
|
+
update<T extends IEntity>(queryObject: IQueryOptions, entity: Partial<T>, operations: Operation[], pluralResourceName: string): Promise<T[]>;
|
|
28
|
+
deleteById(id: string, pluralResourceName: string): Promise<GenericDeleteResult>;
|
|
29
|
+
deleteMany(queryObject: IQueryOptions, pluralResourceName: string): Promise<GenericDeleteResult>;
|
|
30
|
+
find<T extends IEntity>(queryObject: IQueryOptions, pluralResourceName: string): Promise<T[]>;
|
|
31
|
+
findOne<T extends IEntity>(queryObject: IQueryOptions, pluralResourceName: string): Promise<T | null>;
|
|
32
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { convertObjectIdsToStrings, convertStringsToObjectIds } from "./utils/index.js";
|
|
2
|
+
import { create, createMany, batchUpdate, fullUpdateById, partialUpdateById, update, deleteById, deleteMany } from "./commands/index.js";
|
|
3
|
+
import { getAll, get, getById, getCount, find, findOne } from "./queries/index.js";
|
|
4
|
+
import { entityUtils } from "@loomcore/common/utils";
|
|
5
|
+
import { BadRequestError } from "../../errors/bad-request.error.js";
|
|
6
|
+
export class MongoDBDatabase {
|
|
7
|
+
db;
|
|
8
|
+
constructor(db) {
|
|
9
|
+
this.db = db;
|
|
10
|
+
}
|
|
11
|
+
preprocessEntity(entity, schema) {
|
|
12
|
+
if (entity._id && !entityUtils.isValidObjectId(entity._id)) {
|
|
13
|
+
throw new BadRequestError('id is not a valid ObjectId');
|
|
14
|
+
}
|
|
15
|
+
return convertStringsToObjectIds(entity, schema);
|
|
16
|
+
}
|
|
17
|
+
postprocessEntity(single, schema) {
|
|
18
|
+
if (!single)
|
|
19
|
+
return single;
|
|
20
|
+
return convertObjectIdsToStrings(single);
|
|
21
|
+
}
|
|
22
|
+
async getAll(operations, pluralResourceName) {
|
|
23
|
+
return getAll(this.db, operations, pluralResourceName);
|
|
24
|
+
}
|
|
25
|
+
async get(operations, queryOptions, modelSpec, pluralResourceName) {
|
|
26
|
+
return get(this.db, operations, queryOptions, modelSpec, pluralResourceName);
|
|
27
|
+
}
|
|
28
|
+
async getById(operations, queryObject, id, pluralResourceName) {
|
|
29
|
+
return getById(this.db, operations, queryObject, id, pluralResourceName);
|
|
30
|
+
}
|
|
31
|
+
async getCount(pluralResourceName) {
|
|
32
|
+
return getCount(this.db, pluralResourceName);
|
|
33
|
+
}
|
|
34
|
+
async create(entity, pluralResourceName) {
|
|
35
|
+
return create(this.db, pluralResourceName, entity);
|
|
36
|
+
}
|
|
37
|
+
async createMany(entities, pluralResourceName) {
|
|
38
|
+
return createMany(this.db, pluralResourceName, entities);
|
|
39
|
+
}
|
|
40
|
+
async batchUpdate(entities, operations, queryObject, pluralResourceName) {
|
|
41
|
+
return batchUpdate(this.db, entities, operations, queryObject, pluralResourceName);
|
|
42
|
+
}
|
|
43
|
+
async fullUpdateById(operations, id, entity, pluralResourceName) {
|
|
44
|
+
return fullUpdateById(this.db, operations, id, entity, pluralResourceName);
|
|
45
|
+
}
|
|
46
|
+
async partialUpdateById(operations, id, entity, pluralResourceName) {
|
|
47
|
+
return partialUpdateById(this.db, operations, id, entity, pluralResourceName);
|
|
48
|
+
}
|
|
49
|
+
async update(queryObject, entity, operations, pluralResourceName) {
|
|
50
|
+
return update(this.db, queryObject, entity, operations, pluralResourceName);
|
|
51
|
+
}
|
|
52
|
+
async deleteById(id, pluralResourceName) {
|
|
53
|
+
return deleteById(this.db, id, pluralResourceName);
|
|
54
|
+
}
|
|
55
|
+
async deleteMany(queryObject, pluralResourceName) {
|
|
56
|
+
return deleteMany(this.db, queryObject, pluralResourceName);
|
|
57
|
+
}
|
|
58
|
+
async find(queryObject, pluralResourceName) {
|
|
59
|
+
return find(this.db, queryObject, pluralResourceName);
|
|
60
|
+
}
|
|
61
|
+
async findOne(queryObject, pluralResourceName) {
|
|
62
|
+
return findOne(this.db, queryObject, pluralResourceName);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { buildNoSqlMatch, buildFindOptions } from "../utils/index.js";
|
|
2
|
+
export async function findOne(db, queryObject, pluralResourceName) {
|
|
3
|
+
const collection = db.collection(pluralResourceName);
|
|
4
|
+
const matchDocument = buildNoSqlMatch(queryObject);
|
|
5
|
+
const filter = matchDocument.$match;
|
|
6
|
+
const options = buildFindOptions(queryObject);
|
|
7
|
+
const entity = await collection.findOne(filter, options);
|
|
8
|
+
return entity;
|
|
9
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { buildNoSqlMatch, buildFindOptions } from "../utils/index.js";
|
|
2
|
+
export async function find(db, queryObject, pluralResourceName) {
|
|
3
|
+
const collection = db.collection(pluralResourceName);
|
|
4
|
+
const matchDocument = buildNoSqlMatch(queryObject);
|
|
5
|
+
const filter = matchDocument.$match;
|
|
6
|
+
const options = buildFindOptions(queryObject);
|
|
7
|
+
const entities = await collection.find(filter, options).toArray();
|
|
8
|
+
return entities;
|
|
9
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import NoSqlPipeline from "../models/no-sql-pipeline.js";
|
|
2
|
+
export async function getAll(db, operations, pluralResourceName) {
|
|
3
|
+
const collection = db.collection(pluralResourceName);
|
|
4
|
+
const pipeline = new NoSqlPipeline()
|
|
5
|
+
.addOperations(operations)
|
|
6
|
+
.build();
|
|
7
|
+
let aggregateResult;
|
|
8
|
+
if (pipeline.length === 0) {
|
|
9
|
+
const cursor = collection.find();
|
|
10
|
+
aggregateResult = await cursor.toArray();
|
|
11
|
+
}
|
|
12
|
+
else {
|
|
13
|
+
const cursor = collection.aggregate(pipeline);
|
|
14
|
+
aggregateResult = await cursor.toArray();
|
|
15
|
+
}
|
|
16
|
+
return aggregateResult;
|
|
17
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { ObjectId } from "mongodb";
|
|
2
|
+
import { convertOperationsToPipeline } from "../utils/index.js";
|
|
3
|
+
export async function getById(db, operations, id, pluralResourceName) {
|
|
4
|
+
const collection = db.collection(pluralResourceName);
|
|
5
|
+
const objectId = new ObjectId(id);
|
|
6
|
+
const baseQuery = { _id: objectId };
|
|
7
|
+
const operationsDocuments = convertOperationsToPipeline(operations);
|
|
8
|
+
let entity = null;
|
|
9
|
+
if (operationsDocuments.length > 0) {
|
|
10
|
+
const pipeline = [
|
|
11
|
+
{ $match: baseQuery },
|
|
12
|
+
...operationsDocuments
|
|
13
|
+
];
|
|
14
|
+
entity = await collection.aggregate(pipeline).next();
|
|
15
|
+
}
|
|
16
|
+
else {
|
|
17
|
+
entity = await collection.findOne(baseQuery);
|
|
18
|
+
}
|
|
19
|
+
return entity;
|
|
20
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { Db } from "mongodb";
|
|
2
|
+
import { IModelSpec, IQueryOptions, IPagedResult } from "@loomcore/common/models";
|
|
3
|
+
import { Operation } from "../../operations/operation.js";
|
|
4
|
+
export declare function get<T>(db: Db, operations: Operation[], queryOptions: IQueryOptions, modelSpec: IModelSpec, pluralResourceName: string): Promise<IPagedResult<T>>;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import NoSqlPipeline from "../models/no-sql-pipeline.js";
|
|
2
|
+
import { apiUtils } from "../../../utils/api.utils.js";
|
|
3
|
+
export async function get(db, operations, queryOptions, modelSpec, pluralResourceName) {
|
|
4
|
+
const collection = db.collection(pluralResourceName);
|
|
5
|
+
const pipeline = new NoSqlPipeline()
|
|
6
|
+
.addMatch(queryOptions, modelSpec)
|
|
7
|
+
.addOperations(operations)
|
|
8
|
+
.addQueryOptions(queryOptions, true)
|
|
9
|
+
.build();
|
|
10
|
+
const cursor = collection.aggregate(pipeline);
|
|
11
|
+
const aggregateResult = await cursor.next();
|
|
12
|
+
const pagedResult = apiUtils.getPagedResult(aggregateResult?.data || [], aggregateResult?.total || 0, queryOptions);
|
|
13
|
+
return pagedResult;
|
|
14
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { buildNoSqlMatch, buildFindOptions } from "../utils/index.js";
|
|
2
|
+
export async function findOne(db, queryObject, pluralResourceName) {
|
|
3
|
+
const collection = db.collection(pluralResourceName);
|
|
4
|
+
const matchDocument = buildNoSqlMatch(queryObject);
|
|
5
|
+
const filter = matchDocument.$match;
|
|
6
|
+
const options = buildFindOptions(queryObject);
|
|
7
|
+
const entity = await collection.findOne(filter, options);
|
|
8
|
+
return entity;
|
|
9
|
+
}
|