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 { NodeOperationError } from 'n8n-workflow';
|
||||
import type {
|
||||
IExecuteFunctions,
|
||||
INodeExecutionData,
|
||||
AllEntities,
|
||||
NodeOperationError,
|
||||
} from 'n8n-workflow';
|
||||
|
||||
import * as row from './row/Row.resource';
|
||||
import { DATA_TABLE_ID_FIELD } from '../common/fields';
|
||||
import { getDataTableProxyExecute } from '../common/utils';
|
||||
|
||||
type DataTableNodeType = AllEntities<{
|
||||
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[][]> {
|
||||
const operationResult: INodeExecutionData[] = [];
|
||||
let operationResult: INodeExecutionData[] = [];
|
||||
let responseData: INodeExecutionData[] = [];
|
||||
|
||||
const items = this.getInputData();
|
||||
@@ -20,19 +40,28 @@ export async function router(this: IExecuteFunctions): Promise<INodeExecutionDat
|
||||
operation,
|
||||
} as DataTableNodeType;
|
||||
|
||||
// If the operation supports
|
||||
if (canBulk(dataTableNodeData.operation) && !hasComplexId(this)) {
|
||||
try {
|
||||
const proxy = await getDataTableProxyExecute(this);
|
||||
|
||||
responseData = await row[dataTableNodeData.operation]['executeBulk'].call(this, proxy);
|
||||
|
||||
operationResult = responseData;
|
||||
} catch (error) {
|
||||
if (this.continueOnFail()) {
|
||||
operationResult = this.getInputData().map((json) => ({
|
||||
json,
|
||||
error: error as NodeOperationError,
|
||||
}));
|
||||
} else {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for (let i = 0; i < items.length; i++) {
|
||||
try {
|
||||
switch (dataTableNodeData.resource) {
|
||||
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, {
|
||||
itemData: { item: i },
|
||||
});
|
||||
@@ -49,6 +78,7 @@ export async function router(this: IExecuteFunctions): Promise<INodeExecutionDat
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return [operationResult];
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import type {
|
||||
IDataStoreProjectService,
|
||||
IDisplayOptions,
|
||||
IExecuteFunctions,
|
||||
INodeExecutionData,
|
||||
@@ -30,3 +31,13 @@ export async function execute(
|
||||
const insertedRows = await dataStoreProxy.insertRows([row]);
|
||||
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