Repository: n8n-io/n8n
File: packages/cli/src/executions/execution-persistence.ts (TypeScript)

Code before:
		private readonly executionRepository: ExecutionRepository,
		private readonly binaryDataService: BinaryDataService,
		private readonly fsStore: FsStore,
		private readonly storageConfig: StorageConfig,
	) {}

	async create(payload: CreateExecutionPayload) {
		const { data: rawData, workflowData, ...rest } = payload;
		const { connections, nodes, name, settings, id } = workflowData;
		const storedAt = this.storageConfig.modeTag;
		const executionEntity = { ...rest, createdAt: new Date(), storedAt };
		const data = stringify(rawData);
		const workflowVersionId = workflowData.versionId ?? null;

		return await this.executionRepository.manager.transaction(async (tx) => {
			const { identifiers } = await tx.insert(ExecutionEntity, executionEntity);
			const executionId = String(identifiers[0].id);

			if (storedAt === 'db') {
				await tx.insert(ExecutionData, {
					executionId,
					workflowData: { connections, nodes, name, settings, id },
					data,
					workflowVersionId,
				});
				return executionId;
			}

			await this.fsStore.write(
				{ workflowId: workflowData.id, executionId },
				{ data, workflowData, workflowVersionId },
			);
			return executionId;
		});
	}

	async hardDelete(target: DeletionTarget | DeletionTarget[]) {
		const targets = Array.isArray(target) ? target : [target];
		if (targets.length === 0) return;

		const fsTargets = targets.filter((t) => t.storedAt === 'fs');

		await Promise.all([
			this.executionRepository.deleteByIds(targets.map((t) => t.executionId)),
			this.binaryDataService.deleteMany(targets.map((t) => ({ type: 'execution' as const, ...t }))),
			fsTargets.length > 0 ? this.fsStore.delete(fsTargets) : Promise.resolve(),
		]);
	}

	async hardDeleteBy(criteria: ExecutionDeletionCriteria) {
		const refs = await this.executionRepository.deleteExecutionsByFilter(criteria);

Review comment (suggestion):
I would imagine in the long run each "driver" have the exact same interface. That way we wouldn't have to create conditional code like this, but instead would say something like `persistence.writeExecution(..)` where `persistence` refers to the currently configured driver. So the DB driver would implement the same interface as all the other drivers

Code after:
		private readonly executionRepository: ExecutionRepository,
		private readonly binaryDataService: BinaryDataService,
		private readonly fsStore: FsStore,
		private readonly storageConfig: StorageConfig,
	) {}

	/**
	 * Create an execution entity and persist its data to the configured storage.
	 * - In `db` mode, we write both entity and data to the DB in a transaction.
	 * - In `fs` mode, we write the entity to the DB and its data to the filesystem.
	 */
	async create(payload: CreateExecutionPayload) {
		const { data: rawData, workflowData, ...rest } = payload;
		const { connections, nodes, name, settings, id } = workflowData;
		const workflowSnapshot: WorkflowSnapshot = { connections, nodes, name, settings, id };
		const storedAt = this.storageConfig.modeTag;
		const executionEntity = { ...rest, createdAt: new Date(), storedAt };
		const data = stringify(rawData);
		const workflowVersionId = workflowData.versionId ?? null;

		return await this.executionRepository.manager.transaction(async (tx) => {
			const { identifiers } = await tx.insert(ExecutionEntity, executionEntity);
			const executionId = String(identifiers[0].id);

			if (storedAt === 'db') {
				await tx.insert(ExecutionData, {
					executionId,
					workflowData: workflowSnapshot,
					data,
					workflowVersionId,
				});
				return executionId;
			}

			await this.fsStore.write(
				{ workflowId: id, executionId },
				{ data, workflowData: workflowSnapshot, workflowVersionId },
			);
			return executionId;
		});
	}

	async hardDelete(target: DeletionTarget | DeletionTarget[]) {
		const targets = Array.isArray(target) ? target : [target];
		if (targets.length === 0) return;

		const fsTargets = targets.filter((t) => t.storedAt === 'fs');

		await Promise.all([
			this.executionRepository.deleteByIds(targets.map((t) => t.executionId)),
			this.binaryDataService.deleteMany(targets.map((t) => ({ type: 'execution' as const, ...t }))),