diff --git a/packages/cli/src/WorkflowExecuteAdditionalData.ts b/packages/cli/src/WorkflowExecuteAdditionalData.ts index 5760cce3d1..f67db960b6 100644 --- a/packages/cli/src/WorkflowExecuteAdditionalData.ts +++ b/packages/cli/src/WorkflowExecuteAdditionalData.ts @@ -62,11 +62,11 @@ import { findSubworkflowStart, isWorkflowIdValid } from '@/utils'; import { PermissionChecker } from './UserManagement/PermissionChecker'; import { WorkflowsService } from './workflows/workflows.services'; import { InternalHooks } from '@/InternalHooks'; -import type { ExecutionMetadata } from '@db/entities/ExecutionMetadata'; import { ExecutionRepository } from '@db/repositories'; import { EventsService } from '@/services/events.service'; import { SecretsHelper } from './SecretsHelpers'; import { OwnershipService } from './services/ownership.service'; +import { ExecutionMetadataService } from './services/executionMetadata.service'; const ERROR_TRIGGER_TYPE = config.getEnv('nodes.errorTriggerType'); @@ -252,22 +252,6 @@ async function pruneExecutionData(this: WorkflowHooks): Promise { } } -export async function saveExecutionMetadata( - executionId: string, - executionMetadata: Record, -): Promise { - const metadataRows = []; - for (const [key, value] of Object.entries(executionMetadata)) { - metadataRows.push({ - execution: { id: executionId }, - key, - value, - }); - } - - return Db.collections.ExecutionMetadata.save(metadataRows); -} - /** * Returns hook functions to push data to Editor-UI * @@ -663,7 +647,10 @@ function hookFunctionsSave(parentProcessMode?: string): IWorkflowExecuteHooks { try { if (fullRunData.data.resultData.metadata) { - await saveExecutionMetadata(this.executionId, fullRunData.data.resultData.metadata); + await Container.get(ExecutionMetadataService).save( + this.executionId, + fullRunData.data.resultData.metadata, + ); } } catch (e) { Logger.error(`Failed to save metadata for execution ID ${this.executionId}`, e); @@ -800,7 +787,10 @@ function hookFunctionsSaveWorker(): IWorkflowExecuteHooks { try { if (fullRunData.data.resultData.metadata) { - await saveExecutionMetadata(this.executionId, fullRunData.data.resultData.metadata); + await Container.get(ExecutionMetadataService).save( + this.executionId, + fullRunData.data.resultData.metadata, + ); } } catch (e) { Logger.error(`Failed to save metadata for execution ID ${this.executionId}`, e); diff --git a/packages/cli/src/services/executionMetadata.service.ts b/packages/cli/src/services/executionMetadata.service.ts new file mode 100644 index 0000000000..bccb7172ee --- /dev/null +++ b/packages/cli/src/services/executionMetadata.service.ts @@ -0,0 +1,24 @@ +import { ExecutionMetadataRepository } from '@/databases/repositories'; +import { Service } from 'typedi'; +import type { ExecutionMetadata } from '@/databases/entities/ExecutionMetadata'; + +@Service() +export class ExecutionMetadataService { + constructor(private readonly executionMetadataRepository: ExecutionMetadataRepository) {} + + async save( + executionId: string, + executionMetadata: Record, + ): Promise { + const metadataRows = []; + for (const [key, value] of Object.entries(executionMetadata)) { + metadataRows.push({ + execution: { id: executionId }, + key, + value, + }); + } + + return this.executionMetadataRepository.save(metadataRows); + } +} diff --git a/packages/cli/test/unit/WorkflowExecuteAdditionalData.test.ts b/packages/cli/test/unit/WorkflowExecuteAdditionalData.test.ts index 6007c0dd74..5d72a01f2e 100644 --- a/packages/cli/test/unit/WorkflowExecuteAdditionalData.test.ts +++ b/packages/cli/test/unit/WorkflowExecuteAdditionalData.test.ts @@ -1,18 +1,11 @@ -import { saveExecutionMetadata } from '@/WorkflowExecuteAdditionalData'; -import * as Db from '@/Db'; -import { mocked } from 'jest-mock'; - -jest.mock('@/Db', () => { - return { - collections: { - ExecutionMetadata: { - save: jest.fn(async () => []), - }, - }, - }; -}); +import { Container } from 'typedi'; +import { ExecutionMetadataRepository } from '@db/repositories'; +import { ExecutionMetadataService } from '@/services/executionMetadata.service'; +import { mockInstance } from '../integration/shared/utils'; describe('WorkflowExecuteAdditionalData', () => { + const repository = mockInstance(ExecutionMetadataRepository); + test('Execution metadata is saved in a batch', async () => { const toSave = { test1: 'value1', @@ -20,10 +13,10 @@ describe('WorkflowExecuteAdditionalData', () => { }; const executionId = '1234'; - await saveExecutionMetadata(executionId, toSave); + await Container.get(ExecutionMetadataService).save(executionId, toSave); - expect(mocked(Db.collections.ExecutionMetadata.save)).toHaveBeenCalledTimes(1); - expect(mocked(Db.collections.ExecutionMetadata.save).mock.calls[0]).toEqual([ + expect(repository.save).toHaveBeenCalledTimes(1); + expect(repository.save.mock.calls[0]).toEqual([ [ { execution: { id: executionId },