openloops 0.4.5 → 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 +52 -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/db/indexes/automations.js +1 -1
- package/dist/db/indexes/automations.mjs +1 -1
- package/dist/db/migrations/automations.d.ts +1 -0
- package/dist/db/migrations/automations.js +32 -5
- package/dist/db/migrations/automations.mjs +32 -5
- 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/entities/automation.entity.d.ts +9 -9
- package/dist/services/automations/entities/automation.entity.js +21 -20
- package/dist/services/automations/entities/automation.entity.mjs +21 -20
- package/dist/services/automations/entities/trigger.d.ts +16 -8
- package/dist/services/automations/entities/trigger.js +28 -17
- package/dist/services/automations/entities/trigger.mjs +27 -16
- 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 +34 -22
- package/dist/services/automations/repositories/automation.repository.mjs +34 -22
- package/dist/services/automations/runner/automation-runner.d.ts +3 -0
- package/dist/services/automations/runner/automation-runner.js +2 -2
- package/dist/services/automations/runner/automation-runner.mjs +2 -2
- 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 +11 -10
- package/dist/services/automations/use-cases/claim-due-automations.use-case.mjs +11 -10
- package/dist/services/automations/use-cases/claim-queued-automation-runs.use-case.js +10 -9
- package/dist/services/automations/use-cases/claim-queued-automation-runs.use-case.mjs +10 -9
- package/dist/services/automations/use-cases/create-automation.use-case.d.ts +2 -2
- package/dist/services/automations/use-cases/create-automation.use-case.js +8 -8
- package/dist/services/automations/use-cases/create-automation.use-case.mjs +6 -6
- 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 +7 -7
- package/dist/services/automations/use-cases/issue-automation-event-token.use-case.mjs +5 -5
- 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 +10 -8
- package/dist/services/automations/use-cases/receive-automation-event.use-case.mjs +10 -8
- 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 +10 -10
- package/dist/services/automations/use-cases/rotate-automation-event-tokens.use-case.mjs +8 -8
- 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.d.ts +2 -2
- package/dist/services/automations/use-cases/update-automation.use-case.js +16 -12
- package/dist/services/automations/use-cases/update-automation.use-case.mjs +14 -10
- package/dist/services/automations/utils/automation-fields.d.ts +3 -4
- package/dist/services/automations/utils/automation-fields.js +3 -5
- package/dist/services/automations/utils/automation-fields.mjs +4 -6
- 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 +3 -2
- package/docs/agent.md +2 -2
- package/docs/authorization.md +77 -0
- package/docs/automations.md +143 -41
- package/docs/integration-guide.md +8 -10
- package/docs/llms.txt +1 -0
- package/docs/mcp-servers.md +4 -4
- package/docs/users.md +6 -4
- package/examples/automations/create-automation.ts +6 -5
- package/examples/automations/create-event-automation.ts +1 -1
- package/examples/automations/list-automation-runs.ts +2 -0
- package/examples/automations/list-automations.ts +3 -2
- package/examples/automations/preview-automation-runs.ts +1 -1
- package/examples/automations/revoke-automation-event-token.ts +1 -1
- package/examples/automations/run-automation-now.ts +3 -1
- package/examples/automations/update-automation.ts +4 -5
- package/examples/chats/delete-chat.ts +1 -1
- package/examples/mcps/internal-mcp.ts +3 -3
- package/examples/run-agent/run-agent-with-identity.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
|
@@ -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<
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
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>;
|
|
@@ -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);
|