@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.
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 -18
  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 -318
  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,81 @@
1
+ import { MongoMemoryServer } from 'mongodb-memory-server';
2
+ import { MongoClient, ObjectId } from 'mongodb';
3
+ import testUtils from './common-test.utils.js';
4
+ import { initSystemUserContext } from '../config/base-api-config.js';
5
+ export class TestMongoDb {
6
+ static mongoServer;
7
+ static client;
8
+ static db;
9
+ static initPromise = null;
10
+ static async init() {
11
+ if (this.initPromise) {
12
+ return this.initPromise;
13
+ }
14
+ this.initPromise = this._performInit();
15
+ return this.initPromise;
16
+ }
17
+ static getRandomId() {
18
+ return new ObjectId().toString();
19
+ }
20
+ static async _performInit() {
21
+ if (!this.db) {
22
+ this.mongoServer = await MongoMemoryServer.create({
23
+ instance: {
24
+ ip: '127.0.0.1',
25
+ port: 0,
26
+ },
27
+ binary: {
28
+ downloadDir: process.env.HOME ? `${process.env.HOME}/.cache/mongodb-binaries` : undefined,
29
+ }
30
+ });
31
+ const uri = this.mongoServer.getUri();
32
+ this.client = await MongoClient.connect(uri);
33
+ this.db = this.client.db();
34
+ testUtils.initialize(this.db);
35
+ await this.createIndexes(this.db);
36
+ await testUtils.createMetaOrg();
37
+ }
38
+ await initSystemUserContext(this.db);
39
+ return this.db;
40
+ }
41
+ static async createIndexes(db) {
42
+ await db.command({
43
+ createIndexes: "users", indexes: [{ key: { email: 1 }, name: 'email_index', unique: true, collation: { locale: 'en', strength: 1 } }]
44
+ });
45
+ }
46
+ static getDb() {
47
+ if (!this.db) {
48
+ throw new Error('Database not initialized. Call init() first.');
49
+ }
50
+ return this.db;
51
+ }
52
+ static async clearCollections() {
53
+ if (!this.db) {
54
+ throw new Error('Database not initialized');
55
+ }
56
+ const collections = await this.db.collections();
57
+ for (const collection of collections) {
58
+ await collection.deleteMany({});
59
+ }
60
+ }
61
+ static async cleanup() {
62
+ await testUtils.cleanup();
63
+ if (!this.db) {
64
+ throw new Error('Database not initialized');
65
+ }
66
+ const collections = await this.db.collections();
67
+ for (const collection of collections) {
68
+ await collection.deleteMany({});
69
+ }
70
+ if (this.client) {
71
+ await this.client.close();
72
+ }
73
+ if (this.mongoServer) {
74
+ await this.mongoServer.stop();
75
+ }
76
+ this.initPromise = null;
77
+ this.db = undefined;
78
+ this.client = undefined;
79
+ this.mongoServer = undefined;
80
+ }
81
+ }
@@ -0,0 +1,23 @@
1
+ import { IOrganization, IUser, IUserContext } from "@loomcore/common/models";
2
+ export declare const testUser: IUser;
3
+ export declare function getTestUser(): {
4
+ email: string;
5
+ firstName?: string;
6
+ lastName?: string;
7
+ displayName?: string;
8
+ password: string;
9
+ roles?: string[];
10
+ _lastLoggedIn?: Date;
11
+ _lastPasswordChange?: Date;
12
+ _created: Date;
13
+ _createdBy: string;
14
+ _updated: Date;
15
+ _updatedBy: string;
16
+ _deleted?: Date;
17
+ _deletedBy?: string;
18
+ _id: string;
19
+ _orgId?: string;
20
+ };
21
+ export declare const testUserContext: IUserContext;
22
+ export declare const testMetaOrg: IOrganization;
23
+ export declare const testOrg: IOrganization;
@@ -0,0 +1,45 @@
1
+ export const testUser = {
2
+ _id: '69261672f48fb7bf76e54dfb',
3
+ email: 'test@example.com',
4
+ password: 'testpassword',
5
+ firstName: 'Test',
6
+ lastName: 'User',
7
+ displayName: 'Test User',
8
+ roles: ['user'],
9
+ _orgId: '6926167d06c0073a778a124f',
10
+ _created: new Date(),
11
+ _createdBy: 'system',
12
+ _lastLoggedIn: new Date(),
13
+ _lastPasswordChange: new Date(),
14
+ _updated: new Date(),
15
+ _updatedBy: 'system',
16
+ };
17
+ export function getTestUser() {
18
+ return { ...testUser };
19
+ }
20
+ export const testUserContext = {
21
+ user: getTestUser(),
22
+ _orgId: '6926167d06c0073a778a124f'
23
+ };
24
+ export const testMetaOrg = {
25
+ _id: '69261691f936c45f85da24d0',
26
+ name: 'Test Meta Organization',
27
+ code: 'test-meta-org',
28
+ status: 1,
29
+ isMetaOrg: true,
30
+ _created: new Date(),
31
+ _createdBy: 'system',
32
+ _updated: new Date(),
33
+ _updatedBy: 'system',
34
+ };
35
+ export const testOrg = {
36
+ _id: '6926167d06c0073a778a124f',
37
+ name: 'Test Organization',
38
+ code: 'test-org',
39
+ status: 1,
40
+ isMetaOrg: false,
41
+ _created: new Date(),
42
+ _createdBy: 'system',
43
+ _updated: new Date(),
44
+ _updatedBy: 'system',
45
+ };
@@ -0,0 +1,3 @@
1
+ import { IUser, IUserContext } from "@loomcore/common/models";
2
+ export declare const testUser: IUser;
3
+ export declare const testUserContext: IUserContext;
@@ -0,0 +1,16 @@
1
+ export const testUser = {
2
+ _id: '67f33ed5b75090e0dda18a3c',
3
+ email: 'test@example.com',
4
+ password: 'testpassword',
5
+ _orgId: '67e8e19b149f740323af93d7',
6
+ _created: new Date(),
7
+ _createdBy: 'system',
8
+ _updated: new Date(),
9
+ _updatedBy: 'system',
10
+ _deleted: undefined,
11
+ _deletedBy: undefined
12
+ };
13
+ export const testUserContext = {
14
+ user: testUser,
15
+ _orgId: '67e8e19b149f740323af93d7'
16
+ };
@@ -1,5 +1,5 @@
1
- import { Db } from 'mongodb';
2
1
  import { IBaseApiConfig } from '../models/index.js';
