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.
- package/CHANGELOG.md +27 -0
- package/README.md +2 -1
- package/dist/common/base/base.entity.d.ts +3 -9
- package/dist/common/base/base.entity.js +17 -96
- package/dist/common/base/base.entity.mjs +19 -92
- package/dist/common/base/base.use-case.d.ts +2 -0
- package/dist/common/base/index.d.ts +1 -0
- package/dist/common/base/index.js +3 -1
- package/dist/common/base/index.mjs +1 -0
- package/dist/common/base/share.d.ts +12 -0
- package/dist/common/base/share.js +45 -0
- package/dist/common/base/share.mjs +21 -0
- package/dist/common/decorators/index.d.ts +0 -2
- package/dist/common/decorators/index.js +1 -5
- package/dist/common/decorators/index.mjs +0 -2
- package/dist/common/repositories/index.d.ts +1 -0
- package/dist/common/repositories/index.js +3 -1
- package/dist/common/repositories/index.mjs +1 -0
- package/dist/common/repositories/mongo-db.repository.d.ts +31 -11
- package/dist/common/repositories/mongo-db.repository.js +121 -40
- package/dist/common/repositories/mongo-db.repository.mjs +121 -40
- package/dist/common/repositories/scope.d.ts +19 -0
- package/dist/common/repositories/scope.js +63 -0
- package/dist/common/repositories/scope.mjs +40 -0
- package/dist/services/authentication/repositories/revoked-token.repository.d.ts +3 -3
- package/dist/services/authentication/repositories/revoked-token.repository.js +7 -6
- package/dist/services/authentication/repositories/revoked-token.repository.mjs +7 -6
- package/dist/services/authentication/use-cases/is-token-revoked.use-case.js +2 -1
- package/dist/services/authentication/use-cases/is-token-revoked.use-case.mjs +2 -1
- package/dist/services/authentication/use-cases/login.use-case.js +2 -1
- package/dist/services/authentication/use-cases/login.use-case.mjs +2 -1
- package/dist/services/authentication/use-cases/revoke-token.use-case.js +5 -4
- package/dist/services/authentication/use-cases/revoke-token.use-case.mjs +5 -4
- package/dist/services/automations/repositories/automation-run.repository.d.ts +10 -10
- package/dist/services/automations/repositories/automation-run.repository.js +21 -18
- package/dist/services/automations/repositories/automation-run.repository.mjs +21 -18
- package/dist/services/automations/repositories/automation.repository.d.ts +14 -13
- package/dist/services/automations/repositories/automation.repository.js +30 -18
- package/dist/services/automations/repositories/automation.repository.mjs +30 -18
- package/dist/services/automations/runner/automation-runner.d.ts +3 -0
- package/dist/services/automations/runner/automation-runner.js +1 -1
- package/dist/services/automations/runner/automation-runner.mjs +1 -1
- package/dist/services/automations/use-cases/attach-automation-run-chat.use-case.js +3 -2
- package/dist/services/automations/use-cases/attach-automation-run-chat.use-case.mjs +3 -2
- package/dist/services/automations/use-cases/claim-due-automations.use-case.js +7 -6
- package/dist/services/automations/use-cases/claim-due-automations.use-case.mjs +7 -6
- package/dist/services/automations/use-cases/claim-queued-automation-runs.use-case.js +9 -8
- package/dist/services/automations/use-cases/claim-queued-automation-runs.use-case.mjs +9 -8
- package/dist/services/automations/use-cases/create-automation.use-case.js +4 -5
- package/dist/services/automations/use-cases/create-automation.use-case.mjs +2 -3
- package/dist/services/automations/use-cases/delete-automation.use-case.js +6 -9
- package/dist/services/automations/use-cases/delete-automation.use-case.mjs +4 -7
- package/dist/services/automations/use-cases/finish-automation-run.use-case.js +4 -3
- package/dist/services/automations/use-cases/finish-automation-run.use-case.mjs +4 -3
- package/dist/services/automations/use-cases/get-automation-by-id.use-case.js +5 -4
- package/dist/services/automations/use-cases/get-automation-by-id.use-case.mjs +3 -2
- package/dist/services/automations/use-cases/issue-automation-event-token.use-case.js +5 -5
- package/dist/services/automations/use-cases/issue-automation-event-token.use-case.mjs +3 -3
- package/dist/services/automations/use-cases/list-automation-runs-by-automation.use-case.js +3 -1
- package/dist/services/automations/use-cases/list-automation-runs-by-automation.use-case.mjs +3 -1
- package/dist/services/automations/use-cases/list-automation-runs-by-user.use-case.js +2 -0
- package/dist/services/automations/use-cases/list-automation-runs-by-user.use-case.mjs +2 -0
- package/dist/services/automations/use-cases/list-automations-by-user.use-case.js +2 -0
- package/dist/services/automations/use-cases/list-automations-by-user.use-case.mjs +2 -0
- package/dist/services/automations/use-cases/receive-automation-event.use-case.js +7 -6
- package/dist/services/automations/use-cases/receive-automation-event.use-case.mjs +7 -6
- package/dist/services/automations/use-cases/renew-automation-lease.use-case.js +2 -1
- package/dist/services/automations/use-cases/renew-automation-lease.use-case.mjs +2 -1
- package/dist/services/automations/use-cases/revoke-automation-event-token.use-case.js +5 -5
- package/dist/services/automations/use-cases/revoke-automation-event-token.use-case.mjs +3 -3
- package/dist/services/automations/use-cases/rotate-automation-event-tokens.use-case.js +8 -8
- package/dist/services/automations/use-cases/rotate-automation-event-tokens.use-case.mjs +6 -6
- package/dist/services/automations/use-cases/start-automation-run.use-case.js +8 -6
- package/dist/services/automations/use-cases/start-automation-run.use-case.mjs +8 -6
- package/dist/services/automations/use-cases/sync-automation-run.use-case.js +6 -5
- package/dist/services/automations/use-cases/sync-automation-run.use-case.mjs +6 -5
- package/dist/services/automations/use-cases/sync-automation-runs.use-case.js +3 -2
- package/dist/services/automations/use-cases/sync-automation-runs.use-case.mjs +3 -2
- package/dist/services/automations/use-cases/update-automation.use-case.js +8 -7
- package/dist/services/automations/use-cases/update-automation.use-case.mjs +6 -5
- package/dist/services/automations/utils/open-run.d.ts +2 -0
- package/dist/services/automations/utils/open-run.js +2 -2
- package/dist/services/automations/utils/open-run.mjs +2 -2
- package/dist/services/automations/utils/previous-run.js +3 -2
- package/dist/services/automations/utils/previous-run.mjs +3 -2
- package/dist/services/automations/utils/runner-scope.d.ts +3 -0
- package/dist/{common/decorators/admin-only/index.js → services/automations/utils/runner-scope.js} +15 -5
- package/dist/services/automations/utils/runner-scope.mjs +8 -0
- package/dist/services/chats/repositories/chat.repository.d.ts +7 -6
- package/dist/services/chats/repositories/chat.repository.js +16 -11
- package/dist/services/chats/repositories/chat.repository.mjs +16 -11
- package/dist/services/chats/use-cases/create-chat.use-case.js +4 -5
- package/dist/services/chats/use-cases/create-chat.use-case.mjs +2 -3
- package/dist/services/chats/use-cases/delete-chat.use-case.js +7 -9
- package/dist/services/chats/use-cases/delete-chat.use-case.mjs +5 -7
- package/dist/services/chats/use-cases/get-chat-by-id.use-case.js +3 -2
- package/dist/services/chats/use-cases/get-chat-by-id.use-case.mjs +3 -2
- package/dist/services/chats/use-cases/interrupt-execution.use-case.js +5 -7
- package/dist/services/chats/use-cases/interrupt-execution.use-case.mjs +3 -5
- package/dist/services/chats/use-cases/list-chats-by-props.use-case.js +3 -2
- package/dist/services/chats/use-cases/list-chats-by-props.use-case.mjs +3 -2
- package/dist/services/chats/use-cases/list-chats-by-user.use-case.js +2 -0
- package/dist/services/chats/use-cases/list-chats-by-user.use-case.mjs +2 -0
- package/dist/services/chats/use-cases/update-chat.use-case.js +5 -7
- package/dist/services/chats/use-cases/update-chat.use-case.mjs +3 -5
- package/dist/services/llmcalls/repositories/llmcall.repository.d.ts +7 -6
- package/dist/services/llmcalls/repositories/llmcall.repository.js +16 -11
- package/dist/services/llmcalls/repositories/llmcall.repository.mjs +16 -11
- package/dist/services/llmcalls/use-cases/create-llmcall.use-case.js +4 -5
- package/dist/services/llmcalls/use-cases/create-llmcall.use-case.mjs +2 -3
- package/dist/services/llmcalls/use-cases/delete-llmcall.use-case.js +6 -8
- package/dist/services/llmcalls/use-cases/delete-llmcall.use-case.mjs +4 -6
- package/dist/services/llmcalls/use-cases/get-llm-usage-by-chat.use-case.js +4 -3
- package/dist/services/llmcalls/use-cases/get-llm-usage-by-chat.use-case.mjs +2 -1
- package/dist/services/llmcalls/use-cases/get-llm-usage-by-message.use-case.js +4 -3
- package/dist/services/llmcalls/use-cases/get-llm-usage-by-message.use-case.mjs +2 -1
- package/dist/services/llmcalls/use-cases/get-llm-usage-stats.use-case.js +2 -1
- package/dist/services/llmcalls/use-cases/get-llm-usage-stats.use-case.mjs +2 -1
- package/dist/services/llmcalls/use-cases/get-llmcall-by-id.use-case.js +6 -4
- package/dist/services/llmcalls/use-cases/get-llmcall-by-id.use-case.mjs +4 -2
- package/dist/services/llmcalls/use-cases/list-llmcalls-by-props.use-case.js +3 -2
- package/dist/services/llmcalls/use-cases/list-llmcalls-by-props.use-case.mjs +3 -2
- package/dist/services/llmcalls/use-cases/update-llmcall.use-case.js +5 -7
- package/dist/services/llmcalls/use-cases/update-llmcall.use-case.mjs +3 -5
- package/dist/services/mcp-servers/repositories/mcpserver.repository.d.ts +8 -7
- package/dist/services/mcp-servers/repositories/mcpserver.repository.js +18 -12
- package/dist/services/mcp-servers/repositories/mcpserver.repository.mjs +18 -12
- package/dist/services/mcp-servers/use-cases/create-mcpserver.use-case.js +4 -5
- package/dist/services/mcp-servers/use-cases/create-mcpserver.use-case.mjs +2 -3
- package/dist/services/mcp-servers/use-cases/decide-mcpserver-trust.use-case.js +8 -9
- package/dist/services/mcp-servers/use-cases/decide-mcpserver-trust.use-case.mjs +6 -7
- package/dist/services/mcp-servers/use-cases/delete-mcpserver.use-case.js +6 -8
- package/dist/services/mcp-servers/use-cases/delete-mcpserver.use-case.mjs +4 -6
- package/dist/services/mcp-servers/use-cases/get-mcpserver-by-id.use-case.js +5 -4
- package/dist/services/mcp-servers/use-cases/get-mcpserver-by-id.use-case.mjs +3 -2
- package/dist/services/mcp-servers/use-cases/list-all-mcpservers-by-user.use-case.js +4 -3
- package/dist/services/mcp-servers/use-cases/list-all-mcpservers-by-user.use-case.mjs +2 -1
- package/dist/services/mcp-servers/use-cases/update-mcpserver.use-case.js +8 -11
- package/dist/services/mcp-servers/use-cases/update-mcpserver.use-case.mjs +6 -9
- package/dist/services/mcp-servers/use-cases/verify-mcpserver-connection.use-case.js +9 -9
- package/dist/services/mcp-servers/use-cases/verify-mcpserver-connection.use-case.mjs +7 -7
- package/dist/services/tasks/repositories/agent-action.repository.d.ts +4 -4
- package/dist/services/tasks/repositories/agent-action.repository.js +9 -7
- package/dist/services/tasks/repositories/agent-action.repository.mjs +9 -7
- package/dist/services/tasks/repositories/agent-task.repository.d.ts +5 -5
- package/dist/services/tasks/repositories/agent-task.repository.js +11 -9
- package/dist/services/tasks/repositories/agent-task.repository.mjs +11 -9
- package/dist/services/tasks/use-cases/create-agent-action.use-case.js +4 -5
- package/dist/services/tasks/use-cases/create-agent-action.use-case.mjs +2 -3
- package/dist/services/tasks/use-cases/create-agent-task.use-case.js +4 -5
- package/dist/services/tasks/use-cases/create-agent-task.use-case.mjs +2 -3
- package/dist/services/tasks/use-cases/get-agent-task-by-id.use-case.js +5 -4
- package/dist/services/tasks/use-cases/get-agent-task-by-id.use-case.mjs +3 -2
- package/dist/services/tasks/use-cases/list-agent-actions-by-task.use-case.js +3 -1
- package/dist/services/tasks/use-cases/list-agent-actions-by-task.use-case.mjs +3 -1
- package/dist/services/tasks/use-cases/list-agent-tasks-by-chat.use-case.js +3 -1
- package/dist/services/tasks/use-cases/list-agent-tasks-by-chat.use-case.mjs +3 -1
- package/dist/services/tasks/use-cases/update-agent-action.use-case.js +5 -7
- package/dist/services/tasks/use-cases/update-agent-action.use-case.mjs +3 -5
- package/dist/services/tasks/use-cases/update-agent-task.use-case.js +5 -7
- package/dist/services/tasks/use-cases/update-agent-task.use-case.mjs +3 -5
- package/dist/services/traces/repositories/agent-trace.repository.d.ts +3 -3
- package/dist/services/traces/repositories/agent-trace.repository.js +6 -5
- package/dist/services/traces/repositories/agent-trace.repository.mjs +6 -5
- package/dist/services/traces/use-cases/create-agent-traces.use-case.js +5 -9
- package/dist/services/traces/use-cases/create-agent-traces.use-case.mjs +3 -7
- package/dist/services/traces/use-cases/list-agent-traces-by-chat.use-case.js +3 -1
- package/dist/services/traces/use-cases/list-agent-traces-by-chat.use-case.mjs +3 -1
- package/dist/services/users/entities/user-acquisition.js +3 -16
- package/dist/services/users/entities/user-acquisition.mjs +4 -11
- package/dist/services/users/entities/user-marketing.js +3 -16
- package/dist/services/users/entities/user-marketing.mjs +4 -11
- package/dist/services/users/entities/user.entity.js +0 -3
- package/dist/services/users/entities/user.entity.mjs +1 -4
- package/dist/services/users/repositories/user.repository.d.ts +8 -7
- package/dist/services/users/repositories/user.repository.js +26 -12
- package/dist/services/users/repositories/user.repository.mjs +26 -12
- package/dist/services/users/use-cases/create-user.use-case.d.ts +0 -1
- package/dist/services/users/use-cases/create-user.use-case.js +5 -5
- package/dist/services/users/use-cases/create-user.use-case.mjs +3 -3
- package/dist/services/users/use-cases/delete-user.use-case.js +7 -9
- package/dist/services/users/use-cases/delete-user.use-case.mjs +5 -7
- package/dist/services/users/use-cases/get-user-by-id.use-case.js +6 -4
- package/dist/services/users/use-cases/get-user-by-id.use-case.mjs +4 -2
- package/dist/services/users/use-cases/list-users-by-props.use-case.js +3 -2
- package/dist/services/users/use-cases/list-users-by-props.use-case.mjs +3 -2
- package/dist/services/users/use-cases/update-user.use-case.js +5 -7
- package/dist/services/users/use-cases/update-user.use-case.mjs +3 -5
- package/dist/services/workspaces/repositories/workspace-item.repository.d.ts +8 -8
- package/dist/services/workspaces/repositories/workspace-item.repository.js +17 -14
- package/dist/services/workspaces/repositories/workspace-item.repository.mjs +17 -14
- package/dist/services/workspaces/use-cases/delete-workspace-item.use-case.js +6 -3
- package/dist/services/workspaces/use-cases/delete-workspace-item.use-case.mjs +4 -1
- package/dist/services/workspaces/use-cases/get-workspace-item.use-case.js +4 -3
- package/dist/services/workspaces/use-cases/get-workspace-item.use-case.mjs +2 -1
- package/dist/services/workspaces/use-cases/list-workspace-items.use-case.js +2 -0
- package/dist/services/workspaces/use-cases/list-workspace-items.use-case.mjs +2 -0
- package/dist/services/workspaces/use-cases/save-workspace-item.use-case.js +8 -8
- package/dist/services/workspaces/use-cases/save-workspace-item.use-case.mjs +6 -6
- package/dist/services/workspaces/use-cases/search-workspace-items.use-case.js +2 -0
- package/dist/services/workspaces/use-cases/search-workspace-items.use-case.mjs +2 -0
- package/docs/README.md +2 -1
- package/docs/authorization.md +77 -0
- package/docs/automations.md +4 -4
- package/docs/integration-guide.md +8 -10
- package/docs/llms.txt +1 -0
- package/docs/users.md +6 -4
- package/examples/automations/list-automation-runs.ts +2 -0
- package/examples/automations/run-automation-now.ts +3 -1
- package/examples/chats/delete-chat.ts +1 -1
- package/examples/users/create-user.ts +2 -1
- package/examples/users/list-users.ts +1 -0
- package/package.json +1 -1
- package/dist/common/decorators/admin-only/admin-only.d.ts +0 -2
- package/dist/common/decorators/admin-only/admin-only.js +0 -52
- package/dist/common/decorators/admin-only/admin-only.mjs +0 -28
- package/dist/common/decorators/admin-only/index.d.ts +0 -1
- package/dist/common/decorators/admin-only/index.mjs +0 -1
- package/dist/common/decorators/owner-only/index.d.ts +0 -1
- package/dist/common/decorators/owner-only/index.js +0 -22
- package/dist/common/decorators/owner-only/index.mjs +0 -1
- package/dist/common/decorators/owner-only/owner-only.d.ts +0 -2
- package/dist/common/decorators/owner-only/owner-only.js +0 -52
- package/dist/common/decorators/owner-only/owner-only.mjs +0 -28
|
@@ -26,6 +26,7 @@ var import_mongodb = require("mongodb");
|
|
|
26
26
|
var import_error = require("../error/index.js");
|
|
27
27
|
var import_mongo_db = require("./mongo-db.client");
|
|
28
28
|
const DEFAULT_LIMIT = 10;
|
|
29
|
+
const PROTECTED_FIELDS = ["__v", "ownerId", "sharedWith", "createdAt", "createdBy", "updatedAt", "updatedBy", "deletedAt", "deletedBy", "archivedAt", "archivedBy"];
|
|
29
30
|
class VersionConflictError extends Error {
|
|
30
31
|
code = "VERSION_CONFLICT";
|
|
31
32
|
statusCode = 409;
|
|
@@ -39,15 +40,65 @@ class MongoRepository {
|
|
|
39
40
|
collection;
|
|
40
41
|
dbClient;
|
|
41
42
|
_where = { deletedAt: null, archivedAt: null };
|
|
43
|
+
scoped;
|
|
44
|
+
ownerField;
|
|
45
|
+
protectedFields;
|
|
42
46
|
report = import_error.reportError;
|
|
43
47
|
constructor(props) {
|
|
44
|
-
const { database, collection, envvar } = props || {};
|
|
48
|
+
const { database, collection, envvar, scoped = false, ownerField = "ownerId", protectedFields = [] } = props || {};
|
|
45
49
|
if (!database) throw new Error("[Mongo Repository] Database name is required");
|
|
46
50
|
if (!collection) throw new Error("[Mongo Repository] Collection name is required");
|
|
47
51
|
this.dbClient = (0, import_mongo_db.getMongoDBClient)(envvar);
|
|
48
52
|
if (!this.dbClient) throw new Error("[Mongo Repository] Database client not found");
|
|
49
53
|
this.database = database;
|
|
50
54
|
this.collection = collection;
|
|
55
|
+
this.scoped = scoped;
|
|
56
|
+
this.ownerField = ownerField;
|
|
57
|
+
this.protectedFields = [...PROTECTED_FIELDS, ...protectedFields];
|
|
58
|
+
}
|
|
59
|
+
assertScope(scope) {
|
|
60
|
+
if (!scope && this.scoped) throw new Error(`[Mongo Repository] Scope is required in ${this.collection}`);
|
|
61
|
+
}
|
|
62
|
+
actorId(scope) {
|
|
63
|
+
if (!scope || scope.kind === "system") return void 0;
|
|
64
|
+
const userId = this.parseId(scope.userId);
|
|
65
|
+
if (!userId) throw new Error("[Mongo Repository] Invalid user id in scope");
|
|
66
|
+
return userId;
|
|
67
|
+
}
|
|
68
|
+
withScope(filter, scope) {
|
|
69
|
+
this.assertScope(scope);
|
|
70
|
+
if (!scope || scope.trusted) return filter;
|
|
71
|
+
return { $and: [filter, { [this.ownerField]: this.actorId(scope) }] };
|
|
72
|
+
}
|
|
73
|
+
stampCreate(payload, scope) {
|
|
74
|
+
this.assertScope(scope);
|
|
75
|
+
if (!scope) return { ...payload };
|
|
76
|
+
const actor = this.actorId(scope);
|
|
77
|
+
return {
|
|
78
|
+
...this.withoutProtected(payload, scope),
|
|
79
|
+
...scope.trusted || this.ownerField === "_id" ? {} : { [this.ownerField]: actor },
|
|
80
|
+
createdAt: /* @__PURE__ */ new Date(),
|
|
81
|
+
...actor ? { createdBy: actor } : {}
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
withoutProtected(payload, scope) {
|
|
85
|
+
if (!scope || scope.trusted) return payload;
|
|
86
|
+
const kept = Object.fromEntries(Object.entries(payload).filter(([key]) => !this.protectedFields.includes(key)));
|
|
87
|
+
return this.protectedFields.filter((path) => path.includes(".")).reduce((result, path) => this.withoutPath(result, path.split(".")), kept);
|
|
88
|
+
}
|
|
89
|
+
withoutPath(node, path) {
|
|
90
|
+
const [head, ...rest] = path;
|
|
91
|
+
if (!(head in node)) return node;
|
|
92
|
+
if (!rest.length) {
|
|
93
|
+
const { [head]: removed, ...others2 } = node;
|
|
94
|
+
return others2;
|
|
95
|
+
}
|
|
96
|
+
const child = node[head];
|
|
97
|
+
if (!child || typeof child !== "object" || Array.isArray(child) || child instanceof Date || child instanceof import_mongodb.ObjectId) return node;
|
|
98
|
+
const cleaned = this.withoutPath(child, rest);
|
|
99
|
+
if (Object.keys(cleaned).length) return { ...node, [head]: cleaned };
|
|
100
|
+
const { [head]: dropped, ...others } = node;
|
|
101
|
+
return others;
|
|
51
102
|
}
|
|
52
103
|
parseId(id) {
|
|
53
104
|
return typeof id === "string" && import_mongodb.ObjectId.isValid(id) ? new import_mongodb.ObjectId(id) : void 0;
|
|
@@ -59,12 +110,13 @@ class MongoRepository {
|
|
|
59
110
|
const n = Number(limit);
|
|
60
111
|
return !Number.isFinite(n) || n <= 0 ? DEFAULT_LIMIT : n;
|
|
61
112
|
}
|
|
62
|
-
async findOne({ where, select, options }) {
|
|
113
|
+
async findOne({ where, select, options, scope }) {
|
|
114
|
+
const filter = this.withScope({ ...where, ...this._where }, scope);
|
|
63
115
|
try {
|
|
64
116
|
const db = await this.dbClient.db(this.database);
|
|
65
117
|
const collection = db.collection(this.collection);
|
|
66
118
|
const result = await collection.findOne(
|
|
67
|
-
|
|
119
|
+
filter,
|
|
68
120
|
{
|
|
69
121
|
...select ? { projection: select } : {},
|
|
70
122
|
...options
|
|
@@ -85,24 +137,26 @@ class MongoRepository {
|
|
|
85
137
|
const results = await this.query(params, _limit, _limit + 1);
|
|
86
138
|
return { items: results.slice(0, _limit), hasMore: results.length > _limit };
|
|
87
139
|
}
|
|
88
|
-
async query({ where, select, options }, pageSize, fetchSize) {
|
|
140
|
+
async query({ where, select, options, scope }, pageSize, fetchSize) {
|
|
141
|
+
const filter = this.withScope({ ...where, ...this._where }, scope);
|
|
89
142
|
try {
|
|
90
143
|
const db = await this.dbClient.db(this.database);
|
|
91
144
|
const collection = db.collection(this.collection);
|
|
92
145
|
const { page = 1 } = options || {};
|
|
93
146
|
const _skip = +page > 0 ? (+page - 1) * pageSize : 0;
|
|
94
|
-
const result = await collection.find(
|
|
147
|
+
const result = await collection.find(filter, { projection: select }).sort(options?.sortBy || { _id: -1 }).skip(_skip).limit(fetchSize).toArray();
|
|
95
148
|
return result;
|
|
96
149
|
} catch (error) {
|
|
97
150
|
await this.report(error);
|
|
98
151
|
throw error;
|
|
99
152
|
}
|
|
100
153
|
}
|
|
101
|
-
async findAll({ where, select, options }) {
|
|
154
|
+
async findAll({ where, select, options, scope }) {
|
|
155
|
+
const filter = this.withScope({ ...where, ...this._where }, scope);
|
|
102
156
|
try {
|
|
103
157
|
const db = await this.dbClient.db(this.database);
|
|
104
158
|
const collection = db.collection(this.collection);
|
|
105
|
-
const cursor = collection.find(
|
|
159
|
+
const cursor = collection.find(filter, { projection: select });
|
|
106
160
|
const result = await (options?.sortBy ? cursor.sort(options.sortBy) : cursor).toArray();
|
|
107
161
|
return result;
|
|
108
162
|
} catch (error) {
|
|
@@ -110,53 +164,68 @@ class MongoRepository {
|
|
|
110
164
|
throw error;
|
|
111
165
|
}
|
|
112
166
|
}
|
|
113
|
-
async createOne({ payload }) {
|
|
167
|
+
async createOne({ payload, scope }) {
|
|
114
168
|
if (!payload || Object.keys(payload).length === 0) throw new Error("[Mongo Repository] Payload is required for createOne operation");
|
|
169
|
+
const document = this.stampCreate(payload, scope);
|
|
115
170
|
try {
|
|
116
171
|
const db = await this.dbClient.db(this.database);
|
|
117
172
|
const collection = db.collection(this.collection);
|
|
118
|
-
const { insertedId } = await collection.insertOne({ ...
|
|
119
|
-
return insertedId;
|
|
173
|
+
const { insertedId } = await collection.insertOne({ ...document, __v: 1 });
|
|
174
|
+
return { ...document, _id: insertedId, __v: 1 };
|
|
120
175
|
} catch (error) {
|
|
121
176
|
await this.report(error);
|
|
122
177
|
throw error;
|
|
123
178
|
}
|
|
124
179
|
}
|
|
125
|
-
async createMany({ payload }) {
|
|
180
|
+
async createMany({ payload, scope }) {
|
|
181
|
+
const documents = payload.map((item) => this.stampCreate(item, scope));
|
|
126
182
|
try {
|
|
127
183
|
const db = await this.dbClient.db(this.database);
|
|
128
184
|
const collection = db.collection(this.collection);
|
|
129
|
-
const { insertedIds } = await collection.insertMany(
|
|
185
|
+
const { insertedIds } = await collection.insertMany(documents);
|
|
130
186
|
return insertedIds;
|
|
131
187
|
} catch (error) {
|
|
132
188
|
await this.report(error);
|
|
133
189
|
throw error;
|
|
134
190
|
}
|
|
135
191
|
}
|
|
136
|
-
async updateOne({ where, payload,
|
|
192
|
+
async updateOne({ where, payload, expectedVersion, scope }) {
|
|
137
193
|
if (!where || Object.keys(where).length === 0) throw new Error("[Mongo Repository] Where is required for updateOne operation");
|
|
138
194
|
if (!payload || Object.keys(payload).length === 0) throw new Error("[Mongo Repository] Payload is required for updateOne operation");
|
|
195
|
+
const actor = this.actorId(scope);
|
|
196
|
+
const { _id, __v, createdAt, createdBy, toUnset, ...rest } = payload;
|
|
197
|
+
const toSet = {
|
|
198
|
+
...this.withoutProtected(rest, scope),
|
|
199
|
+
...scope ? { updatedAt: /* @__PURE__ */ new Date(), ...actor ? { updatedBy: actor } : {} } : {}
|
|
200
|
+
};
|
|
201
|
+
return this.modifyOne(where, {
|
|
202
|
+
...toSet ? { $set: toSet } : {},
|
|
203
|
+
...toUnset ? { $unset: toUnset } : {}
|
|
204
|
+
}, scope, expectedVersion);
|
|
205
|
+
}
|
|
206
|
+
async softDelete({ where, payload, scope }) {
|
|
207
|
+
if (!where || Object.keys(where).length === 0) throw new Error("[Mongo Repository] Where is required for softDelete operation");
|
|
208
|
+
const actor = this.actorId(scope);
|
|
209
|
+
const { toUnset, ...rest } = payload ?? {};
|
|
210
|
+
const extra = this.withoutProtected(rest, scope);
|
|
211
|
+
return this.modifyOne(where, {
|
|
212
|
+
$set: { ...extra, deletedAt: /* @__PURE__ */ new Date(), ...actor ? { deletedBy: actor } : {} },
|
|
213
|
+
...toUnset ? { $unset: toUnset } : {}
|
|
214
|
+
}, scope);
|
|
215
|
+
}
|
|
216
|
+
async modifyOne(where, update, scope, expectedVersion) {
|
|
139
217
|
const checkVersion = typeof expectedVersion === "number";
|
|
218
|
+
const filter = this.withScope({ ...where, ...checkVersion ? { __v: expectedVersion } : {}, ...this._where }, scope);
|
|
140
219
|
try {
|
|
141
220
|
const db = await this.dbClient.db(this.database);
|
|
142
221
|
const collection = db.collection(this.collection);
|
|
143
|
-
const { _id, __v, createdAt, createdBy, toUnset, ...toSet } = payload;
|
|
144
|
-
const __payload = {
|
|
145
|
-
...toSet ? { $set: toSet } : {},
|
|
146
|
-
...toUnset ? { $unset: toUnset } : {}
|
|
147
|
-
};
|
|
148
222
|
const result = await collection.findOneAndUpdate(
|
|
149
|
-
|
|
150
|
-
{
|
|
151
|
-
|
|
152
|
-
...__payload
|
|
153
|
-
},
|
|
154
|
-
{
|
|
155
|
-
returnDocument: "after"
|
|
156
|
-
}
|
|
223
|
+
filter,
|
|
224
|
+
{ $inc: { __v: 1 }, ...update },
|
|
225
|
+
{ returnDocument: "after" }
|
|
157
226
|
);
|
|
158
227
|
if (!result && checkVersion) {
|
|
159
|
-
const exists = await collection.findOne({ ...where, ...this._where }, { projection: { _id: 1 } });
|
|
228
|
+
const exists = await collection.findOne(this.withScope({ ...where, ...this._where }, scope), { projection: { _id: 1 } });
|
|
160
229
|
if (exists) throw new VersionConflictError();
|
|
161
230
|
}
|
|
162
231
|
return result;
|
|
@@ -165,13 +234,19 @@ class MongoRepository {
|
|
|
165
234
|
throw error;
|
|
166
235
|
}
|
|
167
236
|
}
|
|
168
|
-
async updateMany({ where, payload }) {
|
|
237
|
+
async updateMany({ where, payload, scope }) {
|
|
238
|
+
const filter = this.withScope({ ...where, ...this._where }, scope);
|
|
239
|
+
const actor = this.actorId(scope);
|
|
240
|
+
const toSet = {
|
|
241
|
+
...this.withoutProtected(payload ?? {}, scope),
|
|
242
|
+
...scope ? { updatedAt: /* @__PURE__ */ new Date(), ...actor ? { updatedBy: actor } : {} } : {}
|
|
243
|
+
};
|
|
169
244
|
try {
|
|
170
245
|
const db = await this.dbClient.db(this.database);
|
|
171
246
|
const collection = db.collection(this.collection);
|
|
172
247
|
const result = await collection.updateMany(
|
|
173
|
-
|
|
174
|
-
{ $set:
|
|
248
|
+
filter,
|
|
249
|
+
{ $set: toSet }
|
|
175
250
|
);
|
|
176
251
|
const countUpdated = result.modifiedCount;
|
|
177
252
|
return countUpdated;
|
|
@@ -180,55 +255,61 @@ class MongoRepository {
|
|
|
180
255
|
throw error;
|
|
181
256
|
}
|
|
182
257
|
}
|
|
183
|
-
async deleteOne({ where }) {
|
|
258
|
+
async deleteOne({ where, scope }) {
|
|
259
|
+
const filter = this.withScope({ ...where }, scope);
|
|
184
260
|
try {
|
|
185
261
|
const db = await this.dbClient.db(this.database);
|
|
186
262
|
const collection = db.collection(this.collection);
|
|
187
|
-
const { deletedCount } = await collection.deleteOne(
|
|
263
|
+
const { deletedCount } = await collection.deleteOne(filter);
|
|
188
264
|
return deletedCount;
|
|
189
265
|
} catch (error) {
|
|
190
266
|
await this.report(error);
|
|
191
267
|
throw error;
|
|
192
268
|
}
|
|
193
269
|
}
|
|
194
|
-
async deleteMany({ where }) {
|
|
270
|
+
async deleteMany({ where, scope }) {
|
|
271
|
+
const filter = this.withScope({ ...where }, scope);
|
|
195
272
|
try {
|
|
196
273
|
const db = await this.dbClient.db(this.database);
|
|
197
274
|
const collection = db.collection(this.collection);
|
|
198
|
-
const { deletedCount } = await collection.deleteMany(
|
|
275
|
+
const { deletedCount } = await collection.deleteMany(filter);
|
|
199
276
|
return deletedCount;
|
|
200
277
|
} catch (error) {
|
|
201
278
|
await this.report(error);
|
|
202
279
|
throw error;
|
|
203
280
|
}
|
|
204
281
|
}
|
|
205
|
-
async count({ where, options }) {
|
|
282
|
+
async count({ where, options, scope }) {
|
|
283
|
+
const filter = this.withScope({ ...where, ...options?.includeDeleted ? {} : this._where }, scope);
|
|
206
284
|
try {
|
|
207
285
|
const db = await this.dbClient.db(this.database);
|
|
208
286
|
const collection = db.collection(this.collection);
|
|
209
|
-
const result = await collection.countDocuments(
|
|
287
|
+
const result = await collection.countDocuments(filter);
|
|
210
288
|
return result;
|
|
211
289
|
} catch (error) {
|
|
212
290
|
await this.report(error);
|
|
213
291
|
throw error;
|
|
214
292
|
}
|
|
215
293
|
}
|
|
216
|
-
async aggregate(pipeline) {
|
|
294
|
+
async aggregate(pipeline, scope) {
|
|
295
|
+
const scopeFilter = this.withScope({}, scope);
|
|
296
|
+
const scopedPipeline = Object.keys(scopeFilter).length ? [{ $match: scopeFilter }, ...pipeline] : pipeline;
|
|
217
297
|
try {
|
|
218
298
|
const db = await this.dbClient.db(this.database);
|
|
219
299
|
const collection = db.collection(this.collection);
|
|
220
|
-
const result = await collection.aggregate(
|
|
300
|
+
const result = await collection.aggregate(scopedPipeline).toArray();
|
|
221
301
|
return result;
|
|
222
302
|
} catch (error) {
|
|
223
303
|
await this.report(error);
|
|
224
304
|
throw error;
|
|
225
305
|
}
|
|
226
306
|
}
|
|
227
|
-
async distinct(field, where) {
|
|
307
|
+
async distinct(field, where, scope) {
|
|
308
|
+
const filter = this.withScope({ ...where, ...this._where }, scope);
|
|
228
309
|
try {
|
|
229
310
|
const db = await this.dbClient.db(this.database);
|
|
230
311
|
const collection = db.collection(this.collection);
|
|
231
|
-
const result = await collection.distinct(field,
|
|
312
|
+
const result = await collection.distinct(field, filter);
|
|
232
313
|
return result;
|
|
233
314
|
} catch (error) {
|
|
234
315
|
await this.report(error);
|
|
@@ -3,6 +3,7 @@ import { ObjectId } from "mongodb";
|
|
|
3
3
|
import { reportError } from "../error/index.js";
|
|
4
4
|
import { getMongoDBClient } from "./mongo-db.client";
|
|
5
5
|
const DEFAULT_LIMIT = 10;
|
|
6
|
+
const PROTECTED_FIELDS = ["__v", "ownerId", "sharedWith", "createdAt", "createdBy", "updatedAt", "updatedBy", "deletedAt", "deletedBy", "archivedAt", "archivedBy"];
|
|
6
7
|
class VersionConflictError extends Error {
|
|
7
8
|
code = "VERSION_CONFLICT";
|
|
8
9
|
statusCode = 409;
|
|
@@ -16,15 +17,65 @@ class MongoRepository {
|
|
|
16
17
|
collection;
|
|
17
18
|
dbClient;
|
|
18
19
|
_where = { deletedAt: null, archivedAt: null };
|
|
20
|
+
scoped;
|
|
21
|
+
ownerField;
|
|
22
|
+
protectedFields;
|
|
19
23
|
report = reportError;
|
|
20
24
|
constructor(props) {
|
|
21
|
-
const { database, collection, envvar } = props || {};
|
|
25
|
+
const { database, collection, envvar, scoped = false, ownerField = "ownerId", protectedFields = [] } = props || {};
|
|
22
26
|
if (!database) throw new Error("[Mongo Repository] Database name is required");
|
|
23
27
|
if (!collection) throw new Error("[Mongo Repository] Collection name is required");
|
|
24
28
|
this.dbClient = getMongoDBClient(envvar);
|
|
25
29
|
if (!this.dbClient) throw new Error("[Mongo Repository] Database client not found");
|
|
26
30
|
this.database = database;
|
|
27
31
|
this.collection = collection;
|
|
32
|
+
this.scoped = scoped;
|
|
33
|
+
this.ownerField = ownerField;
|
|
34
|
+
this.protectedFields = [...PROTECTED_FIELDS, ...protectedFields];
|
|
35
|
+
}
|
|
36
|
+
assertScope(scope) {
|
|
37
|
+
if (!scope && this.scoped) throw new Error(`[Mongo Repository] Scope is required in ${this.collection}`);
|
|
38
|
+
}
|
|
39
|
+
actorId(scope) {
|
|
40
|
+
if (!scope || scope.kind === "system") return void 0;
|
|
41
|
+
const userId = this.parseId(scope.userId);
|
|
42
|
+
if (!userId) throw new Error("[Mongo Repository] Invalid user id in scope");
|
|
43
|
+
return userId;
|
|
44
|
+
}
|
|
45
|
+
withScope(filter, scope) {
|
|
46
|
+
this.assertScope(scope);
|
|
47
|
+
if (!scope || scope.trusted) return filter;
|
|
48
|
+
return { $and: [filter, { [this.ownerField]: this.actorId(scope) }] };
|
|
49
|
+
}
|
|
50
|
+
stampCreate(payload, scope) {
|
|
51
|
+
this.assertScope(scope);
|
|
52
|
+
if (!scope) return { ...payload };
|
|
53
|
+
const actor = this.actorId(scope);
|
|
54
|
+
return {
|
|
55
|
+
...this.withoutProtected(payload, scope),
|
|
56
|
+
...scope.trusted || this.ownerField === "_id" ? {} : { [this.ownerField]: actor },
|
|
57
|
+
createdAt: /* @__PURE__ */ new Date(),
|
|
58
|
+
...actor ? { createdBy: actor } : {}
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
withoutProtected(payload, scope) {
|
|
62
|
+
if (!scope || scope.trusted) return payload;
|
|
63
|
+
const kept = Object.fromEntries(Object.entries(payload).filter(([key]) => !this.protectedFields.includes(key)));
|
|
64
|
+
return this.protectedFields.filter((path) => path.includes(".")).reduce((result, path) => this.withoutPath(result, path.split(".")), kept);
|
|
65
|
+
}
|
|
66
|
+
withoutPath(node, path) {
|
|
67
|
+
const [head, ...rest] = path;
|
|
68
|
+
if (!(head in node)) return node;
|
|
69
|
+
if (!rest.length) {
|
|
70
|
+
const { [head]: removed, ...others2 } = node;
|
|
71
|
+
return others2;
|
|
72
|
+
}
|
|
73
|
+
const child = node[head];
|
|
74
|
+
if (!child || typeof child !== "object" || Array.isArray(child) || child instanceof Date || child instanceof ObjectId) return node;
|
|
75
|
+
const cleaned = this.withoutPath(child, rest);
|
|
76
|
+
if (Object.keys(cleaned).length) return { ...node, [head]: cleaned };
|
|
77
|
+
const { [head]: dropped, ...others } = node;
|
|
78
|
+
return others;
|
|
28
79
|
}
|
|
29
80
|
parseId(id) {
|
|
30
81
|
return typeof id === "string" && ObjectId.isValid(id) ? new ObjectId(id) : void 0;
|
|
@@ -36,12 +87,13 @@ class MongoRepository {
|
|
|
36
87
|
const n = Number(limit);
|
|
37
88
|
return !Number.isFinite(n) || n <= 0 ? DEFAULT_LIMIT : n;
|
|
38
89
|
}
|
|
39
|
-
async findOne({ where, select, options }) {
|
|
90
|
+
async findOne({ where, select, options, scope }) {
|
|
91
|
+
const filter = this.withScope({ ...where, ...this._where }, scope);
|
|
40
92
|
try {
|
|
41
93
|
const db = await this.dbClient.db(this.database);
|
|
42
94
|
const collection = db.collection(this.collection);
|
|
43
95
|
const result = await collection.findOne(
|
|
44
|
-
|
|
96
|
+
filter,
|
|
45
97
|
{
|
|
46
98
|
...select ? { projection: select } : {},
|
|
47
99
|
...options
|
|
@@ -62,24 +114,26 @@ class MongoRepository {
|
|
|
62
114
|
const results = await this.query(params, _limit, _limit + 1);
|
|
63
115
|
return { items: results.slice(0, _limit), hasMore: results.length > _limit };
|
|
64
116
|
}
|
|
65
|
-
async query({ where, select, options }, pageSize, fetchSize) {
|
|
117
|
+
async query({ where, select, options, scope }, pageSize, fetchSize) {
|
|
118
|
+
const filter = this.withScope({ ...where, ...this._where }, scope);
|
|
66
119
|
try {
|
|
67
120
|
const db = await this.dbClient.db(this.database);
|
|
68
121
|
const collection = db.collection(this.collection);
|
|
69
122
|
const { page = 1 } = options || {};
|
|
70
123
|
const _skip = +page > 0 ? (+page - 1) * pageSize : 0;
|
|
71
|
-
const result = await collection.find(
|
|
124
|
+
const result = await collection.find(filter, { projection: select }).sort(options?.sortBy || { _id: -1 }).skip(_skip).limit(fetchSize).toArray();
|
|
72
125
|
return result;
|
|
73
126
|
} catch (error) {
|
|
74
127
|
await this.report(error);
|
|
75
128
|
throw error;
|
|
76
129
|
}
|
|
77
130
|
}
|
|
78
|
-
async findAll({ where, select, options }) {
|
|
131
|
+
async findAll({ where, select, options, scope }) {
|
|
132
|
+
const filter = this.withScope({ ...where, ...this._where }, scope);
|
|
79
133
|
try {
|
|
80
134
|
const db = await this.dbClient.db(this.database);
|
|
81
135
|
const collection = db.collection(this.collection);
|
|
82
|
-
const cursor = collection.find(
|
|
136
|
+
const cursor = collection.find(filter, { projection: select });
|
|
83
137
|
const result = await (options?.sortBy ? cursor.sort(options.sortBy) : cursor).toArray();
|
|
84
138
|
return result;
|
|
85
139
|
} catch (error) {
|
|
@@ -87,53 +141,68 @@ class MongoRepository {
|
|
|
87
141
|
throw error;
|
|
88
142
|
}
|
|
89
143
|
}
|
|
90
|
-
async createOne({ payload }) {
|
|
144
|
+
async createOne({ payload, scope }) {
|
|
91
145
|
if (!payload || Object.keys(payload).length === 0) throw new Error("[Mongo Repository] Payload is required for createOne operation");
|
|
146
|
+
const document = this.stampCreate(payload, scope);
|
|
92
147
|
try {
|
|
93
148
|
const db = await this.dbClient.db(this.database);
|
|
94
149
|
const collection = db.collection(this.collection);
|
|
95
|
-
const { insertedId } = await collection.insertOne({ ...
|
|
96
|
-
return insertedId;
|
|
150
|
+
const { insertedId } = await collection.insertOne({ ...document, __v: 1 });
|
|
151
|
+
return { ...document, _id: insertedId, __v: 1 };
|
|
97
152
|
} catch (error) {
|
|
98
153
|
await this.report(error);
|
|
99
154
|
throw error;
|
|
100
155
|
}
|
|
101
156
|
}
|
|
102
|
-
async createMany({ payload }) {
|
|
157
|
+
async createMany({ payload, scope }) {
|
|
158
|
+
const documents = payload.map((item) => this.stampCreate(item, scope));
|
|
103
159
|
try {
|
|
104
160
|
const db = await this.dbClient.db(this.database);
|
|
105
161
|
const collection = db.collection(this.collection);
|
|
106
|
-
const { insertedIds } = await collection.insertMany(
|
|
162
|
+
const { insertedIds } = await collection.insertMany(documents);
|
|
107
163
|
return insertedIds;
|
|
108
164
|
} catch (error) {
|
|
109
165
|
await this.report(error);
|
|
110
166
|
throw error;
|
|
111
167
|
}
|
|
112
168
|
}
|
|
113
|
-
async updateOne({ where, payload,
|
|
169
|
+
async updateOne({ where, payload, expectedVersion, scope }) {
|
|
114
170
|
if (!where || Object.keys(where).length === 0) throw new Error("[Mongo Repository] Where is required for updateOne operation");
|
|
115
171
|
if (!payload || Object.keys(payload).length === 0) throw new Error("[Mongo Repository] Payload is required for updateOne operation");
|
|
172
|
+
const actor = this.actorId(scope);
|
|
173
|
+
const { _id, __v, createdAt, createdBy, toUnset, ...rest } = payload;
|
|
174
|
+
const toSet = {
|
|
175
|
+
...this.withoutProtected(rest, scope),
|
|
176
|
+
...scope ? { updatedAt: /* @__PURE__ */ new Date(), ...actor ? { updatedBy: actor } : {} } : {}
|
|
177
|
+
};
|
|
178
|
+
return this.modifyOne(where, {
|
|
179
|
+
...toSet ? { $set: toSet } : {},
|
|
180
|
+
...toUnset ? { $unset: toUnset } : {}
|
|
181
|
+
}, scope, expectedVersion);
|
|
182
|
+
}
|
|
183
|
+
async softDelete({ where, payload, scope }) {
|
|
184
|
+
if (!where || Object.keys(where).length === 0) throw new Error("[Mongo Repository] Where is required for softDelete operation");
|
|
185
|
+
const actor = this.actorId(scope);
|
|
186
|
+
const { toUnset, ...rest } = payload ?? {};
|
|
187
|
+
const extra = this.withoutProtected(rest, scope);
|
|
188
|
+
return this.modifyOne(where, {
|
|
189
|
+
$set: { ...extra, deletedAt: /* @__PURE__ */ new Date(), ...actor ? { deletedBy: actor } : {} },
|
|
190
|
+
...toUnset ? { $unset: toUnset } : {}
|
|
191
|
+
}, scope);
|
|
192
|
+
}
|
|
193
|
+
async modifyOne(where, update, scope, expectedVersion) {
|
|
116
194
|
const checkVersion = typeof expectedVersion === "number";
|
|
195
|
+
const filter = this.withScope({ ...where, ...checkVersion ? { __v: expectedVersion } : {}, ...this._where }, scope);
|
|
117
196
|
try {
|
|
118
197
|
const db = await this.dbClient.db(this.database);
|
|
119
198
|
const collection = db.collection(this.collection);
|
|
120
|
-
const { _id, __v, createdAt, createdBy, toUnset, ...toSet } = payload;
|
|
121
|
-
const __payload = {
|
|
122
|
-
...toSet ? { $set: toSet } : {},
|
|
123
|
-
...toUnset ? { $unset: toUnset } : {}
|
|
124
|
-
};
|
|
125
199
|
const result = await collection.findOneAndUpdate(
|
|
126
|
-
|
|
127
|
-
{
|
|
128
|
-
|
|
129
|
-
...__payload
|
|
130
|
-
},
|
|
131
|
-
{
|
|
132
|
-
returnDocument: "after"
|
|
133
|
-
}
|
|
200
|
+
filter,
|
|
201
|
+
{ $inc: { __v: 1 }, ...update },
|
|
202
|
+
{ returnDocument: "after" }
|
|
134
203
|
);
|
|
135
204
|
if (!result && checkVersion) {
|
|
136
|
-
const exists = await collection.findOne({ ...where, ...this._where }, { projection: { _id: 1 } });
|
|
205
|
+
const exists = await collection.findOne(this.withScope({ ...where, ...this._where }, scope), { projection: { _id: 1 } });
|
|
137
206
|
if (exists) throw new VersionConflictError();
|
|
138
207
|
}
|
|
139
208
|
return result;
|
|
@@ -142,13 +211,19 @@ class MongoRepository {
|
|
|
142
211
|
throw error;
|
|
143
212
|
}
|
|
144
213
|
}
|
|
145
|
-
async updateMany({ where, payload }) {
|
|
214
|
+
async updateMany({ where, payload, scope }) {
|
|
215
|
+
const filter = this.withScope({ ...where, ...this._where }, scope);
|
|
216
|
+
const actor = this.actorId(scope);
|
|
217
|
+
const toSet = {
|
|
218
|
+
...this.withoutProtected(payload ?? {}, scope),
|
|
219
|
+
...scope ? { updatedAt: /* @__PURE__ */ new Date(), ...actor ? { updatedBy: actor } : {} } : {}
|
|
220
|
+
};
|
|
146
221
|
try {
|
|
147
222
|
const db = await this.dbClient.db(this.database);
|
|
148
223
|
const collection = db.collection(this.collection);
|
|
149
224
|
const result = await collection.updateMany(
|
|
150
|
-
|
|
151
|
-
{ $set:
|
|
225
|
+
filter,
|
|
226
|
+
{ $set: toSet }
|
|
152
227
|
);
|
|
153
228
|
const countUpdated = result.modifiedCount;
|
|
154
229
|
return countUpdated;
|
|
@@ -157,55 +232,61 @@ class MongoRepository {
|
|
|
157
232
|
throw error;
|
|
158
233
|
}
|
|
159
234
|
}
|
|
160
|
-
async deleteOne({ where }) {
|
|
235
|
+
async deleteOne({ where, scope }) {
|
|
236
|
+
const filter = this.withScope({ ...where }, scope);
|
|
161
237
|
try {
|
|
162
238
|
const db = await this.dbClient.db(this.database);
|
|
163
239
|
const collection = db.collection(this.collection);
|
|
164
|
-
const { deletedCount } = await collection.deleteOne(
|
|
240
|
+
const { deletedCount } = await collection.deleteOne(filter);
|
|
165
241
|
return deletedCount;
|
|
166
242
|
} catch (error) {
|
|
167
243
|
await this.report(error);
|
|
168
244
|
throw error;
|
|
169
245
|
}
|
|
170
246
|
}
|
|
171
|
-
async deleteMany({ where }) {
|
|
247
|
+
async deleteMany({ where, scope }) {
|
|
248
|
+
const filter = this.withScope({ ...where }, scope);
|
|
172
249
|
try {
|
|
173
250
|
const db = await this.dbClient.db(this.database);
|
|
174
251
|
const collection = db.collection(this.collection);
|
|
175
|
-
const { deletedCount } = await collection.deleteMany(
|
|
252
|
+
const { deletedCount } = await collection.deleteMany(filter);
|
|
176
253
|
return deletedCount;
|
|
177
254
|
} catch (error) {
|
|
178
255
|
await this.report(error);
|
|
179
256
|
throw error;
|
|
180
257
|
}
|
|
181
258
|
}
|
|
182
|
-
async count({ where, options }) {
|
|
259
|
+
async count({ where, options, scope }) {
|
|
260
|
+
const filter = this.withScope({ ...where, ...options?.includeDeleted ? {} : this._where }, scope);
|
|
183
261
|
try {
|
|
184
262
|
const db = await this.dbClient.db(this.database);
|
|
185
263
|
const collection = db.collection(this.collection);
|
|
186
|
-
const result = await collection.countDocuments(
|
|
264
|
+
const result = await collection.countDocuments(filter);
|
|
187
265
|
return result;
|
|
188
266
|
} catch (error) {
|
|
189
267
|
await this.report(error);
|
|
190
268
|
throw error;
|
|
191
269
|
}
|
|
192
270
|
}
|
|
193
|
-
async aggregate(pipeline) {
|
|
271
|
+
async aggregate(pipeline, scope) {
|
|
272
|
+
const scopeFilter = this.withScope({}, scope);
|
|
273
|
+
const scopedPipeline = Object.keys(scopeFilter).length ? [{ $match: scopeFilter }, ...pipeline] : pipeline;
|
|
194
274
|
try {
|
|
195
275
|
const db = await this.dbClient.db(this.database);
|
|
196
276
|
const collection = db.collection(this.collection);
|
|
197
|
-
const result = await collection.aggregate(
|
|
277
|
+
const result = await collection.aggregate(scopedPipeline).toArray();
|
|
198
278
|
return result;
|
|
199
279
|
} catch (error) {
|
|
200
280
|
await this.report(error);
|
|
201
281
|
throw error;
|
|
202
282
|
}
|
|
203
283
|
}
|
|
204
|
-
async distinct(field, where) {
|
|
284
|
+
async distinct(field, where, scope) {
|
|
285
|
+
const filter = this.withScope({ ...where, ...this._where }, scope);
|
|
205
286
|
try {
|
|
206
287
|
const db = await this.dbClient.db(this.database);
|
|
207
288
|
const collection = db.collection(this.collection);
|
|
208
|
-
const result = await collection.distinct(field,
|
|
289
|
+
const result = await collection.distinct(field, filter);
|
|
209
290
|
return result;
|
|
210
291
|
} catch (error) {
|
|
211
292
|
await this.report(error);
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export interface ScopeUser {
|
|
2
|
+
userId?: string;
|
|
3
|
+
isAdmin(): boolean;
|
|
4
|
+
}
|
|
5
|
+
type ScopeKind = 'view' | 'edit' | 'manage' | 'admin' | 'system';
|
|
6
|
+
export declare class Scope {
|
|
7
|
+
readonly kind: ScopeKind;
|
|
8
|
+
readonly userId?: string | undefined;
|
|
9
|
+
readonly reason?: string | undefined;
|
|
10
|
+
private constructor();
|
|
11
|
+
get trusted(): boolean;
|
|
12
|
+
static canView(user?: ScopeUser): Scope;
|
|
13
|
+
static canEdit(user?: ScopeUser): Scope;
|
|
14
|
+
static canManage(user?: ScopeUser): Scope;
|
|
15
|
+
static asAdmin(user?: ScopeUser): Scope;
|
|
16
|
+
static system(reason: string): Scope;
|
|
17
|
+
private static requireUserId;
|
|
18
|
+
}
|
|
19
|
+
export {};
|