openloops 0.5.0 → 0.6.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 (224) hide show
  1. package/CHANGELOG.md +27 -0
  2. package/README.md +2 -1
  3. package/dist/common/base/base.entity.d.ts +3 -9
  4. package/dist/common/base/base.entity.js +17 -96
  5. package/dist/common/base/base.entity.mjs +19 -92
  6. package/dist/common/base/base.use-case.d.ts +2 -0
  7. package/dist/common/base/index.d.ts +1 -0
  8. package/dist/common/base/index.js +3 -1
  9. package/dist/common/base/index.mjs +1 -0
  10. package/dist/common/base/share.d.ts +12 -0
  11. package/dist/common/base/share.js +45 -0
  12. package/dist/common/base/share.mjs +21 -0
  13. package/dist/common/decorators/index.d.ts +0 -2
  14. package/dist/common/decorators/index.js +1 -5
  15. package/dist/common/decorators/index.mjs +0 -2
  16. package/dist/common/repositories/index.d.ts +1 -0
  17. package/dist/common/repositories/index.js +3 -1
  18. package/dist/common/repositories/index.mjs +1 -0
  19. package/dist/common/repositories/mongo-db.repository.d.ts +31 -11
  20. package/dist/common/repositories/mongo-db.repository.js +121 -40
  21. package/dist/common/repositories/mongo-db.repository.mjs +121 -40
  22. package/dist/common/repositories/scope.d.ts +19 -0
  23. package/dist/common/repositories/scope.js +63 -0
  24. package/dist/common/repositories/scope.mjs +40 -0
  25. package/dist/services/authentication/repositories/revoked-token.repository.d.ts +3 -3
  26. package/dist/services/authentication/repositories/revoked-token.repository.js +7 -6
  27. package/dist/services/authentication/repositories/revoked-token.repository.mjs +7 -6
  28. package/dist/services/authentication/use-cases/is-token-revoked.use-case.js +2 -1
  29. package/dist/services/authentication/use-cases/is-token-revoked.use-case.mjs +2 -1
  30. package/dist/services/authentication/use-cases/login.use-case.js +2 -1
  31. package/dist/services/authentication/use-cases/login.use-case.mjs +2 -1
  32. package/dist/services/authentication/use-cases/revoke-token.use-case.js +5 -4
  33. package/dist/services/authentication/use-cases/revoke-token.use-case.mjs +5 -4
  34. package/dist/services/automations/repositories/automation-run.repository.d.ts +10 -10
  35. package/dist/services/automations/repositories/automation-run.repository.js +21 -18
  36. package/dist/services/automations/repositories/automation-run.repository.mjs +21 -18
  37. package/dist/services/automations/repositories/automation.repository.d.ts +14 -13
  38. package/dist/services/automations/repositories/automation.repository.js +30 -18
  39. package/dist/services/automations/repositories/automation.repository.mjs +30 -18
  40. package/dist/services/automations/runner/automation-runner.d.ts +3 -0
  41. package/dist/services/automations/runner/automation-runner.js +1 -1
  42. package/dist/services/automations/runner/automation-runner.mjs +1 -1
  43. package/dist/services/automations/use-cases/attach-automation-run-chat.use-case.js +3 -2
  44. package/dist/services/automations/use-cases/attach-automation-run-chat.use-case.mjs +3 -2
  45. package/dist/services/automations/use-cases/claim-due-automations.use-case.js +7 -6
  46. package/dist/services/automations/use-cases/claim-due-automations.use-case.mjs +7 -6
  47. package/dist/services/automations/use-cases/claim-queued-automation-runs.use-case.js +9 -8
  48. package/dist/services/automations/use-cases/claim-queued-automation-runs.use-case.mjs +9 -8
  49. package/dist/services/automations/use-cases/create-automation.use-case.js +4 -5
  50. package/dist/services/automations/use-cases/create-automation.use-case.mjs +2 -3
  51. package/dist/services/automations/use-cases/delete-automation.use-case.js +6 -9
  52. package/dist/services/automations/use-cases/delete-automation.use-case.mjs +4 -7
  53. package/dist/services/automations/use-cases/finish-automation-run.use-case.js +4 -3
  54. package/dist/services/automations/use-cases/finish-automation-run.use-case.mjs +4 -3
  55. package/dist/services/automations/use-cases/get-automation-by-id.use-case.js +5 -4
  56. package/dist/services/automations/use-cases/get-automation-by-id.use-case.mjs +3 -2
  57. package/dist/services/automations/use-cases/issue-automation-event-token.use-case.js +5 -5
  58. package/dist/services/automations/use-cases/issue-automation-event-token.use-case.mjs +3 -3
  59. package/dist/services/automations/use-cases/list-automation-runs-by-automation.use-case.js +3 -1
  60. package/dist/services/automations/use-cases/list-automation-runs-by-automation.use-case.mjs +3 -1
  61. package/dist/services/automations/use-cases/list-automation-runs-by-user.use-case.js +2 -0
  62. package/dist/services/automations/use-cases/list-automation-runs-by-user.use-case.mjs +2 -0
  63. package/dist/services/automations/use-cases/list-automations-by-user.use-case.js +2 -0
  64. package/dist/services/automations/use-cases/list-automations-by-user.use-case.mjs +2 -0
  65. package/dist/services/automations/use-cases/receive-automation-event.use-case.js +7 -6
  66. package/dist/services/automations/use-cases/receive-automation-event.use-case.mjs +7 -6
  67. package/dist/services/automations/use-cases/renew-automation-lease.use-case.js +2 -1
  68. package/dist/services/automations/use-cases/renew-automation-lease.use-case.mjs +2 -1
  69. package/dist/services/automations/use-cases/revoke-automation-event-token.use-case.js +5 -5
  70. package/dist/services/automations/use-cases/revoke-automation-event-token.use-case.mjs +3 -3
  71. package/dist/services/automations/use-cases/rotate-automation-event-tokens.use-case.js +8 -8
  72. package/dist/services/automations/use-cases/rotate-automation-event-tokens.use-case.mjs +6 -6
  73. package/dist/services/automations/use-cases/start-automation-run.use-case.js +8 -6
  74. package/dist/services/automations/use-cases/start-automation-run.use-case.mjs +8 -6
  75. package/dist/services/automations/use-cases/sync-automation-run.use-case.js +6 -5
  76. package/dist/services/automations/use-cases/sync-automation-run.use-case.mjs +6 -5
  77. package/dist/services/automations/use-cases/sync-automation-runs.use-case.js +3 -2
  78. package/dist/services/automations/use-cases/sync-automation-runs.use-case.mjs +3 -2
  79. package/dist/services/automations/use-cases/update-automation.use-case.js +8 -7
  80. package/dist/services/automations/use-cases/update-automation.use-case.mjs +6 -5
  81. package/dist/services/automations/utils/open-run.d.ts +2 -0
  82. package/dist/services/automations/utils/open-run.js +2 -2
  83. package/dist/services/automations/utils/open-run.mjs +2 -2
  84. package/dist/services/automations/utils/previous-run.js +3 -2
  85. package/dist/services/automations/utils/previous-run.mjs +3 -2
  86. package/dist/services/automations/utils/runner-scope.d.ts +3 -0
  87. package/dist/{common/decorators/admin-only/index.js → services/automations/utils/runner-scope.js} +15 -5
  88. package/dist/services/automations/utils/runner-scope.mjs +8 -0
  89. package/dist/services/chats/repositories/chat.repository.d.ts +7 -6
  90. package/dist/services/chats/repositories/chat.repository.js +16 -11
  91. package/dist/services/chats/repositories/chat.repository.mjs +16 -11
  92. package/dist/services/chats/use-cases/create-chat.use-case.js +4 -5
  93. package/dist/services/chats/use-cases/create-chat.use-case.mjs +2 -3
  94. package/dist/services/chats/use-cases/delete-chat.use-case.js +7 -9
  95. package/dist/services/chats/use-cases/delete-chat.use-case.mjs +5 -7
  96. package/dist/services/chats/use-cases/get-chat-by-id.use-case.js +3 -2
  97. package/dist/services/chats/use-cases/get-chat-by-id.use-case.mjs +3 -2
  98. package/dist/services/chats/use-cases/interrupt-execution.use-case.js +5 -7
  99. package/dist/services/chats/use-cases/interrupt-execution.use-case.mjs +3 -5
  100. package/dist/services/chats/use-cases/list-chats-by-props.use-case.js +3 -2
  101. package/dist/services/chats/use-cases/list-chats-by-props.use-case.mjs +3 -2
  102. package/dist/services/chats/use-cases/list-chats-by-user.use-case.js +2 -0
  103. package/dist/services/chats/use-cases/list-chats-by-user.use-case.mjs +2 -0
  104. package/dist/services/chats/use-cases/update-chat.use-case.js +5 -7
  105. package/dist/services/chats/use-cases/update-chat.use-case.mjs +3 -5
  106. package/dist/services/llmcalls/repositories/llmcall.repository.d.ts +7 -6
  107. package/dist/services/llmcalls/repositories/llmcall.repository.js +16 -11
  108. package/dist/services/llmcalls/repositories/llmcall.repository.mjs +16 -11
  109. package/dist/services/llmcalls/use-cases/create-llmcall.use-case.js +4 -5
  110. package/dist/services/llmcalls/use-cases/create-llmcall.use-case.mjs +2 -3
  111. package/dist/services/llmcalls/use-cases/delete-llmcall.use-case.js +6 -8
  112. package/dist/services/llmcalls/use-cases/delete-llmcall.use-case.mjs +4 -6
  113. package/dist/services/llmcalls/use-cases/get-llm-usage-by-chat.use-case.js +4 -3
  114. package/dist/services/llmcalls/use-cases/get-llm-usage-by-chat.use-case.mjs +2 -1
  115. package/dist/services/llmcalls/use-cases/get-llm-usage-by-message.use-case.js +4 -3
  116. package/dist/services/llmcalls/use-cases/get-llm-usage-by-message.use-case.mjs +2 -1
  117. package/dist/services/llmcalls/use-cases/get-llm-usage-stats.use-case.js +2 -1
  118. package/dist/services/llmcalls/use-cases/get-llm-usage-stats.use-case.mjs +2 -1
  119. package/dist/services/llmcalls/use-cases/get-llmcall-by-id.use-case.js +6 -4
  120. package/dist/services/llmcalls/use-cases/get-llmcall-by-id.use-case.mjs +4 -2
  121. package/dist/services/llmcalls/use-cases/list-llmcalls-by-props.use-case.js +3 -2
  122. package/dist/services/llmcalls/use-cases/list-llmcalls-by-props.use-case.mjs +3 -2
  123. package/dist/services/llmcalls/use-cases/update-llmcall.use-case.js +5 -7
  124. package/dist/services/llmcalls/use-cases/update-llmcall.use-case.mjs +3 -5
  125. package/dist/services/mcp-servers/repositories/mcpserver.repository.d.ts +8 -7
  126. package/dist/services/mcp-servers/repositories/mcpserver.repository.js +18 -12
  127. package/dist/services/mcp-servers/repositories/mcpserver.repository.mjs +18 -12
  128. package/dist/services/mcp-servers/use-cases/create-mcpserver.use-case.js +4 -5
  129. package/dist/services/mcp-servers/use-cases/create-mcpserver.use-case.mjs +2 -3
  130. package/dist/services/mcp-servers/use-cases/decide-mcpserver-trust.use-case.js +8 -9
  131. package/dist/services/mcp-servers/use-cases/decide-mcpserver-trust.use-case.mjs +6 -7
  132. package/dist/services/mcp-servers/use-cases/delete-mcpserver.use-case.js +6 -8
  133. package/dist/services/mcp-servers/use-cases/delete-mcpserver.use-case.mjs +4 -6
  134. package/dist/services/mcp-servers/use-cases/get-mcpserver-by-id.use-case.js +5 -4
  135. package/dist/services/mcp-servers/use-cases/get-mcpserver-by-id.use-case.mjs +3 -2
  136. package/dist/services/mcp-servers/use-cases/list-all-mcpservers-by-user.use-case.js +4 -3
  137. package/dist/services/mcp-servers/use-cases/list-all-mcpservers-by-user.use-case.mjs +2 -1
  138. package/dist/services/mcp-servers/use-cases/update-mcpserver.use-case.js +8 -11
  139. package/dist/services/mcp-servers/use-cases/update-mcpserver.use-case.mjs +6 -9
  140. package/dist/services/mcp-servers/use-cases/verify-mcpserver-connection.use-case.js +9 -9
  141. package/dist/services/mcp-servers/use-cases/verify-mcpserver-connection.use-case.mjs +7 -7
  142. package/dist/services/tasks/repositories/agent-action.repository.d.ts +4 -4
  143. package/dist/services/tasks/repositories/agent-action.repository.js +9 -7
  144. package/dist/services/tasks/repositories/agent-action.repository.mjs +9 -7
  145. package/dist/services/tasks/repositories/agent-task.repository.d.ts +5 -5
  146. package/dist/services/tasks/repositories/agent-task.repository.js +11 -9
  147. package/dist/services/tasks/repositories/agent-task.repository.mjs +11 -9
  148. package/dist/services/tasks/use-cases/create-agent-action.use-case.js +4 -5
  149. package/dist/services/tasks/use-cases/create-agent-action.use-case.mjs +2 -3
  150. package/dist/services/tasks/use-cases/create-agent-task.use-case.js +4 -5
  151. package/dist/services/tasks/use-cases/create-agent-task.use-case.mjs +2 -3
  152. package/dist/services/tasks/use-cases/get-agent-task-by-id.use-case.js +5 -4
  153. package/dist/services/tasks/use-cases/get-agent-task-by-id.use-case.mjs +3 -2
  154. package/dist/services/tasks/use-cases/list-agent-actions-by-task.use-case.js +3 -1
  155. package/dist/services/tasks/use-cases/list-agent-actions-by-task.use-case.mjs +3 -1
  156. package/dist/services/tasks/use-cases/list-agent-tasks-by-chat.use-case.js +3 -1
  157. package/dist/services/tasks/use-cases/list-agent-tasks-by-chat.use-case.mjs +3 -1
  158. package/dist/services/tasks/use-cases/update-agent-action.use-case.js +5 -7
  159. package/dist/services/tasks/use-cases/update-agent-action.use-case.mjs +3 -5
  160. package/dist/services/tasks/use-cases/update-agent-task.use-case.js +5 -7
  161. package/dist/services/tasks/use-cases/update-agent-task.use-case.mjs +3 -5
  162. package/dist/services/traces/repositories/agent-trace.repository.d.ts +3 -3
  163. package/dist/services/traces/repositories/agent-trace.repository.js +6 -5
  164. package/dist/services/traces/repositories/agent-trace.repository.mjs +6 -5
  165. package/dist/services/traces/use-cases/create-agent-traces.use-case.js +5 -9
  166. package/dist/services/traces/use-cases/create-agent-traces.use-case.mjs +3 -7
  167. package/dist/services/traces/use-cases/list-agent-traces-by-chat.use-case.js +3 -1
  168. package/dist/services/traces/use-cases/list-agent-traces-by-chat.use-case.mjs +3 -1
  169. package/dist/services/users/entities/user-acquisition.js +3 -16
  170. package/dist/services/users/entities/user-acquisition.mjs +4 -11
  171. package/dist/services/users/entities/user-marketing.js +3 -16
  172. package/dist/services/users/entities/user-marketing.mjs +4 -11
  173. package/dist/services/users/entities/user.entity.js +0 -3
  174. package/dist/services/users/entities/user.entity.mjs +1 -4
  175. package/dist/services/users/repositories/user.repository.d.ts +8 -7
  176. package/dist/services/users/repositories/user.repository.js +26 -12
  177. package/dist/services/users/repositories/user.repository.mjs +26 -12
  178. package/dist/services/users/use-cases/create-user.use-case.d.ts +0 -1
  179. package/dist/services/users/use-cases/create-user.use-case.js +5 -5
  180. package/dist/services/users/use-cases/create-user.use-case.mjs +3 -3
  181. package/dist/services/users/use-cases/delete-user.use-case.js +7 -9
  182. package/dist/services/users/use-cases/delete-user.use-case.mjs +5 -7
  183. package/dist/services/users/use-cases/get-user-by-id.use-case.js +6 -4
  184. package/dist/services/users/use-cases/get-user-by-id.use-case.mjs +4 -2
  185. package/dist/services/users/use-cases/list-users-by-props.use-case.js +3 -2
  186. package/dist/services/users/use-cases/list-users-by-props.use-case.mjs +3 -2
  187. package/dist/services/users/use-cases/update-user.use-case.js +5 -7
  188. package/dist/services/users/use-cases/update-user.use-case.mjs +3 -5
  189. package/dist/services/workspaces/repositories/workspace-item.repository.d.ts +8 -8
  190. package/dist/services/workspaces/repositories/workspace-item.repository.js +17 -14
  191. package/dist/services/workspaces/repositories/workspace-item.repository.mjs +17 -14
  192. package/dist/services/workspaces/use-cases/delete-workspace-item.use-case.js +6 -3
  193. package/dist/services/workspaces/use-cases/delete-workspace-item.use-case.mjs +4 -1
  194. package/dist/services/workspaces/use-cases/get-workspace-item.use-case.js +4 -3
  195. package/dist/services/workspaces/use-cases/get-workspace-item.use-case.mjs +2 -1
  196. package/dist/services/workspaces/use-cases/list-workspace-items.use-case.js +2 -0
  197. package/dist/services/workspaces/use-cases/list-workspace-items.use-case.mjs +2 -0
  198. package/dist/services/workspaces/use-cases/save-workspace-item.use-case.js +8 -8
  199. package/dist/services/workspaces/use-cases/save-workspace-item.use-case.mjs +6 -6
  200. package/dist/services/workspaces/use-cases/search-workspace-items.use-case.js +2 -0
  201. package/dist/services/workspaces/use-cases/search-workspace-items.use-case.mjs +2 -0
  202. package/docs/README.md +2 -1
  203. package/docs/authorization.md +77 -0
  204. package/docs/automations.md +4 -4
  205. package/docs/integration-guide.md +8 -10
  206. package/docs/llms.txt +1 -0
  207. package/docs/users.md +6 -4
  208. package/examples/automations/list-automation-runs.ts +2 -0
  209. package/examples/automations/run-automation-now.ts +3 -1
  210. package/examples/chats/delete-chat.ts +1 -1
  211. package/examples/users/create-user.ts +2 -1
  212. package/examples/users/list-users.ts +1 -0
  213. package/package.json +1 -1
  214. package/dist/common/decorators/admin-only/admin-only.d.ts +0 -2
  215. package/dist/common/decorators/admin-only/admin-only.js +0 -52
  216. package/dist/common/decorators/admin-only/admin-only.mjs +0 -28
  217. package/dist/common/decorators/admin-only/index.d.ts +0 -1
  218. package/dist/common/decorators/admin-only/index.mjs +0 -1
  219. package/dist/common/decorators/owner-only/index.d.ts +0 -1
  220. package/dist/common/decorators/owner-only/index.js +0 -22
  221. package/dist/common/decorators/owner-only/index.mjs +0 -1
  222. package/dist/common/decorators/owner-only/owner-only.d.ts +0 -2
  223. package/dist/common/decorators/owner-only/owner-only.js +0 -52
  224. package/dist/common/decorators/owner-only/owner-only.mjs +0 -28