2
+ import { IDatabase } from '../databases/models/index.js';
3
3
  export declare let config: IBaseApiConfig;
4
4
  export declare function setBaseApiConfig(apiConfig: IBaseApiConfig): void;
5
- export declare function initSystemUserContext(db: Db): Promise<void>;
5
+ export declare function initSystemUserContext(database: IDatabase): Promise<void>;
@@ -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(db) {
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(db);
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, IUserContext } from '@loomcore/common/models';
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 preparedEntity = await this.prepareDataForDb(req.userContext, req.body, true);
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 preparedEntities = await this.service.prepareDataForBatchUpdate(req.userContext, entities);
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 preparedEntity = await this.prepareDataForDb(req.userContext, req.body, false);
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 preparedEntity = await this.prepareDataForDb(req.userContext, req.body, false);
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, db: Db);
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, db) {
10
- const authService = new AuthService(db);
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 preparedUser = await this.authService.prepareDataForDb(userContext, body, true);
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, db: Db);
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, db) {
9
- const orgService = new OrganizationService(db);
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: { $regex: new RegExp(`^${name}$`, 'i') } });
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, db: Db);
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, db) {
8
- const userService = new UserService(db);
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,3 @@
1
+ import { Db } from 'mongodb';
2
+ import { Client } from 'pg';
3
+ export type Database = Db | Client;
@@ -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,5 @@
1
+ export declare class DeleteResult {
2
+ success: boolean;
3
+ count: number;
4
+ constructor(success: boolean, count: number);
5
+ }
@@ -0,0 +1,8 @@
1
+ export class DeleteResult {
2
+ success;
3
+ count;
4
+ constructor(success, count) {
5
+ this.success = success;
6
+ this.count = count;
7
+ }
8
+ }
@@ -0,0 +1,5 @@
1
+ export * from './constants.js';
2
+ export * from './database.interface.js';
3
+ export * from './database.js';
4
+ export * from './delete-result.js';
5
+ export * from './update-result.js';
@@ -0,0 +1,5 @@
1
+ export * from './constants.js';
2
+ export * from './database.interface.js';
3
+ export * from './database.js';
4
+ export * from './delete-result.js';
5
+ export * from './update-result.js';
@@ -0,0 +1,5 @@
1
+ export declare class UpdateResult {
2
+ success: boolean;
3
+ count: number;
4
+ constructor(success: boolean, count: number);
5
+ }
@@ -0,0 +1,8 @@
1
+ export class UpdateResult {
2
+ success;
3
+ count;
4
+ constructor(success, count) {
5
+ this.success = success;
6
+ this.count = count;
7
+ }
8
+ }
@@ -0,0 +1,3 @@
1
+ import { Db } from "mongodb";
2
+ import { Operation } from "../../operations/operation.js";
3
+ export declare function batchUpdate<T>(db: Db, entities: Partial<T>[], operations: Operation[], 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, 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: any): Promise<{
3
+ insertedId: any;
4
+ entity: any;
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,9 @@
1
+ import { ObjectId } from "mongodb";
2
+ import { DeleteResult as GenericDeleteResult } from "../../models/delete-result.js";
3
+ export async function deleteById(db, id, pluralResourceName) {
4
+ const collection = db.collection(pluralResourceName);
5
+ const objectId = new ObjectId(id);
6
+ const baseQuery = { _id: objectId };
7
+ const deleteResult = await collection.deleteOne(baseQuery);
8
+ return new GenericDeleteResult(deleteResult.acknowledged, deleteResult.deletedCount);
9
+ }
@@ -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,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 fullUpdateById(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 replaceResult = await collection.replaceOne(filter, entity);
9
+ if (replaceResult.matchedCount <= 0) {
10
+ throw new IdNotFoundError();
11
+ }
12
+ const pipeline = new NoSqlPipeline()
13
+ .addMatch({ filters: { _id: { eq: id } } })
14
+ .addOperations(operations)
15
+ .build();
16
+ const updatedEntity = await collection.aggregate(pipeline).next();
17
+ if (!updatedEntity) {
18
+ throw new IdNotFoundError();
19
+ }
20
+ return updatedEntity;
21
+ }
@@ -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';