@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
@@ -1,323 +0,0 @@
1
- import { ObjectId } from 'mongodb';
2
- import _ from 'lodash';
3
- import { entityUtils } from '@loomcore/common/utils';
4
- import { stringUtils } from './string.utils.js';
5
- function getPropertySchema(key, schema) {
6
- if (!schema || typeof schema !== 'object')
7
- return undefined;
8
- if (schema.allOf && Array.isArray(schema.allOf)) {
9
- for (const nestedSchema of schema.allOf) {
10
- const propSchema = getPropertySchema(key, nestedSchema);
11
- if (propSchema)
12
- return propSchema;
13
- }
14
- }
15
- if (schema.type === 'object' && schema.properties) {
16
- return schema.properties[key];
17
- }
18
- return undefined;
19
- }
20
- export const PROPERTIES_THAT_ARE_NOT_OBJECT_IDS = ['_orgId'];
21
- function convertObjectIdsToStrings(entity, schema) {
22
- if (!entity)
23
- return entity;
24
- const clone = _.cloneDeep(entity);
25
- if (!schema) {
26
- if (clone._id && clone._id instanceof ObjectId) {
27
- clone._id = clone._id.toString();
28
- }
29
- return clone;
30
- }
31
- const processEntity = (obj, subSchema, path = []) => {
32
- if (!obj || typeof obj !== 'object')
33
- return;
34
- if (subSchema.allOf && Array.isArray(subSchema.allOf)) {
35
- for (const nestedSchema of subSchema.allOf) {
36
- processEntity(obj, nestedSchema, path);
37
- }
38
- return;
39
- }
40
- if (subSchema.type === 'object' && subSchema.properties) {
41
- for (const [key, propSchema] of Object.entries(subSchema.properties)) {
42
- if (!propSchema || typeof propSchema !== 'object')
43
- continue;
44
- const typedPropSchema = propSchema;
45
- const fullPath = [...path, key];
46
- if (typedPropSchema.format === 'objectid') {
47
- if (path.length === 0 && PROPERTIES_THAT_ARE_NOT_OBJECT_IDS.includes(key)) {
48
- continue;
49
- }
50
- if (obj[key] instanceof ObjectId) {
51
- obj[key] = obj[key].toString();
52
- }
53
- }
54
- else if (typedPropSchema.type === 'object' && obj[key]) {
55
- processEntity(obj[key], typedPropSchema, fullPath);
56
- }
57
- else if (typedPropSchema.type === 'array' && Array.isArray(obj[key])) {
58
- const items = typedPropSchema.items;
59
- if (items) {
60
- for (let i = 0; i < obj[key].length; i++) {
61
- if (items.format === 'objectid') {
62
- if (path.length === 0 && PROPERTIES_THAT_ARE_NOT_OBJECT_IDS.includes(key)) {
63
- continue;
64
- }
65
- if (obj[key][i] instanceof ObjectId) {
66
- obj[key][i] = obj[key][i].toString();
67
- }
68
- }
69
- else if (items.type === 'object') {
70
- processEntity(obj[key][i], items, [...fullPath, i.toString()]);
71
- }
72
- }
73
- }
74
- }
75
- }
76
- }
77
- };
78
- processEntity(clone, schema);
79
- return clone;
80
- }
81
- function convertStringsToObjectIds(entity, schema) {
82
- if (!entity)
83
- return entity;
84
- const clone = _.cloneDeep(entity);
85
- if (clone._id && typeof clone._id === 'string' && entityUtils.isValidObjectId(clone._id)) {
86
- clone._id = new ObjectId(clone._id);
87
- }
88
- const processEntity = (obj, subSchema, path = []) => {
89
- if (!obj || typeof obj !== 'object')
90
- return obj;
91
- if (obj instanceof Date) {
92
- return obj;
93
- }
94
- if (subSchema.allOf && Array.isArray(subSchema.allOf)) {
95
- let result = { ...obj };
96
- for (const nestedSchema of subSchema.allOf) {
97
- result = processEntity(result, nestedSchema, path);
98
- }
99
- return result;
100
- }
101
- if (Array.isArray(obj)) {
102
- const items = subSchema.items;
103
- if (!items)
104
- return obj;
105
- if (items.format === 'objectid') {
106
- if (path.length === 1 && PROPERTIES_THAT_ARE_NOT_OBJECT_IDS.includes(path[0])) {
107
- return obj;
108
- }
109
- return obj.map(item => {
110
- if (typeof item === 'string' && entityUtils.isValidObjectId(item)) {
111
- return new ObjectId(item);
112
- }
113
- return item;
114
- });
115
- }
116
- if (items.type === 'object') {
117
- return obj.map((item, index) => processEntity(item, items, [...path, index.toString()]));
118
- }
119
- return obj;
120
- }
121
- const result = { ...obj };
122
- if (subSchema.type === 'object' && subSchema.properties) {
123
- for (const [key, propSchema] of Object.entries(subSchema.properties)) {
124
- if (!propSchema || typeof propSchema !== 'object' || result[key] === null || result[key] === undefined) {
125
- continue;
126
- }
127
- const typedPropSchema = propSchema;
128
- const fullPath = [...path, key];
129
- const value = result[key];
130
- const isObjectIdField = typedPropSchema.format === 'objectid';
131
- if (isObjectIdField && path.length === 0 && PROPERTIES_THAT_ARE_NOT_OBJECT_IDS.includes(key)) {
132
- continue;
133
- }
134
- if (isObjectIdField && typeof value === 'string' && entityUtils.isValidObjectId(value)) {
135
- result[key] = new ObjectId(value);
136
- }
137
- else if (typedPropSchema.type === 'array' && Array.isArray(value)) {
138
- result[key] = processEntity(value, typedPropSchema, fullPath);
139
- }
140
- else if (typedPropSchema.type === 'object' && typeof value === 'object' && !Array.isArray(value)) {
141
- result[key] = processEntity(value, typedPropSchema, fullPath);
142
- }
143
- }
144
- }
145
- return result;
146
- };
147
- return processEntity(clone, schema);
148
- }
149
- function convertStringToObjectId(value) {
150
- if (value instanceof ObjectId) {
151
- return value;
152
- }
153
- if (value === null || value === undefined) {
154
- return value;
155
- }
156
- if (typeof value === 'string' && entityUtils.isValidObjectId(value)) {
157
- try {
158
- return new ObjectId(value);
159
- }
160
- catch (error) {
161
- console.warn(`Failed to convert string "${value}" to ObjectId:`, error);
162
- return value;
163
- }
164
- }
165
- return value;
166
- }
167
- function buildMongoMatchFromQueryOptions(queryOptions, modelSpec) {
168
- const filters = queryOptions.filters || {};
169
- const schema = modelSpec?.fullSchema;
170
- let match = {};
171
- console.log('--- Building Mongo Match ---');
172
- console.log('Received queryOptions.filters:', JSON.stringify(filters, null, 2));
173
- console.log('Received modelSpec:', !!modelSpec);
174
- for (const [key, value] of Object.entries(filters)) {
175
- if (value) {
176
- const propSchema = schema ? getPropertySchema(key, schema) : undefined;
177
- if (value.eq !== undefined) {
178
- const isObjectIdField = propSchema?.format === 'objectid';
179
- const valueToCompare = value.eq;
180
- if ((isObjectIdField || (!schema && key.endsWith('Id') && !PROPERTIES_THAT_ARE_NOT_OBJECT_IDS.includes(key)))
181
- && typeof valueToCompare === 'string' && entityUtils.isValidObjectId(valueToCompare)) {
182
- match[key] = new ObjectId(valueToCompare);
183
- }
184
- else if (typeof valueToCompare === 'string' && !isNaN(Number(valueToCompare))) {
185
- match[key] = Number(valueToCompare);
186
- }
187
- else {
188
- match[key] = valueToCompare;
189
- }
190
- }
191
- else if (value.in !== undefined && Array.isArray(value.in)) {
192
- const isObjectIdArray = propSchema?.type === 'array' && propSchema.items?.format === 'objectid';
193
- if (isObjectIdArray || (!schema && (key.endsWith('Id') || key.endsWith('Ids')) && !PROPERTIES_THAT_ARE_NOT_OBJECT_IDS.includes(key))) {
194
- const objectIds = value.in
195
- .filter(val => typeof val === 'string' && entityUtils.isValidObjectId(val))
196
- .map(val => new ObjectId(val));
197
- if (objectIds.length > 0) {
198
- match[key] = { $in: objectIds };
199
- }
200
- }
201
- else {
202
- const convertedValues = value.in.map(val => typeof val === 'string' && !isNaN(Number(val)) ? Number(val) : val);
203
- match[key] = { $in: convertedValues };
204
- }
205
- }
206
- else if (value.gte !== undefined) {
207
- match[key] = { $gte: value.gte };
208
- }
209
- else if (value.lte !== undefined) {
210
- match[key] = { $lte: value.lte };
211
- }
212
- else if (value.gt !== undefined) {
213
- match[key] = { $gt: value.gt };
214
- }
215
- else if (value.lt !== undefined) {
216
- match[key] = { $lt: value.lt };
217
- }
218
- else if (value.contains !== undefined) {
219
- match[key] = { $regex: value.contains, $options: 'i' };
220
- }
221
- }
222
- }
223
- console.log('Constructed $match object:', JSON.stringify(match, null, 2));
224
- console.log('--------------------------');
225
- return { $match: match };
226
- }
227
- function buildSQLWhereClauseFromQueryOptions(queryOptions, columnAliasMap) {
228
- const filters = queryOptions.filters || {};
229
- let whereClause = '';
230
- for (const [key, value] of Object.entries(filters)) {
231
- if (value) {
232
- const tableAlias = (columnAliasMap && columnAliasMap[key]) || '';
233
- whereClause = addKeyValueToWhereClause(whereClause, key, value, tableAlias);
234
- }
235
- }
236
- return whereClause;
237
- }
238
- function addKeyValueToWhereClause(whereClause, key, value, tableAlias = '') {
239
- let column = tableAlias ? `${tableAlias}.${stringUtils.pascalCase(key)}` : stringUtils.pascalCase(key);
240
- let formattedValue = '';
241
- let operator = '=';
242
- if (value) {
243
- if (value.eq !== undefined) {
244
- formattedValue = formatValue(value.eq);
245
- operator = '=';
246
- }
247
- else if (value.in !== undefined && Array.isArray(value.in)) {
248
- const formattedValues = value.in.map(val => formatValue(val)).join(', ');
249
- formattedValue = `(${formattedValues})`;
250
- operator = 'IN';
251
- }
252
- else if (value.gte !== undefined) {
253
- formattedValue = formatValue(value.gte);
254
- operator = '>=';
255
- }
256
- else if (value.lte !== undefined) {
257
- formattedValue = formatValue(value.lte);
258
- operator = '<=';
259
- }
260
- else if (value.gt !== undefined) {
261
- formattedValue = formatValue(value.gt);
262
- operator = '>';
263
- }
264
- else if (value.lt !== undefined) {
265
- formattedValue = formatValue(value.lt);
266
- operator = '<';
267
- }
268
- else if (value.contains !== undefined) {
269
- column = `LOWER(${column})`;
270
- formattedValue = formatValue(value.contains, true).toLowerCase();
271
- operator = 'LIKE';
272
- }
273
- }
274
- const condition = `${column} ${operator} ${formattedValue}`;
275
- return appendToWhereClause(whereClause, condition);
276
- }
277
- function appendToWhereClause(whereClause, condition) {
278
- let newWhereClause = whereClause.trim();
279
- if (newWhereClause.toUpperCase() === 'WHERE' || newWhereClause === '') {
280
- newWhereClause = `WHERE ${condition}`;
281
- }
282
- else {
283
- newWhereClause = `${newWhereClause} AND ${condition}`;
284
- }
285
- return newWhereClause;
286
- }
287
- function formatValue(value, isLikeOperator = false) {
288
- if (typeof value === 'string') {
289
- if (!isNaN(Number(value))) {
290
- return value;
291
- }
292
- if (value.toLowerCase() === 'true') {
293
- return 'TRUE';
294
- }
295
- if (value.toLowerCase() === 'false') {
296
- return 'FALSE';
297
- }
298
- return isLikeOperator ? `'%${value}%'` : `'${value}'`;
299
- }
300
- else if (typeof value === 'number') {
301
- return value.toString();
302
- }
303
- else if (typeof value === 'boolean') {
304
- return value ? 'TRUE' : 'FALSE';
305
- }
306
- else if (value instanceof Date) {
307
- const dateString = value.toISOString().split('T')[0];
308
- return `DATETIME('${dateString}')`;
309
- }
310
- else {
311
- throw new Error('Unsupported value type');
312
- }
313
- }
314
- export const dbUtils = {
315
- buildMongoMatchFromQueryOptions,
316
- buildSQLWhereClauseFromQueryOptions,
317
- addKeyValueToWhereClause,
318
- appendToWhereClause,
319
- formatValue,
320
- convertStringToObjectId,
321
- convertStringsToObjectIds,
322
- convertObjectIdsToStrings,
323
- };