@loomcore/api 0.0.58 → 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.
Files changed (271) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +50 -0
  3. package/dist/__tests__/common-test.utils.d.ts +29 -60
  4. package/dist/__tests__/common-test.utils.js +88 -166
  5. package/dist/__tests__/index.d.ts +6 -0
  6. package/dist/__tests__/index.js +6 -0
  7. package/dist/__tests__/models/category.model.d.ts +8 -0
  8. package/dist/__tests__/models/category.model.js +6 -0
  9. package/dist/__tests__/models/mongo-test-entity.model.d.ts +11 -0
  10. package/dist/__tests__/models/mongo-test-entity.model.js +13 -0
  11. package/dist/__tests__/models/product.model.d.ts +17 -0
  12. package/dist/__tests__/models/product.model.js +10 -0
  13. package/dist/__tests__/models/test-entity.model.d.ts +11 -0
  14. package/dist/__tests__/models/test-entity.model.js +10 -0
  15. package/dist/__tests__/models/test-item.model.d.ts +12 -0
  16. package/dist/__tests__/models/test-item.model.js +9 -0
  17. package/dist/__tests__/mongo-db.test-database.d.ts +15 -0
  18. package/dist/__tests__/mongo-db.test-database.js +74 -0
  19. package/dist/__tests__/postgres-test-migrations/001-create-test-entities-table.migration.d.ts +11 -0
  20. package/dist/__tests__/postgres-test-migrations/001-create-test-entities-table.migration.js +59 -0
  21. package/dist/__tests__/postgres-test-migrations/002-create-categories-table.migration.d.ts +11 -0
  22. package/dist/__tests__/postgres-test-migrations/002-create-categories-table.migration.js +52 -0
  23. package/dist/__tests__/postgres-test-migrations/003-create-products-table.migration.d.ts +11 -0
  24. package/dist/__tests__/postgres-test-migrations/003-create-products-table.migration.js +62 -0
  25. package/dist/__tests__/postgres-test-migrations/004-create-test-users-table.migration.d.ts +11 -0
  26. package/dist/__tests__/postgres-test-migrations/004-create-test-users-table.migration.js +66 -0
  27. package/dist/__tests__/postgres-test-migrations/005-create-test-items-table.migration.d.ts +11 -0
  28. package/dist/__tests__/postgres-test-migrations/005-create-test-items-table.migration.js +50 -0
  29. package/dist/__tests__/postgres-test-migrations/run-test-migrations.d.ts +2 -0
  30. package/dist/__tests__/postgres-test-migrations/run-test-migrations.js +22 -0
  31. package/dist/__tests__/postgres.test-database.d.ts +13 -0
  32. package/dist/__tests__/postgres.test-database.js +85 -0
  33. package/dist/__tests__/test-database.interface.d.ts +7 -0
  34. package/dist/__tests__/test-express-app.d.ts +9 -7
  35. package/dist/__tests__/test-express-app.js +38 -48
  36. package/dist/__tests__/test-mongo-db.d.ts +14 -0
  37. package/dist/__tests__/test-mongo-db.js +81 -0
  38. package/dist/__tests__/test-objects.d.ts +23 -0
  39. package/dist/__tests__/test-objects.js +45 -0
  40. package/dist/__tests__/test-user.d.ts +3 -0
  41. package/dist/__tests__/test-user.js +16 -0
  42. package/dist/config/base-api-config.d.ts +2 -2
  43. package/dist/config/base-api-config.js +2 -2
  44. package/dist/controllers/api.controller.d.ts +1 -5
  45. package/dist/controllers/api.controller.js +4 -11
  46. package/dist/controllers/auth.controller.d.ts +2 -2
  47. package/dist/controllers/auth.controller.js +4 -5
  48. package/dist/controllers/organizations.controller.d.ts +2 -2
  49. package/dist/controllers/organizations.controller.js +4 -4
  50. package/dist/controllers/users.controller.d.ts +2 -2
  51. package/dist/controllers/users.controller.js +2 -2
  52. package/dist/databases/index.d.ts +1 -0
  53. package/dist/databases/index.js +1 -0
  54. package/dist/databases/models/constants.d.ts +1 -0
  55. package/dist/databases/models/constants.js +1 -0
  56. package/dist/databases/models/database.d.ts +3 -0
  57. package/dist/databases/models/database.interface.d.ts +28 -0
  58. package/dist/databases/models/delete-result.d.ts +5 -0
  59. package/dist/databases/models/delete-result.js +8 -0
  60. package/dist/databases/models/index.d.ts +5 -0
  61. package/dist/databases/models/index.js +5 -0
  62. package/dist/databases/models/update-result.d.ts +5 -0
  63. package/dist/databases/models/update-result.js +8 -0
  64. package/dist/databases/mongo-db/commands/batch-update.command.d.ts +3 -0
  65. package/dist/databases/mongo-db/commands/batch-update.command.js +41 -0
  66. package/dist/databases/mongo-db/commands/create-many.command.d.ts +5 -0
  67. package/dist/databases/mongo-db/commands/create-many.command.js +17 -0
  68. package/dist/databases/mongo-db/commands/create.command.d.ts +5 -0
  69. package/dist/databases/mongo-db/commands/create.command.js +17 -0
  70. package/dist/databases/mongo-db/commands/delete-by-id.command.d.ts +3 -0
  71. package/dist/databases/mongo-db/commands/delete-by-id.command.js +9 -0
  72. package/dist/databases/mongo-db/commands/delete-many.command.d.ts +4 -0
  73. package/dist/databases/mongo-db/commands/delete-many.command.js +9 -0
  74. package/dist/databases/mongo-db/commands/full-updateby-id.command.d.ts +3 -0
  75. package/dist/databases/mongo-db/commands/full-updateby-id.command.js +21 -0
  76. package/dist/databases/mongo-db/commands/index.d.ts +8 -0
  77. package/dist/databases/mongo-db/commands/index.js +8 -0
  78. package/dist/databases/mongo-db/commands/mongo-batch-update.command.d.ts +4 -0
  79. package/dist/databases/mongo-db/commands/mongo-batch-update.command.js +41 -0
  80. package/dist/databases/mongo-db/commands/mongo-create-many.command.d.ts +5 -0
  81. package/dist/databases/mongo-db/commands/mongo-create-many.command.js +17 -0
  82. package/dist/databases/mongo-db/commands/mongo-create.command.d.ts +5 -0
  83. package/dist/databases/mongo-db/commands/mongo-create.command.js +17 -0
  84. package/dist/databases/mongo-db/commands/mongo-delete-by-id.command.d.ts +3 -0
  85. package/dist/databases/mongo-db/commands/mongo-delete-by-id.command.js +14 -0
  86. package/dist/databases/mongo-db/commands/mongo-delete-many.command.d.ts +4 -0
  87. package/dist/databases/mongo-db/commands/mongo-delete-many.command.js +9 -0
  88. package/dist/databases/mongo-db/commands/mongo-full-updateby-id.command.d.ts +3 -0
  89. package/dist/databases/mongo-db/commands/mongo-full-updateby-id.command.js +25 -0
  90. package/dist/databases/mongo-db/commands/mongo-partial-update-by-id.command.d.ts +3 -0
  91. package/dist/databases/mongo-db/commands/mongo-partial-update-by-id.command.js +25 -0
  92. package/dist/databases/mongo-db/commands/mongo-update.command.d.ts +4 -0
  93. package/dist/databases/mongo-db/commands/mongo-update.command.js +19 -0
  94. package/dist/databases/mongo-db/commands/partial-update-by-id.command.d.ts +3 -0
  95. package/dist/databases/mongo-db/commands/partial-update-by-id.command.js +21 -0
  96. package/dist/databases/mongo-db/commands/update.command.d.ts +4 -0
  97. package/dist/databases/mongo-db/commands/update.command.js +19 -0
  98. package/dist/databases/mongo-db/index.d.ts +4 -0
  99. package/dist/databases/mongo-db/index.js +4 -0
  100. package/dist/databases/mongo-db/models/no-sql-pipeline.d.ts +15 -0
  101. package/dist/databases/mongo-db/models/no-sql-pipeline.interface.d.ts +11 -0
  102. package/dist/databases/mongo-db/models/no-sql-pipeline.js +43 -0
  103. package/dist/databases/mongo-db/mongo-db.database.d.ts +32 -0
  104. package/dist/databases/mongo-db/mongo-db.database.js +65 -0
  105. package/dist/databases/mongo-db/queries/find-one.query.d.ts +3 -0
  106. package/dist/databases/mongo-db/queries/find-one.query.js +9 -0
  107. package/dist/databases/mongo-db/queries/find.query.d.ts +3 -0
  108. package/dist/databases/mongo-db/queries/find.query.js +9 -0
  109. package/dist/databases/mongo-db/queries/get-all.query.d.ts +3 -0
  110. package/dist/databases/mongo-db/queries/get-all.query.js +17 -0
  111. package/dist/databases/mongo-db/queries/get-by-id.query.d.ts +3 -0
  112. package/dist/databases/mongo-db/queries/get-by-id.query.js +20 -0
  113. package/dist/databases/mongo-db/queries/get-count.query.d.ts +2 -0
  114. package/dist/databases/mongo-db/queries/get-count.query.js +5 -0
  115. package/dist/databases/mongo-db/queries/get.query.d.ts +4 -0
  116. package/dist/databases/mongo-db/queries/get.query.js +14 -0
  117. package/dist/databases/mongo-db/queries/index.d.ts +6 -0
  118. package/dist/databases/mongo-db/queries/index.js +6 -0
  119. package/dist/databases/mongo-db/queries/mongo-find-one.query.d.ts +3 -0
  120. package/dist/databases/mongo-db/queries/mongo-find-one.query.js +9 -0
  121. package/dist/databases/mongo-db/queries/mongo-find.query.d.ts +3 -0
  122. package/dist/databases/mongo-db/queries/mongo-find.query.js +9 -0
  123. package/dist/databases/mongo-db/queries/mongo-get-all.query.d.ts +3 -0
  124. package/dist/databases/mongo-db/queries/mongo-get-all.query.js +17 -0
  125. package/dist/databases/mongo-db/queries/mongo-get-by-id.query.d.ts +4 -0
  126. package/dist/databases/mongo-db/queries/mongo-get-by-id.query.js +17 -0
  127. package/dist/databases/mongo-db/queries/mongo-get-count.query.d.ts +2 -0
  128. package/dist/databases/mongo-db/queries/mongo-get-count.query.js +5 -0
  129. package/dist/databases/mongo-db/queries/mongo-get.query.d.ts +4 -0
  130. package/dist/databases/mongo-db/queries/mongo-get.query.js +14 -0
  131. package/dist/databases/mongo-db/utils/build-find-options.util.d.ts +3 -0
  132. package/dist/databases/mongo-db/utils/build-find-options.util.js +15 -0
  133. package/dist/databases/mongo-db/utils/build-no-sql-match.util.d.ts +3 -0
  134. package/dist/databases/mongo-db/utils/build-no-sql-match.util.js +59 -0
  135. package/dist/databases/mongo-db/utils/convert-object-ids-to-strings.util.d.ts +1 -0
  136. package/dist/databases/mongo-db/utils/convert-object-ids-to-strings.util.js +32 -0
  137. package/dist/databases/mongo-db/utils/convert-operations-to-pipeline.util.d.ts +3 -0
  138. package/dist/databases/mongo-db/utils/convert-operations-to-pipeline.util.js +68 -0
  139. package/dist/databases/mongo-db/utils/convert-query-options-to-pipeline.util.d.ts +3 -0
  140. package/dist/databases/mongo-db/utils/convert-query-options-to-pipeline.util.js +31 -0
  141. package/dist/databases/mongo-db/utils/convert-strings-to-object-ids.util.d.ts +3 -0
  142. package/dist/databases/mongo-db/utils/convert-strings-to-object-ids.util.js +72 -0
  143. package/dist/databases/mongo-db/utils/index.d.ts +7 -0
  144. package/dist/databases/mongo-db/utils/index.js +7 -0
  145. package/dist/databases/operations/join.operation.d.ts +7 -0
  146. package/dist/databases/operations/join.operation.js +12 -0
  147. package/dist/databases/operations/operation.d.ts +2 -0
  148. package/dist/databases/postgres/commands/postgres-batch-update.command.d.ts +4 -0
  149. package/dist/databases/postgres/commands/postgres-batch-update.command.js +56 -0
  150. package/dist/databases/postgres/commands/postgres-create-many.command.d.ts +6 -0
  151. package/dist/databases/postgres/commands/postgres-create-many.command.js +63 -0
  152. package/dist/databases/postgres/commands/postgres-create.command.d.ts +6 -0
  153. package/dist/databases/postgres/commands/postgres-create.command.js +29 -0
  154. package/dist/databases/postgres/commands/postgres-delete-by-id.command.d.ts +3 -0
  155. package/dist/databases/postgres/commands/postgres-delete-by-id.command.js +6 -0
  156. package/dist/databases/postgres/commands/postgres-delete-many.command.d.ts +4 -0
  157. package/dist/databases/postgres/commands/postgres-delete-many.command.js +13 -0
  158. package/dist/databases/postgres/commands/postgres-full-update-by-id.command.d.ts +4 -0
  159. package/dist/databases/postgres/commands/postgres-full-update-by-id.command.js +72 -0
  160. package/dist/databases/postgres/commands/postgres-partial-update-by-id.command.d.ts +4 -0
  161. package/dist/databases/postgres/commands/postgres-partial-update-by-id.command.js +42 -0
  162. package/dist/databases/postgres/commands/postgres-update.command.d.ts +5 -0
  163. package/dist/databases/postgres/commands/postgres-update.command.js +48 -0
  164. package/dist/databases/postgres/migrations/001-create-migrations-table.migration.d.ts +11 -0
  165. package/dist/databases/postgres/migrations/001-create-migrations-table.migration.js +52 -0
  166. package/dist/databases/postgres/migrations/002-create-organizations-table.migration.d.ts +11 -0
  167. package/dist/databases/postgres/migrations/002-create-organizations-table.migration.js +55 -0
  168. package/dist/databases/postgres/migrations/003-create-users-table.migration.d.ts +11 -0
  169. package/dist/databases/postgres/migrations/003-create-users-table.migration.js +65 -0
  170. package/dist/databases/postgres/migrations/004-create-refresh-token-table.migration.d.ts +11 -0
  171. package/dist/databases/postgres/migrations/004-create-refresh-token-table.migration.js +57 -0
  172. package/dist/databases/postgres/migrations/index.d.ts +3 -0
  173. package/dist/databases/postgres/migrations/index.js +3 -0
  174. package/dist/databases/postgres/migrations/migration.d.ts +6 -0
  175. package/dist/databases/postgres/migrations/migration.interface.d.ts +6 -0
  176. package/dist/databases/postgres/migrations/migration.interface.js +1 -0
  177. package/dist/databases/postgres/migrations/migration.js +14 -0
  178. package/dist/databases/postgres/migrations/runMigrations.d.ts +2 -0
  179. package/dist/databases/postgres/migrations/runMigrations.js +20 -0
  180. package/dist/databases/postgres/migrations/setup-for-auth.migration.d.ts +2 -0
  181. package/dist/databases/postgres/migrations/setup-for-auth.migration.js +18 -0
  182. package/dist/databases/postgres/migrations/setup-for-multitenant.migration.d.ts +2 -0
  183. package/dist/databases/postgres/migrations/setup-for-multitenant.migration.js +16 -0
  184. package/dist/databases/postgres/postgres.database.d.ts +31 -0
  185. package/dist/databases/postgres/postgres.database.js +69 -0
  186. package/dist/databases/postgres/queries/postgres-find-one.query.d.ts +3 -0
  187. package/dist/databases/postgres/queries/postgres-find-one.query.js +13 -0
  188. package/dist/databases/postgres/queries/postgres-find.query.d.ts +3 -0
  189. package/dist/databases/postgres/queries/postgres-find.query.js +11 -0
  190. package/dist/databases/postgres/queries/postgres-get-all.query.d.ts +3 -0
  191. package/dist/databases/postgres/queries/postgres-get-all.query.js +14 -0
  192. package/dist/databases/postgres/queries/postgres-get-by-id.query.d.ts +4 -0
  193. package/dist/databases/postgres/queries/postgres-get-by-id.query.js +26 -0
  194. package/dist/databases/postgres/queries/postgres-get-count.query.d.ts +2 -0
  195. package/dist/databases/postgres/queries/postgres-get-count.query.js +4 -0
  196. package/dist/databases/postgres/queries/postgres-get.query.d.ts +4 -0
  197. package/dist/databases/postgres/queries/postgres-get.query.js +26 -0
  198. package/dist/databases/postgres/utils/build-count-query.d.ts +3 -0
  199. package/dist/databases/postgres/utils/build-count-query.js +7 -0
  200. package/dist/databases/postgres/utils/build-join-clauses.d.ts +2 -0
  201. package/dist/databases/postgres/utils/build-join-clauses.js +12 -0
  202. package/dist/databases/postgres/utils/build-order-by-clause.d.ts +2 -0
  203. package/dist/databases/postgres/utils/build-order-by-clause.js +8 -0
  204. package/dist/databases/postgres/utils/build-pagination-clause.d.ts +2 -0
  205. package/dist/databases/postgres/utils/build-pagination-clause.js +9 -0
  206. package/dist/databases/postgres/utils/build-select-clause.d.ts +3 -0
  207. package/dist/databases/postgres/utils/build-select-clause.js +28 -0
  208. package/dist/databases/postgres/utils/build-where-clause.d.ts +5 -0
  209. package/dist/databases/postgres/utils/build-where-clause.js +50 -0
  210. package/dist/databases/postgres/utils/columns-and-values-from-entity.d.ts +5 -0
  211. package/dist/databases/postgres/utils/columns-and-values-from-entity.js +9 -0
  212. package/dist/databases/postgres/utils/convert-null-to-undefined.util.d.ts +2 -0
  213. package/dist/databases/postgres/utils/convert-null-to-undefined.util.js +70 -0
  214. package/dist/databases/postgres/utils/transform-join-results.d.ts +2 -0
  215. package/dist/databases/postgres/utils/transform-join-results.js +33 -0
  216. package/dist/databases/utils/database-to-idatabase.util.d.ts +3 -0
  217. package/dist/databases/utils/database-to-idatabase.util.js +14 -0
  218. package/dist/databases/utils/get-property-schema.util.d.ts +2 -0
  219. package/dist/databases/utils/get-property-schema.util.js +15 -0
  220. package/dist/databases/utils/index.d.ts +1 -0
  221. package/dist/databases/utils/index.js +1 -0
  222. package/dist/models/base-api-config.interface.d.ts +3 -2
  223. package/dist/models/index.d.ts +1 -1
  224. package/dist/models/index.js +1 -1
  225. package/dist/models/refresh-token.d.ts +9 -0
  226. package/dist/models/refresh-token.js +2 -0
  227. package/dist/models/refresh-token.model.d.ts +18 -0
  228. package/dist/models/refresh-token.model.js +13 -0
  229. package/dist/models/refresh-token.spec.d.ts +1 -0
  230. package/dist/models/refresh-token.spec.js +12 -0
  231. package/dist/services/auth.service.d.ts +11 -18
  232. package/dist/services/auth.service.js +29 -50
  233. package/dist/services/generic-api-service/generic-api-service.interface.d.ts +29 -0
  234. package/dist/services/generic-api-service/generic-api-service.interface.js +1 -0
  235. package/dist/services/generic-api-service/generic-api.service.d.ts +37 -0
  236. package/dist/services/generic-api-service/generic-api.service.js +178 -0
  237. package/dist/services/index.d.ts +2 -2
  238. package/dist/services/index.js +2 -2
  239. package/dist/services/multi-tenant-api.service.d.ts +9 -6
  240. package/dist/services/multi-tenant-api.service.js +10 -23
  241. package/dist/services/organization.service.d.ts +5 -5
  242. package/dist/services/organization.service.js +9 -6
  243. package/dist/services/password-reset-token.service.d.ts +3 -3
  244. package/dist/services/password-reset-token.service.js +5 -5
  245. package/dist/services/tenant-query-decorator.d.ts +1 -1
  246. package/dist/services/tenant-query-decorator.js +1 -1
  247. package/dist/services/user.service.d.ts +4 -6
  248. package/dist/services/user.service.js +4 -10
  249. package/dist/services/utils/audit-for-create.util.d.ts +2 -0
  250. package/dist/services/utils/audit-for-create.util.js +9 -0
  251. package/dist/services/utils/audit-for-update.util.d.ts +2 -0
  252. package/dist/services/utils/audit-for-update.util.js +7 -0
  253. package/dist/services/utils/strip-sender-provided-system-properties.util.d.ts +2 -0
  254. package/dist/services/utils/strip-sender-provided-system-properties.util.js +15 -0
  255. package/dist/tsconfig.tsbuildinfo +1 -0
  256. package/dist/utils/api.utils.js +2 -1
  257. package/dist/utils/index.d.ts +0 -1
  258. package/dist/utils/index.js +0 -1
  259. package/dist/utils/sql.db.utils.d.ts +14 -0
  260. package/dist/utils/sql.db.utils.js +94 -0
  261. package/package.json +4 -2
  262. package/dist/services/generic-api-service.interface.d.ts +0 -27
  263. package/dist/services/generic-api.service.d.ts +0 -50
  264. package/dist/services/generic-api.service.js +0 -424
  265. package/dist/utils/db.utils.d.ts +0 -27
  266. package/dist/utils/db.utils.js +0 -323
  267. /package/dist/{controllers/api-controller.utils.d.ts → __tests__/test-database.interface.js} +0 -0
  268. /package/dist/{controllers/api-controller.utils.js → databases/models/database.interface.js} +0 -0
  269. /package/dist/{models/types/index.d.ts → databases/models/database.js} +0 -0
  270. /package/dist/{models/types/index.js → databases/mongo-db/models/no-sql-pipeline.interface.js} +0 -0
  271. /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,5 @@
