@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,94 @@
1
+ import { stringUtils } from './string.utils.js';
2
+ function buildSQLWhereClauseFromQueryOptions(queryOptions, columnAliasMap) {
3
+ const filters = queryOptions.filters || {};
4
+ let whereClause = '';
5
+ for (const [key, value] of Object.entries(filters)) {
6
+ if (value) {
7
+ const tableAlias = (columnAliasMap && columnAliasMap[key]) || '';
8
+ whereClause = addKeyValueToWhereClause(whereClause, key, value, tableAlias);
9
+ }
10
+ }
11
+ return whereClause;
12
+ }
13
+ function addKeyValueToWhereClause(whereClause, key, value, tableAlias = '') {
14
+ let column = tableAlias ? `${tableAlias}.${stringUtils.pascalCase(key)}` : stringUtils.pascalCase(key);
15
+ let formattedValue = '';
16
+ let operator = '=';
17
+ if (value) {
18
+ if (value.eq !== undefined) {
19
+ formattedValue = formatValue(value.eq);
20
+ operator = '=';
21
+ }
22
+ else if (value.in !== undefined && Array.isArray(value.in)) {
23
+ const formattedValues = value.in.map(val => formatValue(val)).join(', ');
24
+ formattedValue = `(${formattedValues})`;
25
+ operator = 'IN';
26
+ }
27
+ else if (value.gte !== undefined) {
28
+ formattedValue = formatValue(value.gte);
29
+ operator = '>=';
30
+ }
31
+ else if (value.lte !== undefined) {
32
+ formattedValue = formatValue(value.lte);
33
+ operator = '<=';
34
+ }
35
+ else if (value.gt !== undefined) {
36
+ formattedValue = formatValue(value.gt);
37
+ operator = '>';
38
+ }
39
+ else if (value.lt !== undefined) {
40
+ formattedValue = formatValue(value.lt);
41
+ operator = '<';
42
+ }
43
+ else if (value.contains !== undefined) {
44
+ column = `LOWER(${column})`;
45
+ formattedValue = formatValue(value.contains, true).toLowerCase();
46
+ operator = 'LIKE';
47
+ }
48
+ }
49
+ const condition = `${column} ${operator} ${formattedValue}`;
50
+ return appendToWhereClause(whereClause, condition);
51
+ }
52
+ function appendToWhereClause(whereClause, condition) {
53
+ let newWhereClause = whereClause.trim();
54
+ if (newWhereClause.toUpperCase() === 'WHERE' || newWhereClause === '') {
55
+ newWhereClause = `WHERE ${condition}`;
56
+ }
57
+ else {
58
+ newWhereClause = `${newWhereClause} AND ${condition}`;
59
+ }
60
+ return newWhereClause;
61
+ }
62
+ function formatValue(value, isLikeOperator = false) {
63
+ if (typeof value === 'string') {
64
+ if (!isNaN(Number(value))) {
65
+ return value;
66
+ }
67
+ if (value.toLowerCase() === 'true') {
68
+ return 'TRUE';
69
+ }
70
+ if (value.toLowerCase() === 'false') {
71
+ return 'FALSE';
72
+ }
73
+ return isLikeOperator ? `'%${value}%'` : `'${value}'`;
74
+ }
75
+ else if (typeof value === 'number') {
76
+ return value.toString();
77
+ }
78
+ else if (typeof value === 'boolean') {
79
+ return value ? 'TRUE' : 'FALSE';
80
+ }
81
+ else if (value instanceof Date) {
82
+ const dateString = value.toISOString().split('T')[0];
83
+ return `DATETIME('${dateString}')`;
84
+ }
85
+ else {
86
+ throw new Error('Unsupported value type');
87
+ }
88
+ }
89
+ export const sqlDbUtils = {
90
+ buildSQLWhereClauseFromQueryOptions,
91
+ addKeyValueToWhereClause,
92
+ appendToWhereClause,
93
+ formatValue,
94
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@loomcore/api",
3
- "version": "0.0.59",
3
+ "version": "0.1.0",
4
4
  "private": false,
5
5
  "description": "Loom Core Api - An opinionated Node.js api using Typescript, Express, and MongoDb",
6
6
  "scripts": {
@@ -33,6 +33,7 @@
33
33
  "exports": {
34
34
  "./config": "./dist/config/index.js",
35
35
  "./controllers": "./dist/controllers/index.js",
36
+ "./database": "./dist/database/index.js",
36
37
  "./errors": "./dist/errors/index.js",
37
38
  "./middleware": "./dist/middleware/index.js",
38
39
  "./models": "./dist/models/index.js",
@@ -42,10 +43,11 @@
42
43
  "dependencies": {
43
44
  "jsonwebtoken": "^9.0.2",
44
45
  "node-mailjet": "^6.0.8",
46
+ "postgres": "^3.4.7",
45
47
  "qs": "^6.14.0"
46
48
  },
47
49
  "peerDependencies": {
48
- "@loomcore/common": "^0.0.14",
50
+ "@loomcore/common": "^0.0.19",
49
51
  "@sinclair/typebox": "0.34.33",
50
52
  "cookie-parser": "^1.4.6",
51
53
  "cors": "^2.8.5",
@@ -1,27 +0,0 @@
1
- import { DeleteResult } from 'mongodb';
2
- import { ValueError } from '@sinclair/typebox/errors';
3
- import { IUserContext, IEntity, IPagedResult, IQueryOptions } from '@loomcore/common/models';
4
- export interface IGenericApiService<T extends IEntity> {
5
- validate(doc: any, isPartial?: boolean): ValueError[] | null;
6
- validateMany(docs: any[], isPartial?: boolean): ValueError[] | null;
7
- prepareDataForDb(userContext: IUserContext, entity: T, isCreate?: boolean): Promise<T>;
8
- prepareDataForDb(userContext: IUserContext, entity: Partial<T>, isCreate?: boolean): Promise<Partial<T>>;
9
- prepareDataForDb(userContext: IUserContext, entity: T[], isCreate?: boolean): Promise<T[]>;
10
- prepareDataForDb(userContext: IUserContext, entity: Partial<T>[], isCreate?: boolean): Promise<Partial<T>[]>;
11
- prepareDataForBatchUpdate(userContext: IUserContext, entities: Partial<T>[]): Promise<Partial<T>[]>;
12
- getAll(userContext: IUserContext): Promise<T[]>;
13
- get(userContext: IUserContext, queryOptions: IQueryOptions): Promise<IPagedResult<T>>;
14
- getById(userContext: IUserContext, id: string): Promise<T>;
15
- getCount(userContext: IUserContext): Promise<number>;
16
- create(userContext: IUserContext, entity: T | Partial<T>): Promise<T | null>;
17
- createMany(userContext: IUserContext, entities: T[]): Promise<T[]>;
18
- batchUpdate(userContext: IUserContext, entities: Partial<T>[]): Promise<T[]>;
19
- fullUpdateById(userContext: IUserContext, id: string, entity: T): Promise<T>;
20
- partialUpdateById(userContext: IUserContext, id: string, entity: Partial<T>): Promise<T>;
21
- partialUpdateByIdWithoutBeforeAndAfter(userContext: IUserContext, id: string, entity: T): Promise<T>;
22
- update(userContext: IUserContext, queryObject: any, entity: Partial<T>): Promise<T[]>;
23
- deleteById(userContext: IUserContext, id: string): Promise<DeleteResult>;
24
- deleteMany(userContext: IUserContext, queryObject: any): Promise<DeleteResult>;
25
- find(userContext: IUserContext, mongoQueryObject: any, options?: any): Promise<T[]>;
26
- findOne(userContext: IUserContext, mongoQueryObject: any, options?: any): Promise<T>;
27
- }
@@ -1,50 +0,0 @@
1
- import { Db, Collection, DeleteResult, Document, FindOptions } from 'mongodb';
2
- import { ValueError } from '@sinclair/typebox/errors';
3
- import { IUserContext, IEntity, IQueryOptions, IPagedResult, IModelSpec } from '@loomcore/common/models';
4
- import { IGenericApiService } from './generic-api-service.interface.js';
5
- export declare class GenericApiService<T extends IEntity> implements IGenericApiService<T> {
6
- protected db: Db;
7
- protected pluralResourceName: string;
8
- protected singularResourceName: string;
9
- protected collection: Collection;
10
- protected modelSpec?: IModelSpec;
11
- constructor(db: Db, pluralResourceName: string, singularResourceName: string, modelSpec?: IModelSpec);
12
- validate(doc: any, isPartial?: boolean): ValueError[] | null;
13
- validateMany(docs: any[], isPartial?: boolean): ValueError[] | null;
14
- protected getAdditionalPipelineStages(): any[];
15
- protected createAggregationPipeline(userContext: IUserContext, query: any, queryOptions?: IQueryOptions): any[];
16
- getAll(userContext: IUserContext): Promise<T[]>;
17
- get(userContext: IUserContext, queryOptions?: IQueryOptions): Promise<IPagedResult<T>>;
18
- getById(userContext: IUserContext, id: string): Promise<T>;
19
- getCount(userContext: IUserContext): Promise<number>;
20
- create(userContext: IUserContext, preparedEntity: T | Partial<T>): Promise<T | null>;
21
- createMany(userContext: IUserContext, preparedEntities: T[]): Promise<T[]>;
22
- batchUpdate(userContext: IUserContext, preparedEntities: Partial<T>[]): Promise<T[]>;
23
- fullUpdateById(userContext: IUserContext, id: string, preparedEntity: T): Promise<T>;
24
- partialUpdateById(userContext: IUserContext, id: string, preparedEntity: Partial<T>): Promise<T>;
25
- partialUpdateByIdWithoutBeforeAndAfter(userContext: IUserContext, id: string, preparedEntity: T): Promise<T>;
26
- update(userContext: IUserContext, queryObject: any, preparedEntity: Partial<T>): Promise<T[]>;
27
- deleteById(userContext: IUserContext, id: string): Promise<DeleteResult>;
28
- deleteMany(userContext: IUserContext, queryObject: any): Promise<DeleteResult>;
29
- find(userContext: IUserContext, mongoQueryObject: any, options?: FindOptions<Document> | undefined): Promise<T[]>;
30
- findOne(userContext: IUserContext, mongoQueryObject: any, options?: FindOptions<Document> | undefined): Promise<T>;
31
- auditForCreate(userContext: IUserContext, doc: any): void;
32
- auditForUpdate(userContext: IUserContext, doc: any): void;
33
- onBeforeCreate<E extends T | T[] | Partial<T> | Partial<T>[]>(userContext: IUserContext, entities: E): Promise<E | E[]>;
34
- onAfterCreate<E extends T | T[]>(userContext: IUserContext, entities: E): Promise<E | E[]>;
35
- onBeforeUpdate<E extends T | T[] | Partial<T> | Partial<T>[]>(userContext: IUserContext, entities: E): Promise<E | E[]>;
36
- onAfterUpdate<E extends T | T[] | Partial<T> | Partial<T>[]>(userContext: IUserContext | undefined, entities: E): Promise<E>;
37
- onBeforeDelete(userContext: IUserContext, queryObject: any): Promise<any>;
38
- onAfterDelete(userContext: IUserContext, queryObject: any): Promise<any>;
39
- transformList(list: any[]): T[];
40
- transformSingle(single: any): T;
41
- private stripSenderProvidedSystemProperties;
42
- prepareDataForDb(userContext: IUserContext, entity: T, isCreate?: boolean): Promise<T>;
43
- prepareDataForDb(userContext: IUserContext, entity: Partial<T>, isCreate?: boolean): Promise<Partial<T>>;
44
- prepareDataForDb(userContext: IUserContext, entity: T[], isCreate?: boolean): Promise<T[]>;
45
- prepareDataForDb(userContext: IUserContext, entity: Partial<T>[], isCreate?: boolean): Promise<Partial<T>[]>;
46
- prepareDataForBatchUpdate(userContext: IUserContext, entities: Partial<T>[]): Promise<Partial<T>[]>;
47
- protected prepareEntity(userContext: IUserContext, entity: T | Partial<T>, isCreate: boolean, allowId?: boolean): Promise<T | Partial<T>>;
48
- protected prepareQuery(userContext: IUserContext | undefined, query: any): any;
49
- protected prepareQueryOptions(userContext: IUserContext | undefined, queryOptions: IQueryOptions): IQueryOptions;
50
- }
@@ -1,424 +0,0 @@
1
- import { ObjectId } from 'mongodb';
2
- import moment from 'moment';
3
- import _ from 'lodash';
4
- import { DefaultQueryOptions } from '@loomcore/common/models';
5
- import { entityUtils } from '@loomcore/common/utils';
6
- import { BadRequestError, DuplicateKeyError, IdNotFoundError, NotFoundError, ServerError } from '../errors/index.js';
7
- import { apiUtils, dbUtils } from '../utils/index.js';
8
- export class GenericApiService {
9
- db;
10
- pluralResourceName;
11
- singularResourceName;
12
- collection;
13
- modelSpec;
14
- constructor(db, pluralResourceName, singularResourceName, modelSpec) {
15
- this.db = db;
16
- this.pluralResourceName = pluralResourceName;
17
- this.singularResourceName = singularResourceName;
18
- this.collection = db.collection(pluralResourceName);
19
- this.modelSpec = modelSpec;
20
- }
21
- validate(doc, isPartial = false) {
22
- if (!this.modelSpec) {
23
- return null;
24
- }
25
- const validator = isPartial ? this.modelSpec.partialValidator : this.modelSpec.validator;
26
- return entityUtils.validate(validator, doc);
27
- }
28
- validateMany(docs, isPartial = false) {
29
- if (!this.modelSpec) {
30
- return null;
31
- }
32
- const validator = isPartial ? this.modelSpec.partialValidator : this.modelSpec.validator;
33
- let allErrors = [];
34
- for (const doc of docs) {
35
- const errors = entityUtils.validate(validator, doc);
36
- if (errors && errors.length > 0) {
37
- allErrors.push(...errors);
38
- }
39
- }
40
- return allErrors.length > 0 ? allErrors : null;
41
- }
42
- getAdditionalPipelineStages() {
43
- return [];
44
- }
45
- createAggregationPipeline(userContext, query, queryOptions) {
46
- const pipeline = [
47
- { $match: query },
48
- { $facet: {
49
- data: (() => {
50
- const resultStages = [];
51
- if (queryOptions) {
52
- if (queryOptions.orderBy) {
53
- resultStages.push({
54
- $sort: {
55
- [queryOptions.orderBy]: queryOptions.sortDirection === 'asc' ? 1 : -1
56
- }
57
- });
58
- }
59
- if (queryOptions.page && queryOptions.pageSize) {
60
- resultStages.push({ $skip: (queryOptions.page - 1) * queryOptions.pageSize });
61
- resultStages.push({ $limit: queryOptions.pageSize });
62
- }
63
- }
64
- return resultStages;
65
- })(),
66
- count: [{ $count: 'total' }]
67
- } },
68
- { $project: {
69
- data: 1,
70
- total: { $arrayElemAt: ['$count.total', 0] }
71
- } }
72
- ];
73
- return pipeline;
74
- }
75
- async getAll(userContext) {
76
- const query = this.prepareQuery(userContext, {});
77
- let entities = [];
78
- if (this.getAdditionalPipelineStages().length > 0) {
79
- const pipeline = this.createAggregationPipeline(userContext, query);
80
- entities = await this.collection.aggregate(pipeline).toArray();
81
- }
82
- else {
83
- const cursor = this.collection.find(query);
84
- entities = await cursor.toArray();
85
- }
86
- return this.transformList(entities);
87
- }
88
- async get(userContext, queryOptions = { ...DefaultQueryOptions }) {
89
- const preparedOptions = this.prepareQueryOptions(userContext, queryOptions);
90
- const match = dbUtils.buildMongoMatchFromQueryOptions(preparedOptions, this.modelSpec);
91
- const additionalStages = this.getAdditionalPipelineStages();
92
- const results = [...additionalStages];
93
- if (preparedOptions.orderBy) {
94
- results.push({ $sort: { [preparedOptions.orderBy]: preparedOptions.sortDirection === 'asc' ? 1 : -1 } });
95
- }
96
- if (preparedOptions.page && preparedOptions.pageSize) {
97
- results.push({ $skip: (preparedOptions.page - 1) * preparedOptions.pageSize });
98
- results.push({ $limit: preparedOptions.pageSize });
99
- }
100
- const pipeline = [
101
- match,
102
- {
103
- $facet: {
104
- results: results,
105
- total: [
106
- { $count: 'total' }
107
- ]
108
- }
109
- }
110
- ];
111
- let pagedResult = apiUtils.getPagedResult([], 0, preparedOptions);
112
- const cursor = this.collection.aggregate(pipeline);
113
- const aggregateResult = await cursor.next();
114
- if (aggregateResult) {
115
- let total = 0;
116
- if (aggregateResult.total && aggregateResult.total.length > 0) {
117
- total = aggregateResult.total[0].total;
118
- }
119
- const entities = this.transformList(aggregateResult.results);
120
- pagedResult = apiUtils.getPagedResult(entities, total, preparedOptions);
121
- }
122
- return pagedResult;
123
- }
124
- async getById(userContext, id) {
125
- if (!entityUtils.isValidObjectId(id)) {
126
- throw new BadRequestError('id is not a valid ObjectId');
127
- }
128
- const baseQuery = { _id: new ObjectId(id) };
129
- const query = this.prepareQuery(userContext, baseQuery);
130
- let entity = null;
131
- if (this.getAdditionalPipelineStages().length > 0) {
132
- const pipeline = [
133
- { $match: query },
134
- ...this.getAdditionalPipelineStages()
135
- ];
136
- entity = await this.collection.aggregate(pipeline).next();
137
- }
138
- else {
139
- entity = await this.collection.findOne(query);
140
- }
141
- if (!entity) {
142
- throw new IdNotFoundError();
143
- }
144
- return this.transformSingle(entity);
145
- }
146
- async getCount(userContext) {
147
- const query = this.prepareQuery(userContext, {});
148
- const count = await this.collection.countDocuments(query);
149
- return count;
150
- }
151
- async create(userContext, preparedEntity) {
152
- let createdEntity = null;
153
- try {
154
- const entity = await this.onBeforeCreate(userContext, preparedEntity);
155
- const insertResult = await this.collection.insertOne(entity);
156
- if (insertResult.insertedId) {
157
- createdEntity = this.transformSingle(entity);
158
- }
159
- if (createdEntity) {
160
- await this.onAfterCreate(userContext, createdEntity);
161
- }
162
- }
163
- catch (err) {
164
- if (err.code === 11000) {
165
- throw new DuplicateKeyError(`${this.singularResourceName} already exists`);
166
- }
167
- throw new BadRequestError(`Error creating ${this.singularResourceName}`);
168
- }
169
- return createdEntity;
170
- }
171
- async createMany(userContext, preparedEntities) {
172
- let createdEntities = [];
173
- if (preparedEntities.length) {
174
- try {
175
- const entities = await this.onBeforeCreate(userContext, preparedEntities);
176
- const insertResult = await this.collection.insertMany(entities);
177
- if (insertResult.insertedIds) {
178
- createdEntities = this.transformList(entities);
179
- }
180
- await this.onAfterCreate(userContext, createdEntities);
181
- }
182
- catch (err) {
183
- if (err.code === 11000) {
184
- throw new DuplicateKeyError(`One or more ${this.pluralResourceName} already exist`);
185
- }
186
- throw new BadRequestError(`Error creating ${this.pluralResourceName}`);
187
- }
188
- }
189
- return createdEntities;
190
- }
191
- async batchUpdate(userContext, preparedEntities) {
192
- if (!preparedEntities || preparedEntities.length === 0) {
193
- return [];
194
- }
195
- const entities = await this.onBeforeUpdate(userContext, preparedEntities);
196
- const operations = [];
197
- const entityIds = [];
198
- for (const entity of entities) {
199
- const { _id, ...updateData } = entity;
200
- if (!_id || !(_id instanceof ObjectId)) {
201
- throw new BadRequestError('Each entity in a batch update must have a valid _id that has been converted to an ObjectId.');
202
- }
203
- entityIds.push(_id);
204
- operations.push({
205
- updateOne: {
206
- filter: { _id },
207
- update: { $set: updateData },
208
- },
209
- });
210
- }
211
- if (operations.length > 0) {
212
- await this.collection.bulkWrite(operations);
213
- }
214
- const query = this.prepareQuery(userContext, { _id: { $in: entityIds } });
215
- const rawUpdatedEntities = await this.collection.find(query).toArray();
216
- const updatedEntities = this.transformList(rawUpdatedEntities);
217
- if (updatedEntities.length > 0) {
218
- await this.onAfterUpdate(userContext, updatedEntities);
219
- }
220
- return updatedEntities;
221
- }
222
- async fullUpdateById(userContext, id, preparedEntity) {
223
- if (!entityUtils.isValidObjectId(id)) {
224
- throw new BadRequestError('id is not a valid ObjectId');
225
- }
226
- const baseQuery = { _id: new ObjectId(id) };
227
- const query = this.prepareQuery(userContext, baseQuery);
228
- const existingEntity = await this.collection.findOne(query);
229
- if (!existingEntity) {
230
- throw new IdNotFoundError();
231
- }
232
- const auditProperties = {
233
- _created: existingEntity._created,
234
- _createdBy: existingEntity._createdBy,
235
- };
236
- const entity = await this.onBeforeUpdate(userContext, preparedEntity);
237
- Object.assign(entity, auditProperties);
238
- const mongoUpdateResult = await this.collection.replaceOne(query, entity);
239
- if (mongoUpdateResult?.matchedCount <= 0) {
240
- throw new IdNotFoundError();
241
- }
242
- await this.onAfterUpdate(userContext, entity);
243
- const updatedEntity = await this.collection.findOne(query);
244
- return this.transformSingle(updatedEntity);
245
- }
246
- async partialUpdateById(userContext, id, preparedEntity) {
247
- if (!entityUtils.isValidObjectId(id)) {
248
- throw new BadRequestError('id is not a valid ObjectId');
249
- }
250
- const entity = await this.onBeforeUpdate(userContext, preparedEntity);
251
- const baseQuery = { _id: new ObjectId(id) };
252
- const query = this.prepareQuery(userContext, baseQuery);
253
- const updatedEntity = await this.collection.findOneAndUpdate(query, { $set: entity }, { returnDocument: 'after' });
254
- if (!updatedEntity) {
255
- throw new IdNotFoundError();
256
- }
257
- else {
258
- const typedEntity = updatedEntity;
259
- await this.onAfterUpdate(userContext, typedEntity);
260
- }
261
- return this.transformSingle(updatedEntity);
262
- }
263
- async partialUpdateByIdWithoutBeforeAndAfter(userContext, id, preparedEntity) {
264
- if (!entityUtils.isValidObjectId(id)) {
265
- throw new BadRequestError('id is not a valid ObjectId');
266
- }
267
- const baseQuery = { _id: new ObjectId(id) };
268
- const query = this.prepareQuery(userContext, baseQuery);
269
- const modifyResult = await this.collection.findOneAndUpdate(query, { $set: preparedEntity }, { returnDocument: 'after' });
270
- let updatedEntity = null;
271
- if (modifyResult?.ok === 1) {
272
- updatedEntity = modifyResult.value;
273
- }
274
- else {
275
- if (!modifyResult?.value) {
276
- throw new IdNotFoundError();
277
- }
278
- else {
279
- throw new ServerError(`Error updating ${this.singularResourceName} - ${JSON.stringify(modifyResult.lastErrorObject)}`);
280
- }
281
- }
282
- return this.transformSingle(updatedEntity);
283
- }
284
- async update(userContext, queryObject, preparedEntity) {
285
- const entity = await this.onBeforeUpdate(userContext, preparedEntity);
286
- const query = this.prepareQuery(userContext, queryObject);
287
- const mongoUpdateResult = await this.collection.updateMany(query, { $set: entity });
288
- if (mongoUpdateResult?.matchedCount <= 0) {
289
- throw new NotFoundError('No records found matching update query');
290
- }
291
- await this.onAfterUpdate(userContext, entity);
292
- const updatedEntities = await this.collection.find(query).toArray();
293
- return this.transformList(updatedEntities);
294
- }
295
- async deleteById(userContext, id) {
296
- if (!entityUtils.isValidObjectId(id)) {
297
- throw new BadRequestError('id is not a valid ObjectId');
298
- }
299
- const baseQuery = { _id: new ObjectId(id) };
300
- const query = this.prepareQuery(userContext, baseQuery);
301
- await this.onBeforeDelete(userContext, query);
302
- const deleteResult = await this.collection.deleteOne(query);
303
- if (deleteResult.deletedCount <= 0) {
304
- throw new IdNotFoundError();
305
- }
306
- await this.onAfterDelete(userContext, query);
307
- return deleteResult;
308
- }
309
- async deleteMany(userContext, queryObject) {
310
- const query = this.prepareQuery(userContext, queryObject);
311
- await this.onBeforeDelete(userContext, query);
312
- const deleteResult = await this.collection.deleteMany(query);
313
- await this.onAfterDelete(userContext, query);
314
- return deleteResult;
315
- }
316
- async find(userContext, mongoQueryObject, options) {
317
- const query = this.prepareQuery(userContext, mongoQueryObject);
318
- const cursor = this.collection.find(query, options);
319
- const entities = await cursor.toArray();
320
- return this.transformList(entities);
321
- }
322
- async findOne(userContext, mongoQueryObject, options) {
323
- const query = this.prepareQuery(userContext, mongoQueryObject);
324
- const entity = await this.collection.findOne(query, options);
325
- return this.transformSingle(entity);
326
- }
327
- auditForCreate(userContext, doc) {
328
- const now = moment().utc().toDate();
329
- const userId = userContext.user?._id?.toString() ?? 'system';
330
- doc._created = now;
331
- doc._createdBy = userId;
332
- doc._updated = now;
333
- doc._updatedBy = userId;
334
- }
335
- auditForUpdate(userContext, doc) {
336
- const userId = userContext.user?._id?.toString() ?? 'system';
337
- doc._updated = moment().utc().toDate();
338
- doc._updatedBy = userId;
339
- }
340
- async onBeforeCreate(userContext, entities) {
341
- return Promise.resolve(entities);
342
- }
343
- async onAfterCreate(userContext, entities) {
344
- return Promise.resolve(entities);
345
- }
346
- async onBeforeUpdate(userContext, entities) {
347
- return Promise.resolve(entities);
348
- }
349
- onAfterUpdate(userContext, entities) {
350
- return Promise.resolve(entities);
351
- }
352
- onBeforeDelete(userContext, queryObject) {
353
- return Promise.resolve(queryObject);
354
- }
355
- onAfterDelete(userContext, queryObject) {
356
- return Promise.resolve(queryObject);
357
- }
358
- transformList(list) {
359
- if (!list)
360
- return [];
361
- return list.map(item => this.transformSingle(item));
362
- }
363
- transformSingle(single) {
364
- if (!single)
365
- return single;
366
- if (!this.modelSpec?.fullSchema) {
367
- throw new ServerError(`Cannot transform entity: No model specification with schema provided for ${this.pluralResourceName}`);
368
- }
369
- const transformedEntity = dbUtils.convertObjectIdsToStrings(single, this.modelSpec.fullSchema);
370
- return transformedEntity;
371
- }
372
- stripSenderProvidedSystemProperties(userContext, doc, allowId = false) {
373
- const isSystemUser = userContext.user?._id === 'system';
374
- if (isSystemUser) {
375
- return;
376
- }
377
- const propertiesToIgnore = ['_orgId'];
378
- if (allowId) {
379
- propertiesToIgnore.push('_id');
380
- }
381
- for (const key in doc) {
382
- if (Object.prototype.hasOwnProperty.call(doc, key) && key.startsWith('_') && !propertiesToIgnore.includes(key)) {
383
- delete doc[key];
384
- }
385
- }
386
- }
387
- async prepareDataForDb(userContext, entity, isCreate = false) {
388
- if (Array.isArray(entity)) {
389
- return await Promise.all(entity.map(item => this.prepareEntity(userContext, item, isCreate)));
390
- }
391
- else {
392
- return await this.prepareEntity(userContext, entity, isCreate);
393
- }
394
- }
395
- async prepareDataForBatchUpdate(userContext, entities) {
396
- return Promise.all(entities.map(item => this.prepareEntity(userContext, item, false, true)));
397
- }
398
- async prepareEntity(userContext, entity, isCreate, allowId = false) {
399
- const preparedEntity = _.clone(entity);
400
- this.stripSenderProvidedSystemProperties(userContext, preparedEntity, allowId);
401
- if (this.modelSpec?.isAuditable) {
402
- if (isCreate) {
403
- this.auditForCreate(userContext, preparedEntity);
404
- }
405
- else {
406
- this.auditForUpdate(userContext, preparedEntity);
407
- }
408
- }
409
- if (!this.modelSpec?.fullSchema) {
410
- throw new ServerError(`Cannot prepare entity: No model specification with schema provided for ${this.pluralResourceName}`);
411
- }
412
- let cleanedEntity = preparedEntity;
413
- if (this.modelSpec) {
414
- cleanedEntity = this.modelSpec.decode(preparedEntity);
415
- }
416
- return dbUtils.convertStringsToObjectIds(cleanedEntity, this.modelSpec.fullSchema);
417
- }
418
- prepareQuery(userContext, query) {
419
- return query;
420
- }
421
- prepareQueryOptions(userContext, queryOptions) {
422
- return queryOptions;
423
- }
424
- }
@@ -1,27 +0,0 @@
1
- import { ObjectId } from 'mongodb';
2
- import { TSchema } from '@sinclair/typebox';
3
- import { IQueryOptions, Filter, IModelSpec } from '@loomcore/common/models';
4
- export declare const PROPERTIES_THAT_ARE_NOT_OBJECT_IDS: string[];
5
- declare function convertObjectIdsToStrings<T>(entity: T, schema?: TSchema): T;
6
- declare function convertStringsToObjectIds(entity: any, schema: TSchema): any;
7
- declare function convertStringToObjectId(value: any): ObjectId | any;
8
- declare function buildMongoMatchFromQueryOptions(queryOptions: IQueryOptions, modelSpec?: IModelSpec): {
9
- $match: any;
10
- };
11
- declare function buildSQLWhereClauseFromQueryOptions(queryOptions: IQueryOptions, columnAliasMap: {
12
- [key: string]: string;
13
- }): string;
14
- declare function addKeyValueToWhereClause(whereClause: string, key: string, value: Filter, tableAlias?: string): string;
15
- declare function appendToWhereClause(whereClause: string, condition: string): string;
16
- declare function formatValue(value: string | number | boolean | Date, isLikeOperator?: boolean): string;
17
- export declare const dbUtils: {
18
- buildMongoMatchFromQueryOptions: typeof buildMongoMatchFromQueryOptions;
19
- buildSQLWhereClauseFromQueryOptions: typeof buildSQLWhereClauseFromQueryOptions;
20
- addKeyValueToWhereClause: typeof addKeyValueToWhereClause;
21
- appendToWhereClause: typeof appendToWhereClause;
22
- formatValue: typeof formatValue;
23
- convertStringToObjectId: typeof convertStringToObjectId;
24
- convertStringsToObjectIds: typeof convertStringsToObjectIds;
25
- convertObjectIdsToStrings: typeof convertObjectIdsToStrings;
26
- };
27
- export {};