mirror of
https://github.com/n8n-io/n8n.git
synced 2024-12-25 04:34:06 -08:00
refactor(core): Set up ExecutionMetadata
service (no-changelog) (#7103)
Co-authored-by: कारतोफ्फेलस्क्रिप्ट™ <aditya@netroy.in>
This commit is contained in:
parent
cffda65b33
commit
b7320f5322
|
@ -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<void> {
|
|||
}
|
||||
}
|
||||
|
||||
export async function saveExecutionMetadata(
|
||||
executionId: string,
|
||||
executionMetadata: Record<string, string>,
|
||||
): Promise<ExecutionMetadata[]> {
|
||||
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);
|
||||
|
|
24
packages/cli/src/services/executionMetadata.service.ts
Normal file
24
packages/cli/src/services/executionMetadata.service.ts
Normal file
|
@ -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<string, string>,
|
||||
): Promise<ExecutionMetadata[]> {
|
||||
const metadataRows = [];
|
||||
for (const [key, value] of Object.entries(executionMetadata)) {
|
||||
metadataRows.push({
|
||||
execution: { id: executionId },
|
||||
key,
|
||||
value,
|
||||
});
|
||||
}
|
||||
|
||||
return this.executionMetadataRepository.save(metadataRows);
|
||||
}
|
||||
}
|
|
@ -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 },
|
||||
|
|
Loading…
Reference in a new issue