1
+ import { Db } from "mongodb";
2
+ export declare function createMany<T>(db: Db, pluralResourceName: string, entities: Partial<T>[]): Promise<{
3
+ insertedIds: any;
4
+ entities: any[];
5
+ }>;
@@ -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,5 @@
1
+ import { Db } from "mongodb";
2
+ export declare function create<T>(db: Db, pluralResourceName: string, entity: Partial<T>): Promise<{
3
+ insertedId: any;
4
+ entity: T;
5
+ }>;
@@ -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,3 @@
1
+ import { Db } from "mongodb";
2
+ import { DeleteResult as GenericDeleteResult } from "../../models/delete-result.js";
3
+ export declare function deleteById(db: Db, id: string, pluralResourceName: string): Promise<GenericDeleteResult>;
@@ -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,3 @@
1
+ import { Db } from "mongodb";
2
+ import { Operation } from "../../operations/operation.js";
3
+ export declare function fullUpdateById<T>(db: Db, operations: Operation[], id: string, entity: any, pluralResourceName: string): Promise<T>;
@@ -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,3 @@
1
+ import { Db } from "mongodb";
2
+ import { Operation } from "../../operations/operation.js";
3
+ export declare function partialUpdateById<T>(db: Db, operations: Operation[], id: string, entity: Partial<any>, pluralResourceName: string): Promise<T>;
@@ -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,3 @@
1
+ import { Db } from "mongodb";
2
+ import { Operation } from "../../operations/operation.js";
3
+ export declare function partialUpdateById<T>(db: Db, operations: Operation[], id: string, entity: Partial<any>, pluralResourceName: string): Promise<T>;
@@ -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,4 @@
1
+ export * from './utils/index.js';
2
+ export * from './mongo-db.database.js';
3
+ export * from './queries/index.js';
4
+ export * from './commands/index.js';
@@ -0,0 +1,4 @@
1
+ export * from './utils/index.js';
2
+ export * from './mongo-db.database.js';
3
+ export * from './queries/index.js';
4
+ export * from './commands/index.js';
@@ -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,3 @@
1
+ import { Db } from "mongodb";
2
+ import { IQueryOptions } from "@loomcore/common/models";
3
+ export declare function findOne<T>(db: Db, queryObject: IQueryOptions, pluralResourceName: string): Promise<T | null>;
@@ -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,3 @@
1
+ import { Db } from "mongodb";
2
+ import { IQueryOptions } from "@loomcore/common/models";
3
+ export declare function find<T>(db: Db, queryObject: IQueryOptions, pluralResourceName: string): Promise<T[]>;
@@ -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,3 @@
1
+ import { Db } from "mongodb";
2
+ import { Operation } from "../../operations/operation.js";
3
+ export declare function getAll<T>(db: Db, operations: Operation[], pluralResourceName: string): Promise<T[]>;
@@ -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,3 @@
1
+ import { Db } from "mongodb";
2
+ import { Operation } from "../../operations/operation.js";
3
+ export declare function getById<T>(db: Db, operations: Operation[], id: string, pluralResourceName: string): Promise<T | null>;
@@ -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,2 @@
1
+ import { Db } from "mongodb";
2
+ export declare function getCount(db: Db, pluralResourceName: string): Promise<number>;
@@ -0,0 +1,5 @@
1
+ export async function getCount(db, pluralResourceName) {
2
+ const collection = db.collection(pluralResourceName);
3
+ const result = await collection.countDocuments();
4
+ return result;
5
+ }
@@ -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,6 @@
1
+ export * from './mongo-get-all.query.js';
2
+ export * from './mongo-get.query.js';
3
+ export * from './mongo-get-by-id.query.js';
4
+ export * from './mongo-get-count.query.js';
5
+ export * from './mongo-find.query.js';
6
+ export * from './mongo-find-one.query.js';
@@ -0,0 +1,6 @@
1
+ export * from './mongo-get-all.query.js';
2
+ export * from './mongo-get.query.js';
3
+ export * from './mongo-get-by-id.query.js';
4
+ export * from './mongo-get-count.query.js';
5
+ export * from './mongo-find.query.js';
6
+ export * from './mongo-find-one.query.js';
@@ -0,0 +1,3 @@
1
+ import { Db } from "mongodb";
2
+ import { IQueryOptions } from "@loomcore/common/models";
3
+ export declare function findOne<T>(db: Db, queryObject: IQueryOptions, pluralResourceName: string): Promise<T | null>;
@@ -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,3 @@
1
+ import { Db } from "mongodb";
2
+ import { IQueryOptions } from "@loomcore/common/models";
3
+ export declare function find<T>(db: Db, queryObject: IQueryOptions, pluralResourceName: string): Promise<T[]>;