UNPKG

n8n-nodes-base

Version:

Base nodes of n8n

144 lines 5.93 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.router = router; const n8n_workflow_1 = require("n8n-workflow"); const row = __importStar(require("./row/Row.resource")); const table = __importStar(require("./table/Table.resource")); const fields_1 = require("../common/fields"); const utils_1 = require("../common/utils"); const BULK_OPERATIONS = ['insert']; function hasBulkExecute(operation) { return BULK_OPERATIONS.includes(operation); } function hasComplexId(ctx) { const dataTableIdExpr = ctx.getNodeParameter(`${fields_1.DATA_TABLE_ID_FIELD}.value`, 0, undefined, { rawExpressions: true, }); return typeof dataTableIdExpr === 'string' && dataTableIdExpr.includes('{'); } async function router() { let operationResult = []; let responseData = []; const items = this.getInputData(); const resource = this.getNodeParameter('resource', 0); const operation = this.getNodeParameter('operation', 0); const dataTableNodeData = { resource, operation, }; if (dataTableNodeData.resource === 'table') { // Table operations for (let i = 0; i < items.length; i++) { try { const tableOperation = dataTableNodeData.operation === 'delete' ? table.deleteTable : table[dataTableNodeData.operation]; responseData = await tableOperation.execute.call(this, i); const executionData = this.helpers.constructExecutionMetaData(responseData, { itemData: { item: i }, }); operationResult = operationResult.concat(executionData); } catch (error) { if (this.continueOnFail()) { const inputData = this.getInputData(i)[0].json; if (error instanceof n8n_workflow_1.NodeApiError || error instanceof n8n_workflow_1.NodeOperationError) { operationResult.push({ json: inputData, error }); } else { operationResult.push({ json: inputData, error: new n8n_workflow_1.NodeOperationError(this.getNode(), error), }); } } else { throw error; } } } } else if (hasBulkExecute(dataTableNodeData.operation) && !hasComplexId(this)) { // Row bulk operations try { const proxy = await (0, utils_1.getDataTableProxyExecute)(this); responseData = await row[dataTableNodeData.operation]['executeBulk'].call(this, proxy); operationResult = responseData; } catch (error) { if (this.continueOnFail()) { if (error instanceof n8n_workflow_1.NodeApiError || error instanceof n8n_workflow_1.NodeOperationError) { operationResult = this.getInputData().map((json) => ({ json, error })); } else { operationResult = this.getInputData().map((json) => ({ json })); } } else { throw error; } } } else { // Row operations for (let i = 0; i < items.length; i++) { try { responseData = await row[dataTableNodeData.operation].execute.call(this, i); const executionData = this.helpers.constructExecutionMetaData(responseData, { itemData: { item: i }, }); // pushing here risks stack overflows for very high numbers (~100k) of results on filter-based queries (update, get, etc.) operationResult = operationResult.concat(executionData); } catch (error) { if (this.continueOnFail()) { const inputData = this.getInputData(i)[0].json; if (error instanceof n8n_workflow_1.NodeApiError || error instanceof n8n_workflow_1.NodeOperationError) { operationResult.push({ json: inputData, error }); } else { operationResult.push({ json: inputData }); } } else { throw error; } } } } return [operationResult]; } //# sourceMappingURL=router.js.map