package/CHANGELOG.md CHANGED
@@ -2,6 +2,33 @@
2
2
 
3
3
  All notable changes to `openloops` are written here, newest first. The format follows [Keep a Changelog](https://keepachangelog.com) and the versions follow [semantic versioning](https://semver.org), where a minor version can still break the API while the package is in beta. Changes before 0.4.0 are not recorded here, see the commits and the tags.
4
4
 
5
+ ## 0.6.0
6
+
7
+ This version breaks the API of the use cases. The functions that read or write the records of a user need the `currentUser` or a `scope` (login, the tokens, the preview of runs, the events and the functions of the runner do not), creating a record takes its owner from the scope and not from the payload, and the ownership of the records is checked by the repository. Follow the [migration steps](docs/authorization.md#migrating-from-05) when you update.
8
+
9
+ ### Added
10
+
11
+ - `Scope`, exported from `openloops/base`. Every repository receives one and filters by owner in the same query that reads or writes, so a record that is not the user's is answered as not found (a 404 `HttpError`, `{}` in `getChatById`, `null` in the other getters). `Scope.canView`, `canEdit` and `canManage` take the `currentUser`, `Scope.asAdmin` fails with 403 when the user is not an admin, and `Scope.system(reason)` is for workers and flows without a user.
12
+ - Every use case accepts `scope`, to override the one it builds from `currentUser`.
13
+ - The repository removes the fields of control (`ownerId`, `sharedWith`, `createdAt`, `createdBy`, `updatedAt`, `updatedBy`, `deletedAt`, `deletedBy`, `archivedAt`, `archivedBy`, `__v`, and `role`, `marketing.drawNumber` and `acquisition.referrerCode` in the users) from the payload of a user and fills them in itself. `createOne` returns the stored document, and there are `softDelete` and `createMany` with the same behavior.
14
+ - `docs/authorization.md`.
15
+
16
+ ### Changed
17
+
18
+ - **Creating records.** `createChat`, `createMcpServer`, `createAutomation`, `createUser`, `createLLMCall`, `createAgentTask`, `createAgentAction`, `createAgentTraces` and `saveWorkspaceItem` set the owner and the author from the scope. A user scope ignores the `ownerId` and the other fields of control of the payload, and a record cannot be created without a `currentUser` or a `scope` (401). To create a record for another user, or without a user, pass `Scope.asAdmin(currentUser)` or `Scope.system(reason)`. See [creating records](docs/authorization.md#creating-records).
19
+ - `getAutomationById`, `listAutomationRunsByAutomation`, `startAutomationRun` and `runner.runNow` need the `currentUser` and only reach the automations of that user. `runner.runNow` also accepts `scope`.
20
+ - A call without `currentUser` and without `scope` fails with 401.
21
+ - `issueAutomationEventToken`, `revokeAutomationEventToken` and `rotateAutomationEventTokens` are for the owner, and an admin needs `Scope.asAdmin`. They answer 404, not 403, to anyone else.
22
+ - The hard delete is for `Scope.asAdmin` or `Scope.system`, and a user that asks for it gets 403.
23
+ - Updating or deleting a record that does not exist or is not the user's answers a 404 `HttpError` and not a generic error.
24
+ - `sharedWith` is a list of `{ userId, permission }`, where `permission` is `read`, `write` or `manage`, and not a list of ids. Nothing reads it yet.
25
+ - `createUser` creates the account with the scope you pass, and keeps `role`, the draw number and the referrer code only with a trusted scope.
26
+
27
+ ### Removed
28
+
29
+ - The `sanitize` parameter of `createUser`.
30
+ - `setOwner`, `setCreatedBy`, `setUpdatedBy`, `setDeletedBy` and `setArchivedBy` of `BaseEntity`, and the `AdminOnly` and `OwnerOnly` decorators. The repository does that work.
31
+
5
32
  ## 0.5.0
6
33
 
7
34
  This version breaks the shape of the automations. Follow the [migration steps](docs/automations.md#migrating-to-050) when you update.
package/README.md CHANGED
@@ -281,13 +281,14 @@ Every pattern above, and a lot more, lives as a runnable example in [`examples/`
281
281
  | `openloops/users` | User management |
282
282
  | `openloops/authentication` | Login and token issuing |
283
283
  | `openloops/llmcalls` | LLM usage and cost tracking |
284
- | `openloops/base` | `CurrentUser`, `CurrentSession` |
284
+ | `openloops/base` | `CurrentUser`, `CurrentSession`, `Scope` |
285
285
  | `openloops/common` | `Logger` and shared utilities |
286
286
  | `openloops/clients` | `LLMClient` and the HTTP clients used by built-in tools (e.g. Serper) |
287
287
 
288
288
  ## Learn more
289
289
 
290
290
  - [Integration guide](docs/integration-guide.md)
291
+ - [Authorization](docs/authorization.md)
291
292
  - [Documentation index](docs/README.md)
292
293
  - [The `Agent`](docs/agent.md)
293
294
  - [Building an `AgentLoop`](docs/loops.md)
@@ -1,6 +1,6 @@
1
1
  import 'reflect-metadata';
2
2
  import { ObjectId } from 'bson';
3
- import { CurrentUser } from './index.js';
3
+ import { Share, ShareProps } from './share';
4
4
  export interface BaseEntityProps {
5
5
  _id?: string | ObjectId;
6
6
  __v?: number;
@@ -14,7 +14,7 @@ export interface BaseEntityProps {
14
14
  deletedReason?: string;
15
15
  archivedAt?: string | Date;
16
16
  archivedBy?: string | ObjectId;
17
- sharedWith?: string[] | ObjectId[];
17
+ sharedWith?: ShareProps[];
18
18
  toUnset?: Record<string, unknown>;
19
19
  }
20
20
  export interface Document extends BaseEntityProps {
@@ -33,7 +33,7 @@ export declare class BaseEntity {
33
33
  deletedReason?: string;
34
34
  archivedAt?: Date;
35
35
  archivedBy?: ObjectId;
36
- sharedWith?: ObjectId[];
36
+ sharedWith?: Share[];
37
37
  toUnset?: Record<string, unknown>;
38
38
  static toObjectId(id?: string | ObjectId): ObjectId | undefined;
39
39
  private static toDate;
@@ -43,14 +43,8 @@ export declare class BaseEntity {
43
43
  toJSON(): Document;
44
44
  setDefaults(): void;
45
45
  setId(id: string | ObjectId): void;
46
- setOwner(currentUser?: CurrentUser): void;
47
- setCreatedBy(currentUser?: CurrentUser, sanitize?: boolean): void;
48
- setUpdatedBy(currentUser?: CurrentUser): void;
49
- setDeletedBy(currentUser?: CurrentUser): void;
50
- setArchivedBy(currentUser?: CurrentUser): void;
51
46
  toDocument(): Document;
52
47
  toFlatDocument(): Document;
53
48
  stringify(): string;
54
49
  unset(fields: (keyof this)[]): {} | undefined;
55
- private sanitize;
56
50
  }
@@ -16,14 +16,6 @@ var __copyProps = (to, from, except, desc) => {
16
16
  return to;
17
17
  };
18
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
- var __decorateClass = (decorators, target, key, kind) => {
20
- var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target;
21
- for (var i = decorators.length - 1, decorator; i >= 0; i--)
22
- if (decorator = decorators[i])
23
- result = (kind ? decorator(target, key, result) : decorator(result)) || result;
24
- if (kind && result) __defProp(target, key, result);
25
- return result;
26
- };
27
19
  var base_entity_exports = {};
28
20
  __export(base_entity_exports, {
29
21
  BaseEntity: () => BaseEntity
@@ -34,7 +26,8 @@ var import_bson = require("bson");
34
26
  var import_decorators = require("../decorators/index.js");
35
27
  var import_helpers = require("../helpers/index.js");
36
28
  var import_utils = require("./utils");
37
- const _BaseEntity = class _BaseEntity {
29
+ var import_share = require("./share");
30
+ class BaseEntity {
38
31
  _id;
39
32
  __v;
40
33
  ownerId;
@@ -70,19 +63,19 @@ const _BaseEntity = class _BaseEntity {
70
63
  return (0, import_utils.deleteEmptyFields)(json);
71
64
  }
72
65
  constructor(props) {
73
- this._id = _BaseEntity.toObjectId(props._id);
74
- this.__v = _BaseEntity.toNumber(props.__v);
75
- this.ownerId = _BaseEntity.toObjectId(props.ownerId);
76
- this.createdAt = _BaseEntity.toDate(props.createdAt);
77
- this.createdBy = _BaseEntity.toObjectId(props.createdBy);
78
- this.updatedAt = _BaseEntity.toDate(props.updatedAt);
79
- this.updatedBy = _BaseEntity.toObjectId(props.updatedBy);
80
- this.deletedAt = _BaseEntity.toDate(props.deletedAt);
81
- this.deletedBy = _BaseEntity.toObjectId(props.deletedBy);
66
+ this._id = BaseEntity.toObjectId(props._id);
67
+ this.__v = BaseEntity.toNumber(props.__v);
68
+ this.ownerId = BaseEntity.toObjectId(props.ownerId);
69
+ this.createdAt = BaseEntity.toDate(props.createdAt);
70
+ this.createdBy = BaseEntity.toObjectId(props.createdBy);
71
+ this.updatedAt = BaseEntity.toDate(props.updatedAt);
72
+ this.updatedBy = BaseEntity.toObjectId(props.updatedBy);
73
+ this.deletedAt = BaseEntity.toDate(props.deletedAt);
74
+ this.deletedBy = BaseEntity.toObjectId(props.deletedBy);
82
75
  this.deletedReason = (0, import_helpers.sanitizeString)(props.deletedReason);
83
- this.archivedAt = _BaseEntity.toDate(props.archivedAt);
84
- this.archivedBy = _BaseEntity.toObjectId(props.archivedBy);
85
- this.sharedWith = props.sharedWith?.length ? props.sharedWith.map((id) => _BaseEntity.toObjectId(id)) : void 0;
76
+ this.archivedAt = BaseEntity.toDate(props.archivedAt);
77
+ this.archivedBy = BaseEntity.toObjectId(props.archivedBy);
78
+ this.sharedWith = (0, import_share.createShares)(props.sharedWith);
86
79
  }
87
80
  toJSON() {
88
81
  return {
@@ -98,42 +91,14 @@ const _BaseEntity = class _BaseEntity {
98
91
  deletedReason: this.deletedReason,
99
92
  archivedAt: this.archivedAt,
100
93
  archivedBy: this.archivedBy,
101
- sharedWith: this.sharedWith?.length ? this.sharedWith.map((id) => id.toHexString()) : void 0,
94
+ sharedWith: this.sharedWith?.length ? this.sharedWith : void 0,
102
95
  toUnset: this.toUnset
103
96
  };
104
97
  }
105
98
  setDefaults() {
106
99
  }
107
100
  setId(id) {
108
- this._id = _BaseEntity.toObjectId(id);
109
- }
110
- setOwner(currentUser) {
111
- if (!currentUser) return;
112
- this.ownerId = _BaseEntity.toObjectId(currentUser.userId);
113
- }
114
- setCreatedBy(currentUser, sanitize = true) {
115
- if (sanitize) this.sanitize(currentUser);
116
- this.createdAt = /* @__PURE__ */ new Date();
117
- this.createdBy = _BaseEntity.toObjectId(currentUser?.userId);
118
- this.setDefaults();
119
- }
120
- setUpdatedBy(currentUser) {
121
- if (!currentUser) return;
122
- this.sanitize(currentUser);
123
- this.updatedAt = /* @__PURE__ */ new Date();
124
- this.updatedBy = _BaseEntity.toObjectId(currentUser.userId);
125
- }
126
- setDeletedBy(currentUser) {
127
- if (!currentUser) return;
128
- this.sanitize(currentUser);
129
- this.deletedAt = /* @__PURE__ */ new Date();
130
- this.deletedBy = _BaseEntity.toObjectId(currentUser.userId);
131
- }
132
- setArchivedBy(currentUser) {
133
- if (!currentUser) return;
134
- this.sanitize(currentUser);
135
- this.archivedAt = /* @__PURE__ */ new Date();
136
- this.archivedBy = _BaseEntity.toObjectId(currentUser.userId);
101
+ this._id = BaseEntity.toObjectId(id);
137
102
  }
138
103
  toDocument() {
139
104
  return (0, import_utils.deleteEmptyFields)(this.toJSON());
@@ -156,51 +121,7 @@ const _BaseEntity = class _BaseEntity {
156
121
  delete this[field];
157
122
  });
158
123
  }
159
- sanitize(currentUser) {
160
- if (!currentUser?.isAdmin()) {
161
- for (const key in this) {
162
- const isAdminOnly = Reflect.getMetadata("adminOnly", this.constructor.prototype, key);
163
- if (isAdminOnly) {
164
- delete this[key];
165
- }
166
- }
167
- }
168
- }
169
- };
170
- __decorateClass([
171
- import_decorators.AdminOnly
172
- ], _BaseEntity.prototype, "__v", 2);
173
- __decorateClass([
174
- import_decorators.AdminOnly
175
- ], _BaseEntity.prototype, "ownerId", 2);
176
- __decorateClass([
177
- import_decorators.AdminOnly
178
- ], _BaseEntity.prototype, "createdAt", 2);
179
- __decorateClass([
180
- import_decorators.AdminOnly
181
- ], _BaseEntity.prototype, "createdBy", 2);
182
- __decorateClass([
183
- import_decorators.AdminOnly
184
- ], _BaseEntity.prototype, "updatedAt", 2);
185
- __decorateClass([
186
- import_decorators.AdminOnly
187
- ], _BaseEntity.prototype, "updatedBy", 2);
188
- __decorateClass([
189
- import_decorators.AdminOnly
190
- ], _BaseEntity.prototype, "deletedAt", 2);
191
- __decorateClass([
192
- import_decorators.AdminOnly
193
- ], _BaseEntity.prototype, "deletedBy", 2);
194
- __decorateClass([
195
- import_decorators.AdminOnly
196
- ], _BaseEntity.prototype, "archivedAt", 2);
197
- __decorateClass([
198
- import_decorators.AdminOnly
199
- ], _BaseEntity.prototype, "archivedBy", 2);
200
- __decorateClass([
201
- import_decorators.OwnerOnly
202
- ], _BaseEntity.prototype, "sharedWith", 2);
203
- let BaseEntity = _BaseEntity;
124
+ }
204
125
  // Annotate the CommonJS export names for ESM import in node:
205
126
  0 && (module.exports = {
206
127
  BaseEntity
@@ -1,12 +1,11 @@
1
- import {
2
- __decorateClass
3
- } from "../../chunk-CF3WOMB3.mjs";
1
+ import "../../chunk-CF3WOMB3.mjs";
4
2
  import "reflect-metadata";
5
3
  import { ObjectId } from "bson";
6
- import { AdminOnly, OwnerOnly, handleMaskInLogs } from "../decorators/index.js";
4
+ import { handleMaskInLogs } from "../decorators/index.js";
7
5
  import { sanitizeString } from "../helpers/index.js";
8
6
  import { deleteEmptyFields, flattenObject } from "./utils";
9
- const _BaseEntity = class _BaseEntity {
7
+ import { createShares } from "./share";
8
+ class BaseEntity {
10
9
  _id;
11
10
  __v;
12
11
  ownerId;
@@ -42,19 +41,19 @@ const _BaseEntity = class _BaseEntity {
42
41
  return deleteEmptyFields(json);
43
42
  }
44
43
  constructor(props) {
45
- this._id = _BaseEntity.toObjectId(props._id);
46
- this.__v = _BaseEntity.toNumber(props.__v);
47
- this.ownerId = _BaseEntity.toObjectId(props.ownerId);
48
- this.createdAt = _BaseEntity.toDate(props.createdAt);
49
- this.createdBy = _BaseEntity.toObjectId(props.createdBy);
50
- this.updatedAt = _BaseEntity.toDate(props.updatedAt);
51
- this.updatedBy = _BaseEntity.toObjectId(props.updatedBy);
52
- this.deletedAt = _BaseEntity.toDate(props.deletedAt);
53
- this.deletedBy = _BaseEntity.toObjectId(props.deletedBy);
44
+ this._id = BaseEntity.toObjectId(props._id);
45
+ this.__v = BaseEntity.toNumber(props.__v);
46
+ this.ownerId = BaseEntity.toObjectId(props.ownerId);
47
+ this.createdAt = BaseEntity.toDate(props.createdAt);
48
+ this.createdBy = BaseEntity.toObjectId(props.createdBy);
49
+ this.updatedAt = BaseEntity.toDate(props.updatedAt);
50
+ this.updatedBy = BaseEntity.toObjectId(props.updatedBy);
51
+ this.deletedAt = BaseEntity.toDate(props.deletedAt);
52
+ this.deletedBy = BaseEntity.toObjectId(props.deletedBy);
54
53
  this.deletedReason = sanitizeString(props.deletedReason);
55
- this.archivedAt = _BaseEntity.toDate(props.archivedAt);
56
- this.archivedBy = _BaseEntity.toObjectId(props.archivedBy);
57
- this.sharedWith = props.sharedWith?.length ? props.sharedWith.map((id) => _BaseEntity.toObjectId(id)) : void 0;
54
+ this.archivedAt = BaseEntity.toDate(props.archivedAt);
55
+ this.archivedBy = BaseEntity.toObjectId(props.archivedBy);
56
+ this.sharedWith = createShares(props.sharedWith);
58
57
  }
59
58
  toJSON() {
60
59
  return {
@@ -70,42 +69,14 @@ const _BaseEntity = class _BaseEntity {
70
69
  deletedReason: this.deletedReason,
71
70
  archivedAt: this.archivedAt,
72
71
  archivedBy: this.archivedBy,
73
- sharedWith: this.sharedWith?.length ? this.sharedWith.map((id) => id.toHexString()) : void 0,
72
+ sharedWith: this.sharedWith?.length ? this.sharedWith : void 0,
74
73
  toUnset: this.toUnset
75
74
  };
76
75
  }
77
76
  setDefaults() {
78
77
  }
79
78
  setId(id) {
80
- this._id = _BaseEntity.toObjectId(id);
81
- }
82
- setOwner(currentUser) {
83
- if (!currentUser) return;
84
- this.ownerId = _BaseEntity.toObjectId(currentUser.userId);
85
- }
86
- setCreatedBy(currentUser, sanitize = true) {
87
- if (sanitize) this.sanitize(currentUser);
88
- this.createdAt = /* @__PURE__ */ new Date();
89
- this.createdBy = _BaseEntity.toObjectId(currentUser?.userId);
90
- this.setDefaults();
91
- }
92
- setUpdatedBy(currentUser) {
93
- if (!currentUser) return;
94
- this.sanitize(currentUser);
95
- this.updatedAt = /* @__PURE__ */ new Date();
96
- this.updatedBy = _BaseEntity.toObjectId(currentUser.userId);
97
- }
98
- setDeletedBy(currentUser) {
99
- if (!currentUser) return;
100
- this.sanitize(currentUser);
101
- this.deletedAt = /* @__PURE__ */ new Date();
102
- this.deletedBy = _BaseEntity.toObjectId(currentUser.userId);
103
- }
104
- setArchivedBy(currentUser) {
105
- if (!currentUser) return;
106
- this.sanitize(currentUser);
107
- this.archivedAt = /* @__PURE__ */ new Date();
108
- this.archivedBy = _BaseEntity.toObjectId(currentUser.userId);
79
+ this._id = BaseEntity.toObjectId(id);
109
80
  }
110
81
  toDocument() {
111
82
  return deleteEmptyFields(this.toJSON());
@@ -128,51 +99,7 @@ const _BaseEntity = class _BaseEntity {
128
99
  delete this[field];
129
100
  });
130
101
  }
131
- sanitize(currentUser) {
132
- if (!currentUser?.isAdmin()) {
133
- for (const key in this) {
134
- const isAdminOnly = Reflect.getMetadata("adminOnly", this.constructor.prototype, key);
135
- if (isAdminOnly) {
136
- delete this[key];
137
- }
138
- }
139
- }
140
- }
141
- };
142
- __decorateClass([
143
- AdminOnly
144
- ], _BaseEntity.prototype, "__v", 2);
145
- __decorateClass([
146
- AdminOnly
147
- ], _BaseEntity.prototype, "ownerId", 2);
148
- __decorateClass([
149
- AdminOnly
150
- ], _BaseEntity.prototype, "createdAt", 2);
151
- __decorateClass([
152
- AdminOnly
153
- ], _BaseEntity.prototype, "createdBy", 2);
154
- __decorateClass([
155
- AdminOnly
156
- ], _BaseEntity.prototype, "updatedAt", 2);
157
- __decorateClass([
158
- AdminOnly
159
- ], _BaseEntity.prototype, "updatedBy", 2);
160
- __decorateClass([
161
- AdminOnly
162
- ], _BaseEntity.prototype, "deletedAt", 2);
163
- __decorateClass([
164
- AdminOnly
165
- ], _BaseEntity.prototype, "deletedBy", 2);
166
- __decorateClass([
167
- AdminOnly
168
- ], _BaseEntity.prototype, "archivedAt", 2);
169
- __decorateClass([
170
- AdminOnly
171
- ], _BaseEntity.prototype, "archivedBy", 2);
172
- __decorateClass([
173
- OwnerOnly
174
- ], _BaseEntity.prototype, "sharedWith", 2);
175
- let BaseEntity = _BaseEntity;
102
+ }
176
103
  export {
177
104
  BaseEntity
178
105
  };
@@ -1,9 +1,11 @@
1
1
  import { CurrentUser, CurrentSession } from './index.js';
2
2
  import { HttpError } from '../error/index.js';
3
+ import { Scope } from '../repositories/index.js';
3
4
  import { BaseApp } from './base.app';
4
5
  export interface BaseUseCaseParams {
5
6
  currentUser?: CurrentUser;
6
7
  currentSession?: CurrentSession;
8
+ scope?: Scope;
7
9
  }
8
10
  export interface BaseUseCaseOutput {
9
11
  message?: string;
@@ -1,2 +1,3 @@
1
1
  export * from './current-session';
2
2
  export * from './current-user';
3
+ export * from '../repositories/scope';
@@ -17,8 +17,10 @@ var base_exports = {};
17
17
  module.exports = __toCommonJS(base_exports);
18
18
  __reExport(base_exports, require("./current-session"), module.exports);
19
19
  __reExport(base_exports, require("./current-user"), module.exports);
20
+ __reExport(base_exports, require("../repositories/scope"), module.exports);
20
21
  // Annotate the CommonJS export names for ESM import in node:
21
22
  0 && (module.exports = {
22
23
  ...require("./current-session"),
23
- ...require("./current-user")
24
+ ...require("./current-user"),
25
+ ...require("../repositories/scope")
24
26
  });
@@ -1,2 +1,3 @@
1
1
  export * from "./current-session";
2
2
  export * from "./current-user";
3
+ export * from "../repositories/scope";
@@ -0,0 +1,12 @@
1
+ import { ObjectId } from 'bson';
2
+ export declare const SHARE_PERMISSIONS: readonly ["read", "write", "manage"];
3
+ export type SharePermission = typeof SHARE_PERMISSIONS[number];
4
+ export interface ShareProps {
5
+ userId: string | ObjectId;
6
+ permission: SharePermission;
7
+ }
8
+ export interface Share {
9
+ userId: ObjectId;
10
+ permission: SharePermission;
11
+ }
12
+ export declare const createShares: (props?: ShareProps[]) => Share[] | undefined;
@@ -0,0 +1,45 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var share_exports = {};
20
+ __export(share_exports, {
21
+ SHARE_PERMISSIONS: () => SHARE_PERMISSIONS,
22
+ createShares: () => createShares
23
+ });
24
+ module.exports = __toCommonJS(share_exports);
25
+ var import_bson = require("bson");
26
+ const SHARE_PERMISSIONS = ["read", "write", "manage"];
27
+ const createShares = (props) => {
28
+ if (!props?.length) return void 0;
29
+ const byUser = /* @__PURE__ */ new Map();
30
+ for (const { userId, permission } of props) {
31
+ if (!SHARE_PERMISSIONS.includes(permission)) throw new Error(`Share: Invalid permission: ${permission}`);
32
+ if (!userId) throw new Error("Share: userId is required");
33
+ const id = userId instanceof import_bson.ObjectId ? userId : new import_bson.ObjectId(userId);
34
+ const current = byUser.get(id.toString());
35
+ if (!current || SHARE_PERMISSIONS.indexOf(permission) > SHARE_PERMISSIONS.indexOf(current.permission)) {
36
+ byUser.set(id.toString(), { userId: id, permission });
37
+ }
38
+ }
39
+ return [...byUser.values()];
40
+ };
41
+ // Annotate the CommonJS export names for ESM import in node:
42
+ 0 && (module.exports = {
43
+ SHARE_PERMISSIONS,
44
+ createShares
45
+ });
@@ -0,0 +1,21 @@
1
+ import "../../chunk-CF3WOMB3.mjs";
2
+ import { ObjectId } from "bson";
3
+ const SHARE_PERMISSIONS = ["read", "write", "manage"];
4
+ const createShares = (props) => {
5
+ if (!props?.length) return void 0;
6
+ const byUser = /* @__PURE__ */ new Map();
7
+ for (const { userId, permission } of props) {
8
+ if (!SHARE_PERMISSIONS.includes(permission)) throw new Error(`Share: Invalid permission: ${permission}`);
9
+ if (!userId) throw new Error("Share: userId is required");
10
+ const id = userId instanceof ObjectId ? userId : new ObjectId(userId);
11
+ const current = byUser.get(id.toString());
12
+ if (!current || SHARE_PERMISSIONS.indexOf(permission) > SHARE_PERMISSIONS.indexOf(current.permission)) {
13
+ byUser.set(id.toString(), { userId: id, permission });
14
+ }
15
+ }
16
+ return [...byUser.values()];
17
+ };
18
+ export {
19
+ SHARE_PERMISSIONS,
20
+ createShares
21
+ };
@@ -1,3 +1 @@
1
1
  export * from './mask-in-logs';
2
- export * from './admin-only';
3
- export * from './owner-only';
@@ -16,11 +16,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
16
16
  var decorators_exports = {};
17
17
  module.exports = __toCommonJS(decorators_exports);
18
18
  __reExport(decorators_exports, require("./mask-in-logs"), module.exports);
19
- __reExport(decorators_exports, require("./admin-only"), module.exports);
20
- __reExport(decorators_exports, require("./owner-only"), module.exports);
21
19
  // Annotate the CommonJS export names for ESM import in node:
22
20
  0 && (module.exports = {
23
- ...require("./mask-in-logs"),
24
- ...require("./admin-only"),
25
- ...require("./owner-only")
21
+ ...require("./mask-in-logs")
26
22
  });
@@ -1,3 +1 @@
1
1
  export * from "./mask-in-logs";
2
- export * from "./admin-only";
3
- export * from "./owner-only";
@@ -1,2 +1,3 @@
1
1
  export * from './mongo-db.repository';
2
2
  export * from './mongo-db.client';
3
+ export * from './scope';
@@ -17,8 +17,10 @@ var repositories_exports = {};
17
17
  module.exports = __toCommonJS(repositories_exports);
18
18
  __reExport(repositories_exports, require("./mongo-db.repository"), module.exports);
19
19
  __reExport(repositories_exports, require("./mongo-db.client"), module.exports);
20
+ __reExport(repositories_exports, require("./scope"), module.exports);
20
21
  // Annotate the CommonJS export names for ESM import in node:
21
22
  0 && (module.exports = {
22
23
  ...require("./mongo-db.repository"),
23
- ...require("./mongo-db.client")
24
+ ...require("./mongo-db.client"),
25
+ ...require("./scope")
24
26
  });
@@ -1,2 +1,3 @@
1
1
  export * from "./mongo-db.repository";
2
2
  export * from "./mongo-db.client";
3
+ export * from "./scope";
@@ -1,4 +1,6 @@
1
1
  import { ObjectId } from 'mongodb';
2
+ import { Document } from '../base/base.entity.js';
3
+ import { Scope } from './scope';
2
4
  export declare class VersionConflictError extends Error {
3
5
  readonly code = "VERSION_CONFLICT";
4
6
  readonly statusCode = 409;
@@ -9,32 +11,48 @@ export declare class MongoRepository<T> {
9
11
  private readonly collection;
10
12
  private readonly dbClient;
11
13
  private readonly _where;
14
+ private readonly scoped;
15
+ private readonly ownerField;
16
+ private readonly protectedFields;
12
17
  private readonly report;
13
18
  constructor(props: {
14
19
  database: string;
15
20
  collection: string;
16
21
  envvar?: string;
22
+ scoped?: boolean;
23
+ ownerField?: string;
24
+ protectedFields?: string[];
17
25
  });
26
+ private assertScope;
27
+ private actorId;
28
+ private withScope;
29
+ private stampCreate;
30
+ private withoutProtected;
31
+ private withoutPath;
18
32
  parseId(id: string | undefined): ObjectId | undefined;
19
33
  cleanInput(value: string): string;
20
34
  private clampLimit;
21
- findOne({ where, select, options }: QueryParams<T>): Promise<T | null>;
35
+ findOne({ where, select, options, scope }: QueryParams<T>): Promise<T | null>;
22
36
  findMany(params: QueryParams<T>): Promise<T[]>;
23
37
  findManyPaginated(params: QueryParams<T>): Promise<{
24
38
  items: T[];
25
39
  hasMore: boolean;
26
40
  }>;
27
41
  private query;
28
- findAll({ where, select, options }: QueryParams<T>): Promise<T[]>;
29
- createOne({ payload }: CommandParams<T>): Promise<ObjectId>;
30
- createMany({ payload }: CommandParams<T>): Promise<CreateManyResponse>;
31
- updateOne({ where, payload, options, expectedVersion }: CommandParams<T>): Promise<T>;
32
- updateMany({ where, payload }: CommandParams<T>): Promise<number>;
33
- deleteOne({ where }: CommandParams<T>): Promise<number>;
34
- deleteMany({ where }: CommandParams<T>): Promise<number>;
35
- count({ where, options }: QueryParams<T>): Promise<number>;
36
- aggregate(pipeline: PipelineStage[]): Promise<T[]>;
37
- distinct(field: string, where?: Record<string, unknown>): Promise<T[]>;
42
+ findAll({ where, select, options, scope }: QueryParams<T>): Promise<T[]>;
43
+ createOne({ payload, scope }: CommandParams<T>): Promise<Document & {
44
+ _id: ObjectId;
45
+ }>;
46
+ createMany({ payload, scope }: CommandParams<T>): Promise<CreateManyResponse>;
47
+ updateOne({ where, payload, expectedVersion, scope }: CommandParams<T>): Promise<T>;
48
+ softDelete({ where, payload, scope }: CommandParams<T>): Promise<T | null>;
49
+ private modifyOne;
50
+ updateMany({ where, payload, scope }: CommandParams<T>): Promise<number>;
51
+ deleteOne({ where, scope }: CommandParams<T>): Promise<number>;
52
+ deleteMany({ where, scope }: CommandParams<T>): Promise<number>;
53
+ count({ where, options, scope }: QueryParams<T>): Promise<number>;
54
+ aggregate(pipeline: PipelineStage[], scope?: Scope): Promise<T[]>;
55
+ distinct(field: string, where?: Record<string, unknown>, scope?: Scope): Promise<T[]>;
38
56
  }
39
57
  export type PipelineStage = {
40
58
  $addFields: Record<string, unknown>;
@@ -73,6 +91,7 @@ type CreateManyResponse = {
73
91
  [key: number]: ObjectId;
74
92
  };
75
93
  export interface QueryParams<T> {
94
+ scope?: Scope;
76
95
  where?: Partial<T>;
77
96
  select?: {
78
97
  [K in keyof T]?: 1 | 0;
@@ -90,6 +109,7 @@ export interface QueryOptions {
90
109
  includeDeleted?: boolean;
91
110
  }
92
111
  export interface CommandParams<T> {
112
+ scope?: Scope;
93
113
  where?: Partial<T>;
94
114
  payload?: Partial<T> | Partial<T>[];
95
115
  options?: Record<string, unknown>;