mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-16 17:46:45 +00:00
fix(Data Table Node): Add bulk insert option (no-changelog) (#19176)
This commit is contained in:
@@ -1,14 +1,34 @@
|
|||||||
import type { IExecuteFunctions, INodeExecutionData, AllEntities } from 'n8n-workflow';
|
import type {
|
||||||
import { NodeOperationError } from 'n8n-workflow';
|
IExecuteFunctions,
|
||||||
|
INodeExecutionData,
|
||||||
|
AllEntities,
|
||||||
|
NodeOperationError,
|
||||||
|
} from 'n8n-workflow';
|
||||||
|
|
||||||
import * as row from './row/Row.resource';
|
import * as row from './row/Row.resource';
|
||||||
|
import { DATA_TABLE_ID_FIELD } from '../common/fields';
|
||||||
|
import { getDataTableProxyExecute } from '../common/utils';
|
||||||
|
|
||||||
type DataTableNodeType = AllEntities<{
|
type DataTableNodeType = AllEntities<{
|
||||||
row: 'insert' | 'get' | 'deleteRows' | 'update' | 'upsert';
|
row: 'insert' | 'get' | 'deleteRows' | 'update' | 'upsert';
|
||||||
}>;
|
}>;
|
||||||
|
|
||||||
|
const BULK_OPERATIONS = ['insert'] as const;
|
||||||
|
|
||||||
|
function canBulk(operation: string): operation is (typeof BULK_OPERATIONS)[number] {
|
||||||
|
return (BULK_OPERATIONS as readonly string[]).includes(operation);
|
||||||
|
}
|
||||||
|
|
||||||
|
function hasComplexId(ctx: IExecuteFunctions) {
|
||||||
|
const dataStoreIdExpr = ctx.getNodeParameter(`${DATA_TABLE_ID_FIELD}.value`, 0, undefined, {
|
||||||
|
rawExpressions: true,
|
||||||
|
});
|
||||||
|
|
||||||
|
return typeof dataStoreIdExpr === 'string' && dataStoreIdExpr.includes('{');
|
||||||
|
}
|
||||||
|
|
||||||
export async function router(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
export async function router(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
||||||
const operationResult: INodeExecutionData[] = [];
|
let operationResult: INodeExecutionData[] = [];
|
||||||
let responseData: INodeExecutionData[] = [];
|
let responseData: INodeExecutionData[] = [];
|
||||||
|
|
||||||
const items = this.getInputData();
|
const items = this.getInputData();
|
||||||
@@ -20,34 +40,44 @@ export async function router(this: IExecuteFunctions): Promise<INodeExecutionDat
|
|||||||
operation,
|
operation,
|
||||||
} as DataTableNodeType;
|
} as DataTableNodeType;
|
||||||
|
|
||||||
for (let i = 0; i < items.length; i++) {
|
// If the operation supports
|
||||||
|
if (canBulk(dataTableNodeData.operation) && !hasComplexId(this)) {
|
||||||
try {
|
try {
|
||||||
switch (dataTableNodeData.resource) {
|
const proxy = await getDataTableProxyExecute(this);
|
||||||
case 'row':
|
|
||||||
responseData = await row[dataTableNodeData.operation].execute.call(this, i);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
throw new NodeOperationError(
|
|
||||||
this.getNode(),
|
|
||||||
`The resource "${resource}" is not supported!`,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
const executionData = this.helpers.constructExecutionMetaData(responseData, {
|
responseData = await row[dataTableNodeData.operation]['executeBulk'].call(this, proxy);
|
||||||
itemData: { item: i },
|
|
||||||
});
|
|
||||||
|
|
||||||
operationResult.push.apply(operationResult, executionData);
|
operationResult = responseData;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (this.continueOnFail()) {
|
if (this.continueOnFail()) {
|
||||||
operationResult.push({
|
operationResult = this.getInputData().map((json) => ({
|
||||||
json: this.getInputData(i)[0].json,
|
json,
|
||||||
error: error as NodeOperationError,
|
error: error as NodeOperationError,
|
||||||
});
|
}));
|
||||||
} else {
|
} else {
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
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 },
|
||||||
|
});
|
||||||
|
|
||||||
|
operationResult.push.apply(operationResult, executionData);
|
||||||
|
} catch (error) {
|
||||||
|
if (this.continueOnFail()) {
|
||||||
|
operationResult.push({
|
||||||
|
json: this.getInputData(i)[0].json,
|
||||||
|
error: error as NodeOperationError,
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return [operationResult];
|
return [operationResult];
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import type {
|
import type {
|
||||||
|
IDataStoreProjectService,
|
||||||
IDisplayOptions,
|
IDisplayOptions,
|
||||||
IExecuteFunctions,
|
IExecuteFunctions,
|
||||||
INodeExecutionData,
|
INodeExecutionData,
|
||||||
@@ -30,3 +31,13 @@ export async function execute(
|
|||||||
const insertedRows = await dataStoreProxy.insertRows([row]);
|
const insertedRows = await dataStoreProxy.insertRows([row]);
|
||||||
return insertedRows.map((json) => ({ json }));
|
return insertedRows.map((json) => ({ json }));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function executeBulk(
|
||||||
|
this: IExecuteFunctions,
|
||||||
|
proxy: IDataStoreProjectService,
|
||||||
|
): Promise<INodeExecutionData[]> {
|
||||||
|
const rows = this.getInputData().flatMap((_, i) => [getAddRow(this, i)]);
|
||||||
|
|
||||||
|
const insertedRows = await proxy.insertRows(rows);
|
||||||
|
return insertedRows.map((json, item) => ({ json, pairedItem: { item } }));
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user