n8n
Version:
n8n Workflow Automation Tool
100 lines • 5.46 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DataTableExporter = void 0;
const backend_common_1 = require("@n8n/backend-common");
const di_1 = require("@n8n/di");
const n8n_workflow_1 = require("n8n-workflow");
const data_table_service_1 = require("../../../../modules/data-table/data-table.service");
const data_table_serializer_1 = require("./data-table.serializer");
const unique_filename_allocator_1 = require("../../io/unique-filename-allocator");
let DataTableExporter = class DataTableExporter {
constructor(dataTableService, dataTableSerializer, moduleRegistry) {
this.dataTableService = dataTableService;
this.dataTableSerializer = dataTableSerializer;
this.moduleRegistry = moduleRegistry;
}
async export(request) {
if (request.requirements.length === 0) {
return { entries: [], requirements: [] };
}
if (!this.moduleRegistry.isActive('data-table')) {
throw new n8n_workflow_1.UserError('The exported workflows use data tables, but the data-table module is disabled on this instance.');
}
const usedByWorkflowsById = this.groupByDataTableId(request.requirements);
const requestedIds = [...usedByWorkflowsById.keys()];
const dataTables = await this.dataTableService.findDataTablesByIdsForUser(requestedIds, request.user, ['dataTable:read']);
this.assertAllRequestedDataTablesFound(requestedIds, dataTables);
const allocators = new Map();
const allocatorFor = (baseDir) => {
const existing = allocators.get(baseDir);
if (existing)
return existing;
const created = new unique_filename_allocator_1.UniqueFilenameAllocator(baseDir, 'data-table');
allocators.set(baseDir, created);
return created;
};
const entries = [];
const requirements = [];
for (const dataTable of dataTables) {
const usedByWorkflows = usedByWorkflowsById.get(dataTable.id) ?? [];
const baseDir = this.resolveBaseDir(dataTable, request.projectTargetsById);
const target = allocatorFor(baseDir).allocate(dataTable.name);
request.writer.writeDirectory(target);
request.writer.writeFile(`${target}/data-table.json`, JSON.stringify(this.dataTableSerializer.serialize(dataTable), null, '\t'));
entries.push({ id: dataTable.id, name: dataTable.name, target });
requirements.push({
id: dataTable.id,
name: dataTable.name,
usedByWorkflows,
});
}
return { entries, requirements };
}
resolveBaseDir(dataTable, projectTargetsById) {
if (!projectTargetsById || projectTargetsById.size === 0)
return 'data-tables';
const prefix = projectTargetsById.get(dataTable.projectId);
return prefix ? `${prefix}/data-tables` : 'data-tables';
}
groupByDataTableId(requirements) {
const grouped = new Map();
for (const requirement of requirements) {
const usedByWorkflows = grouped.get(requirement.dataTableId);
if (usedByWorkflows) {
if (!usedByWorkflows.includes(requirement.workflowId)) {
usedByWorkflows.push(requirement.workflowId);
}
}
else {
grouped.set(requirement.dataTableId, [requirement.workflowId]);
}
}
return grouped;
}
assertAllRequestedDataTablesFound(requestedDataTableIds, foundDataTables) {
const foundDataTableIds = new Set(foundDataTables.map(({ id }) => id));
const missingDataTableIds = requestedDataTableIds.filter((id) => !foundDataTableIds.has(id));
if (missingDataTableIds.length > 0) {
const displayedDataTableIds = missingDataTableIds.slice(0, 20);
const omittedCount = missingDataTableIds.length - displayedDataTableIds.length;
throw new n8n_workflow_1.UserError(`${missingDataTableIds.length} data table(s) not found or not accessible. Export aborted.`, {
description: `Missing data table IDs: ${displayedDataTableIds.join(', ')}${omittedCount > 0 ? `, and ${omittedCount} more` : ''}`,
});
}
}
};
exports.DataTableExporter = DataTableExporter;
exports.DataTableExporter = DataTableExporter = __decorate([
(0, di_1.Service)(),
__metadata("design:paramtypes", [data_table_service_1.DataTableService, data_table_serializer_1.DataTableSerializer, backend_common_1.ModuleRegistry])
], DataTableExporter);
//# sourceMappingURL=data-table.exporter.js.map