first commit
Security: Sync from Public / sync-from-public (push) Has been cancelled
Test: Benchmark Nightly / build (push) Has been cancelled
Test: Benchmark Nightly / Notify Cats on failure (push) Has been cancelled
CI: Python / Checks (push) Has been cancelled
Test: Evals Python / Workflow Comparison Python (push) Has been cancelled
Util: Check Docs URLs / check-docs-urls (push) Has been cancelled
Test: Visual Storybook / Cloudflare Pages (push) Has been cancelled
Test: E2E Performance / build-and-test-performance (push) Has been cancelled
Test: Workflows Nightly / Run Workflow Tests (push) Has been cancelled
Util: Cleanup CI Docker Images / Delete stale CI images (push) Has been cancelled
Test: Benchmark Destroy Env / build (push) Has been cancelled
Util: Update Node Popularity / update-popularity (push) Has been cancelled
Test: E2E Coverage Weekly / Coverage Tests (push) Has been cancelled
Security: Sync from Public / sync-from-public (push) Has been cancelled
Test: Benchmark Nightly / build (push) Has been cancelled
Test: Benchmark Nightly / Notify Cats on failure (push) Has been cancelled
CI: Python / Checks (push) Has been cancelled
Test: Evals Python / Workflow Comparison Python (push) Has been cancelled
Util: Check Docs URLs / check-docs-urls (push) Has been cancelled
Test: Visual Storybook / Cloudflare Pages (push) Has been cancelled
Test: E2E Performance / build-and-test-performance (push) Has been cancelled
Test: Workflows Nightly / Run Workflow Tests (push) Has been cancelled
Util: Cleanup CI Docker Images / Delete stale CI images (push) Has been cancelled
Test: Benchmark Destroy Env / build (push) Has been cancelled
Util: Update Node Popularity / update-popularity (push) Has been cancelled
Test: E2E Coverage Weekly / Coverage Tests (push) Has been cancelled
This commit is contained in:
@@ -0,0 +1,122 @@
|
||||
import type {
|
||||
IExecuteFunctions,
|
||||
INodeExecutionData,
|
||||
AllEntities,
|
||||
DataTableRowOperation,
|
||||
DataTableTableOperation,
|
||||
} from 'n8n-workflow';
|
||||
import { NodeApiError, NodeOperationError } from 'n8n-workflow';
|
||||
|
||||
import * as row from './row/Row.resource';
|
||||
import * as table from './table/Table.resource';
|
||||
import { DATA_TABLE_ID_FIELD } from '../common/fields';
|
||||
import { getDataTableProxyExecute } from '../common/utils';
|
||||
|
||||
type DataTableNodeType = AllEntities<{
|
||||
row: DataTableRowOperation;
|
||||
table: DataTableTableOperation;
|
||||
}>;
|
||||
|
||||
const BULK_OPERATIONS = ['insert'] as const;
|
||||
|
||||
function hasBulkExecute(operation: string): operation is (typeof BULK_OPERATIONS)[number] {
|
||||
return (BULK_OPERATIONS as readonly string[]).includes(operation);
|
||||
}
|
||||
|
||||
function hasComplexId(ctx: IExecuteFunctions) {
|
||||
const dataTableIdExpr = ctx.getNodeParameter(`${DATA_TABLE_ID_FIELD}.value`, 0, undefined, {
|
||||
rawExpressions: true,
|
||||
});
|
||||
|
||||
return typeof dataTableIdExpr === 'string' && dataTableIdExpr.includes('{');
|
||||
}
|
||||
|
||||
export async function router(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
||||
let operationResult: INodeExecutionData[] = [];
|
||||
let responseData: INodeExecutionData[] = [];
|
||||
|
||||
const items = this.getInputData();
|
||||
const resource = this.getNodeParameter('resource', 0);
|
||||
const operation = this.getNodeParameter('operation', 0);
|
||||
|
||||
const dataTableNodeData = {
|
||||
resource,
|
||||
operation,
|
||||
} as DataTableNodeType;
|
||||
|
||||
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 NodeApiError || error instanceof NodeOperationError) {
|
||||
operationResult.push({ json: inputData, error });
|
||||
} else {
|
||||
operationResult.push({
|
||||
json: inputData,
|
||||
error: new NodeOperationError(this.getNode(), error as Error),
|
||||
});
|
||||
}
|
||||
} else {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if (hasBulkExecute(dataTableNodeData.operation) && !hasComplexId(this)) {
|
||||
// Row bulk operations
|
||||
try {
|
||||
const proxy = await getDataTableProxyExecute(this);
|
||||
|
||||
responseData = await row[dataTableNodeData.operation]['executeBulk'].call(this, proxy);
|
||||
|
||||
operationResult = responseData;
|
||||
} catch (error) {
|
||||
if (this.continueOnFail()) {
|
||||
if (error instanceof NodeApiError || error instanceof 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 NodeApiError || error instanceof NodeOperationError) {
|
||||
operationResult.push({ json: inputData, error });
|
||||
} else {
|
||||
operationResult.push({ json: inputData });
|
||||
}
|
||||
} else {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return [operationResult];
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
import type { INodeProperties } from 'n8n-workflow';
|
||||
|
||||
import * as deleteRows from './delete.operation';
|
||||
import * as rowExists from './rowExists.operation';
|
||||
import * as rowNotExists from './rowNotExists.operation';
|
||||
import * as get from './get.operation';
|
||||
import * as insert from './insert.operation';
|
||||
import * as update from './update.operation';
|
||||
import * as upsert from './upsert.operation';
|
||||
import { DATA_TABLE_RESOURCE_LOCATOR_BASE } from '../../common/fields';
|
||||
|
||||
export { insert, get, rowExists, rowNotExists, deleteRows, update, upsert };
|
||||
|
||||
export const description: INodeProperties[] = [
|
||||
{
|
||||
displayName: 'Operation',
|
||||
name: 'operation',
|
||||
type: 'options',
|
||||
noDataExpression: true,
|
||||
displayOptions: {
|
||||
show: {
|
||||
resource: ['row'],
|
||||
},
|
||||
},
|
||||
options: [
|
||||
{
|
||||
name: 'Delete',
|
||||
value: deleteRows.FIELD,
|
||||
description: 'Delete row(s)',
|
||||
action: 'Delete row(s)',
|
||||
},
|
||||
{
|
||||
name: 'Get',
|
||||
value: get.FIELD,
|
||||
description: 'Get row(s)',
|
||||
action: 'Get row(s)',
|
||||
},
|
||||
{
|
||||
name: 'If Row Exists',
|
||||
value: rowExists.FIELD,
|
||||
description: 'Match input items that are in the data table',
|
||||
action: 'If row exists',
|
||||
},
|
||||
{
|
||||
name: 'If Row Does Not Exist',
|
||||
value: rowNotExists.FIELD,
|
||||
description: 'Match input items that are not in the data table',
|
||||
action: 'If row does not exist',
|
||||
},
|
||||
{
|
||||
name: 'Insert',
|
||||
value: insert.FIELD,
|
||||
description: 'Insert a new row',
|
||||
action: 'Insert row',
|
||||
},
|
||||
{
|
||||
name: 'Update',
|
||||
value: update.FIELD,
|
||||
description: 'Update row(s) matching certain fields',
|
||||
action: 'Update row(s)',
|
||||
},
|
||||
{
|
||||
name: 'Upsert',
|
||||
value: upsert.FIELD,
|
||||
description: 'Update row(s), or insert if there is no match',
|
||||
action: 'Upsert row(s)',
|
||||
},
|
||||
],
|
||||
default: 'insert',
|
||||
},
|
||||
{
|
||||
...DATA_TABLE_RESOURCE_LOCATOR_BASE,
|
||||
modes: [
|
||||
{
|
||||
...DATA_TABLE_RESOURCE_LOCATOR_BASE.modes[0],
|
||||
typeOptions: {
|
||||
...DATA_TABLE_RESOURCE_LOCATOR_BASE.modes[0].typeOptions,
|
||||
allowNewResource: {
|
||||
label: 'resourceLocator.dataTable.createNew',
|
||||
url: '/projects/{{$projectId}}/datatables/new',
|
||||
},
|
||||
},
|
||||
},
|
||||
...DATA_TABLE_RESOURCE_LOCATOR_BASE.modes.slice(1),
|
||||
],
|
||||
displayOptions: { show: { resource: ['row'] } },
|
||||
},
|
||||
...deleteRows.description,
|
||||
...insert.description,
|
||||
...get.description,
|
||||
...rowExists.description,
|
||||
...rowNotExists.description,
|
||||
...update.description,
|
||||
...upsert.description,
|
||||
];
|
||||
@@ -0,0 +1,51 @@
|
||||
import {
|
||||
NodeOperationError,
|
||||
type IDisplayOptions,
|
||||
type IExecuteFunctions,
|
||||
type INodeExecutionData,
|
||||
type INodeProperties,
|
||||
} from 'n8n-workflow';
|
||||
|
||||
import { DRY_RUN } from '../../common/fields';
|
||||
import { getSelectFields, getSelectFilter } from '../../common/selectMany';
|
||||
import { getDataTableProxyExecute, getDryRunParameter } from '../../common/utils';
|
||||
|
||||
// named `deleteRows` since `delete` is a reserved keyword
|
||||
export const FIELD: string = 'deleteRows';
|
||||
|
||||
const displayOptions: IDisplayOptions = {
|
||||
show: {
|
||||
resource: ['row'],
|
||||
operation: [FIELD],
|
||||
},
|
||||
};
|
||||
|
||||
export const description: INodeProperties[] = [
|
||||
...getSelectFields(displayOptions),
|
||||
{
|
||||
displayName: 'Options',
|
||||
name: 'options',
|
||||
type: 'collection',
|
||||
default: {},
|
||||
placeholder: 'Add option',
|
||||
options: [DRY_RUN],
|
||||
displayOptions,
|
||||
},
|
||||
];
|
||||
|
||||
export async function execute(
|
||||
this: IExecuteFunctions,
|
||||
index: number,
|
||||
): Promise<INodeExecutionData[]> {
|
||||
const dataTableProxy = await getDataTableProxyExecute(this, index);
|
||||
const dryRun = getDryRunParameter(this, index);
|
||||
const filter = await getSelectFilter(this, index);
|
||||
|
||||
if (filter.filters.length === 0) {
|
||||
throw new NodeOperationError(this.getNode(), 'At least one condition is required');
|
||||
}
|
||||
|
||||
const result = await dataTableProxy.deleteRows({ filter, dryRun });
|
||||
|
||||
return result.map((json) => ({ json }));
|
||||
}
|
||||
@@ -0,0 +1,122 @@
|
||||
import type {
|
||||
IDisplayOptions,
|
||||
IExecuteFunctions,
|
||||
INodeExecutionData,
|
||||
INodeProperties,
|
||||
} from 'n8n-workflow';
|
||||
|
||||
import { ROWS_LIMIT_DEFAULT } from '../../common/constants';
|
||||
import { executeSelectMany, getSelectFields } from '../../common/selectMany';
|
||||
import { getDataTableProxyExecute } from '../../common/utils';
|
||||
|
||||
export const FIELD: string = 'get';
|
||||
|
||||
const displayOptions: IDisplayOptions = {
|
||||
show: {
|
||||
resource: ['row'],
|
||||
operation: [FIELD],
|
||||
},
|
||||
};
|
||||
|
||||
export const description: INodeProperties[] = [
|
||||
...getSelectFields(displayOptions),
|
||||
{
|
||||
displayName: 'Return All',
|
||||
name: 'returnAll',
|
||||
type: 'boolean',
|
||||
displayOptions,
|
||||
default: false,
|
||||
description: 'Whether to return all results or only up to a given limit',
|
||||
},
|
||||
{
|
||||
displayName: 'Limit',
|
||||
name: 'limit',
|
||||
type: 'number',
|
||||
displayOptions: {
|
||||
...displayOptions,
|
||||
show: {
|
||||
...displayOptions.show,
|
||||
returnAll: [false],
|
||||
},
|
||||
},
|
||||
typeOptions: {
|
||||
minValue: 1,
|
||||
},
|
||||
default: ROWS_LIMIT_DEFAULT,
|
||||
description: 'Max number of results to return',
|
||||
},
|
||||
{
|
||||
displayName: 'Order By',
|
||||
name: 'orderBy',
|
||||
type: 'boolean',
|
||||
displayOptions,
|
||||
default: false,
|
||||
description: 'Whether to sort the results by a column',
|
||||
},
|
||||
{
|
||||
// eslint-disable-next-line n8n-nodes-base/node-param-display-name-wrong-for-dynamic-options
|
||||
displayName: 'Order By Column',
|
||||
name: 'orderByColumn',
|
||||
type: 'options',
|
||||
// eslint-disable-next-line n8n-nodes-base/node-param-description-wrong-for-dynamic-options
|
||||
description:
|
||||
'Choose from the list, or specify using an <a href="https://docs.n8n.io/code/expressions/">expression</a>',
|
||||
typeOptions: {
|
||||
loadOptionsDependsOn: ['dataTableId.value'],
|
||||
loadOptionsMethod: 'getDataTableColumns',
|
||||
},
|
||||
displayOptions: {
|
||||
...displayOptions,
|
||||
show: {
|
||||
...displayOptions.show,
|
||||
orderBy: [true],
|
||||
},
|
||||
},
|
||||
default: 'createdAt',
|
||||
},
|
||||
{
|
||||
displayName: 'Order By Direction',
|
||||
name: 'orderByDirection',
|
||||
type: 'options',
|
||||
options: [
|
||||
{
|
||||
name: 'Ascending',
|
||||
value: 'ASC',
|
||||
},
|
||||
{
|
||||
name: 'Descending',
|
||||
value: 'DESC',
|
||||
},
|
||||
],
|
||||
displayOptions: {
|
||||
...displayOptions,
|
||||
show: {
|
||||
...displayOptions.show,
|
||||
orderBy: [true],
|
||||
},
|
||||
},
|
||||
default: 'DESC',
|
||||
description: 'Sort direction for the column',
|
||||
},
|
||||
];
|
||||
|
||||
export async function execute(
|
||||
this: IExecuteFunctions,
|
||||
index: number,
|
||||
): Promise<INodeExecutionData[]> {
|
||||
const dataTableProxy = await getDataTableProxyExecute(this, index);
|
||||
|
||||
// Extract sort parameters
|
||||
let sortBy: [string, 'ASC' | 'DESC'] | undefined;
|
||||
const orderBy = this.getNodeParameter('orderBy', index, false) as boolean;
|
||||
|
||||
if (orderBy) {
|
||||
const column = this.getNodeParameter('orderByColumn', index, '') as string;
|
||||
const direction = this.getNodeParameter('orderByDirection', index, 'ASC') as 'ASC' | 'DESC';
|
||||
if (column) {
|
||||
sortBy = [column, direction];
|
||||
}
|
||||
}
|
||||
|
||||
return await executeSelectMany(this, index, dataTableProxy, false, undefined, sortBy);
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
import type {
|
||||
IDataTableProjectService,
|
||||
IDisplayOptions,
|
||||
IExecuteFunctions,
|
||||
INodeExecutionData,
|
||||
INodeProperties,
|
||||
} from 'n8n-workflow';
|
||||
|
||||
import { getAddRow, makeAddRow } from '../../common/addRow';
|
||||
import { getDataTableProxyExecute } from '../../common/utils';
|
||||
|
||||
export const FIELD: string = 'insert';
|
||||
|
||||
const displayOptions: IDisplayOptions = {
|
||||
show: {
|
||||
resource: ['row'],
|
||||
operation: [FIELD],
|
||||
},
|
||||
};
|
||||
|
||||
export const description: INodeProperties[] = [
|
||||
makeAddRow(FIELD, displayOptions),
|
||||
{
|
||||
displayName: 'Options',
|
||||
name: 'options',
|
||||
type: 'collection',
|
||||
placeholder: 'Add Option',
|
||||
default: {},
|
||||
options: [
|
||||
{
|
||||
displayName: 'Optimize Bulk',
|
||||
name: 'optimizeBulk',
|
||||
type: 'boolean',
|
||||
default: false,
|
||||
noDataExpression: true, // bulk inserts don't support expressions so this is a bit paradoxical
|
||||
description: 'Whether to improve bulk insert performance 5x by not returning inserted data',
|
||||
},
|
||||
],
|
||||
displayOptions,
|
||||
},
|
||||
];
|
||||
|
||||
export async function execute(
|
||||
this: IExecuteFunctions,
|
||||
index: number,
|
||||
): Promise<INodeExecutionData[]> {
|
||||
const optimizeBulkEnabled = this.getNodeParameter('options.optimizeBulk', index, false);
|
||||
const dataTableProxy = await getDataTableProxyExecute(this, index);
|
||||
|
||||
const row = getAddRow(this, index);
|
||||
|
||||
if (optimizeBulkEnabled) {
|
||||
// This function is always called by index, so we inherently cannot operate in bulk
|
||||
this.addExecutionHints({
|
||||
message: 'Unable to optimize bulk insert due to expression in Data table ID ',
|
||||
location: 'outputPane',
|
||||
});
|
||||
const json = await dataTableProxy.insertRows([row], 'count');
|
||||
return [{ json }];
|
||||
} else {
|
||||
const insertedRows = await dataTableProxy.insertRows([row], 'all');
|
||||
return insertedRows.map((json, item) => ({ json, pairedItem: { item } }));
|
||||
}
|
||||
}
|
||||
|
||||
export async function executeBulk(
|
||||
this: IExecuteFunctions,
|
||||
proxy: IDataTableProjectService,
|
||||
): Promise<INodeExecutionData[]> {
|
||||
const optimizeBulkEnabled = this.getNodeParameter('options.optimizeBulk', 0, false);
|
||||
const rows = this.getInputData().flatMap((_, i) => [getAddRow(this, i)]);
|
||||
|
||||
if (optimizeBulkEnabled) {
|
||||
const json = await proxy.insertRows(rows, 'count');
|
||||
return [{ json }];
|
||||
} else {
|
||||
const insertedRows = await proxy.insertRows(rows, 'all');
|
||||
return insertedRows.map((json, item) => ({ json, pairedItem: { item } }));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
import type {
|
||||
IDisplayOptions,
|
||||
IExecuteFunctions,
|
||||
INodeExecutionData,
|
||||
INodeProperties,
|
||||
} from 'n8n-workflow';
|
||||
|
||||
import { executeSelectMany, getSelectFields } from '../../common/selectMany';
|
||||
import { getDataTableProxyExecute } from '../../common/utils';
|
||||
|
||||
export const FIELD: string = 'rowExists';
|
||||
|
||||
const displayOptions: IDisplayOptions = {
|
||||
show: {
|
||||
resource: ['row'],
|
||||
operation: [FIELD],
|
||||
},
|
||||
};
|
||||
|
||||
export const description: INodeProperties[] = [...getSelectFields(displayOptions, true, true)];
|
||||
|
||||
export async function execute(
|
||||
this: IExecuteFunctions,
|
||||
index: number,
|
||||
): Promise<INodeExecutionData[]> {
|
||||
const dataTableProxy = await getDataTableProxyExecute(this, index);
|
||||
const hits = await executeSelectMany(this, index, dataTableProxy, undefined, 1);
|
||||
return hits.length > 0 ? [this.getInputData()[index]] : [];
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
import type {
|
||||
IDisplayOptions,
|
||||
IExecuteFunctions,
|
||||
INodeExecutionData,
|
||||
INodeProperties,
|
||||
} from 'n8n-workflow';
|
||||
|
||||
import { executeSelectMany, getSelectFields } from '../../common/selectMany';
|
||||
import { getDataTableProxyExecute } from '../../common/utils';
|
||||
|
||||
export const FIELD: string = 'rowNotExists';
|
||||
|
||||
const displayOptions: IDisplayOptions = {
|
||||
show: {
|
||||
resource: ['row'],
|
||||
operation: [FIELD],
|
||||
},
|
||||
};
|
||||
|
||||
export const description: INodeProperties[] = [...getSelectFields(displayOptions, true, true)];
|
||||
|
||||
export async function execute(
|
||||
this: IExecuteFunctions,
|
||||
index: number,
|
||||
): Promise<INodeExecutionData[]> {
|
||||
const dataTableProxy = await getDataTableProxyExecute(this, index);
|
||||
const hits = await executeSelectMany(this, index, dataTableProxy, undefined, 1);
|
||||
return hits.length === 0 ? [this.getInputData()[index]] : [];
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
import {
|
||||
NodeOperationError,
|
||||
type IDisplayOptions,
|
||||
type IExecuteFunctions,
|
||||
type INodeExecutionData,
|
||||
type INodeProperties,
|
||||
} from 'n8n-workflow';
|
||||
|
||||
import { makeAddRow, getAddRow } from '../../common/addRow';
|
||||
import { DRY_RUN } from '../../common/fields';
|
||||
import { getSelectFields, getSelectFilter } from '../../common/selectMany';
|
||||
import { getDataTableProxyExecute, getDryRunParameter } from '../../common/utils';
|
||||
|
||||
export const FIELD: string = 'update';
|
||||
|
||||
const displayOptions: IDisplayOptions = {
|
||||
show: {
|
||||
resource: ['row'],
|
||||
operation: [FIELD],
|
||||
},
|
||||
};
|
||||
|
||||
export const description: INodeProperties[] = [
|
||||
...getSelectFields(displayOptions),
|
||||
makeAddRow(FIELD, displayOptions),
|
||||
{
|
||||
displayName: 'Options',
|
||||
name: 'options',
|
||||
type: 'collection',
|
||||
default: {},
|
||||
placeholder: 'Add option',
|
||||
options: [DRY_RUN],
|
||||
displayOptions,
|
||||
},
|
||||
];
|
||||
|
||||
export async function execute(
|
||||
this: IExecuteFunctions,
|
||||
index: number,
|
||||
): Promise<INodeExecutionData[]> {
|
||||
const dataTableProxy = await getDataTableProxyExecute(this, index);
|
||||
const dryRun = getDryRunParameter(this, index);
|
||||
const row = getAddRow(this, index);
|
||||
const filter = await getSelectFilter(this, index);
|
||||
|
||||
if (filter.filters.length === 0) {
|
||||
throw new NodeOperationError(this.getNode(), 'At least one condition is required');
|
||||
}
|
||||
|
||||
const updatedRows = await dataTableProxy.updateRows({
|
||||
data: row,
|
||||
filter,
|
||||
dryRun,
|
||||
});
|
||||
|
||||
return updatedRows.map((json) => ({ json }));
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
import {
|
||||
NodeOperationError,
|
||||
type IDisplayOptions,
|
||||
type IExecuteFunctions,
|
||||
type INodeExecutionData,
|
||||
type INodeProperties,
|
||||
} from 'n8n-workflow';
|
||||
|
||||
import { makeAddRow, getAddRow } from '../../common/addRow';
|
||||
import { DRY_RUN } from '../../common/fields';
|
||||
import { getSelectFields, getSelectFilter } from '../../common/selectMany';
|
||||
import { getDataTableProxyExecute, getDryRunParameter } from '../../common/utils';
|
||||
|
||||
export const FIELD: string = 'upsert';
|
||||
|
||||
const displayOptions: IDisplayOptions = {
|
||||
show: {
|
||||
resource: ['row'],
|
||||
operation: [FIELD],
|
||||
},
|
||||
};
|
||||
|
||||
export const description: INodeProperties[] = [
|
||||
...getSelectFields(displayOptions, true),
|
||||
makeAddRow(FIELD, displayOptions),
|
||||
{
|
||||
displayName: 'Options',
|
||||
name: 'options',
|
||||
type: 'collection',
|
||||
default: {},
|
||||
placeholder: 'Add option',
|
||||
options: [DRY_RUN],
|
||||
displayOptions,
|
||||
},
|
||||
];
|
||||
|
||||
export async function execute(
|
||||
this: IExecuteFunctions,
|
||||
index: number,
|
||||
): Promise<INodeExecutionData[]> {
|
||||
const dataTableProxy = await getDataTableProxyExecute(this, index);
|
||||
const dryRun = getDryRunParameter(this, index);
|
||||
const row = getAddRow(this, index);
|
||||
const filter = await getSelectFilter(this, index);
|
||||
|
||||
if (filter.filters.length === 0) {
|
||||
throw new NodeOperationError(this.getNode(), 'At least one condition is required');
|
||||
}
|
||||
|
||||
const result = await dataTableProxy.upsertRow({
|
||||
data: row,
|
||||
filter,
|
||||
dryRun,
|
||||
});
|
||||
|
||||
return result.map((json) => ({ json }));
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
import type { INodeProperties } from 'n8n-workflow';
|
||||
|
||||
import * as create from './create.operation';
|
||||
import * as deleteTable from './delete.operation';
|
||||
import * as list from './list.operation';
|
||||
import * as update from './update.operation';
|
||||
import { DATA_TABLE_RESOURCE_LOCATOR_BASE } from '../../common/fields';
|
||||
|
||||
export { create, deleteTable, list, update };
|
||||
|
||||
export const description: INodeProperties[] = [
|
||||
{
|
||||
displayName: 'Operation',
|
||||
name: 'operation',
|
||||
type: 'options',
|
||||
noDataExpression: true,
|
||||
displayOptions: {
|
||||
show: {
|
||||
resource: ['table'],
|
||||
},
|
||||
},
|
||||
options: [
|
||||
{
|
||||
name: 'Create',
|
||||
value: create.FIELD,
|
||||
description: 'Create a new data table',
|
||||
action: 'Create a data table',
|
||||
},
|
||||
{
|
||||
name: 'Delete',
|
||||
value: deleteTable.FIELD,
|
||||
description: 'Delete a data table',
|
||||
action: 'Delete a data table',
|
||||
},
|
||||
{
|
||||
name: 'List',
|
||||
value: list.FIELD,
|
||||
description: 'List all data tables',
|
||||
action: 'List data tables',
|
||||
},
|
||||
{
|
||||
name: 'Update',
|
||||
value: update.FIELD,
|
||||
description: 'Update a data table name',
|
||||
action: 'Update a data table',
|
||||
},
|
||||
],
|
||||
default: 'list',
|
||||
},
|
||||
{
|
||||
...DATA_TABLE_RESOURCE_LOCATOR_BASE,
|
||||
displayOptions: {
|
||||
show: {
|
||||
resource: ['table'],
|
||||
operation: [deleteTable.FIELD, update.FIELD],
|
||||
},
|
||||
},
|
||||
},
|
||||
...create.description,
|
||||
...deleteTable.description,
|
||||
...list.description,
|
||||
...update.description,
|
||||
];
|
||||
@@ -0,0 +1,131 @@
|
||||
import type {
|
||||
CreateDataTableColumnOptions,
|
||||
IDisplayOptions,
|
||||
IExecuteFunctions,
|
||||
INodeExecutionData,
|
||||
INodeProperties,
|
||||
} from 'n8n-workflow';
|
||||
|
||||
import { getDataTableAggregateProxy } from '../../common/utils';
|
||||
|
||||
export const FIELD = 'create';
|
||||
|
||||
const displayOptions: IDisplayOptions = {
|
||||
show: {
|
||||
resource: ['table'],
|
||||
operation: [FIELD],
|
||||
},
|
||||
};
|
||||
|
||||
export const description: INodeProperties[] = [
|
||||
{
|
||||
displayName: 'Name',
|
||||
name: 'tableName',
|
||||
type: 'string',
|
||||
required: true,
|
||||
default: '',
|
||||
placeholder: 'e.g. My Data Table',
|
||||
description: 'The name of the data table to create',
|
||||
displayOptions,
|
||||
},
|
||||
{
|
||||
displayName: 'Columns',
|
||||
name: 'columns',
|
||||
type: 'fixedCollection',
|
||||
typeOptions: {
|
||||
multipleValues: true,
|
||||
},
|
||||
default: {},
|
||||
placeholder: 'Add Column',
|
||||
description: 'The columns to create in the data table',
|
||||
displayOptions,
|
||||
options: [
|
||||
{
|
||||
name: 'column',
|
||||
displayName: 'Column',
|
||||
values: [
|
||||
{
|
||||
displayName: 'Name',
|
||||
name: 'name',
|
||||
type: 'string',
|
||||
default: '',
|
||||
required: true,
|
||||
description: 'The name of the column',
|
||||
},
|
||||
{
|
||||
displayName: 'Type',
|
||||
name: 'type',
|
||||
type: 'options',
|
||||
default: 'string',
|
||||
options: [
|
||||
{ name: 'Boolean', value: 'boolean' },
|
||||
{ name: 'Date', value: 'date' },
|
||||
{ name: 'Number', value: 'number' },
|
||||
{ name: 'String', value: 'string' },
|
||||
],
|
||||
description: 'The type of the column',
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
displayName: 'Options',
|
||||
name: 'options',
|
||||
type: 'collection',
|
||||
placeholder: 'Add Option',
|
||||
default: {},
|
||||
displayOptions,
|
||||
options: [
|
||||
{
|
||||
displayName: 'Reuse Existing Tables',
|
||||
name: 'createIfNotExists',
|
||||
type: 'boolean',
|
||||
default: true,
|
||||
description:
|
||||
'Whether to return existing table if one exists with the same name without throwing an error',
|
||||
},
|
||||
],
|
||||
},
|
||||
];
|
||||
|
||||
export async function execute(
|
||||
this: IExecuteFunctions,
|
||||
index: number,
|
||||
): Promise<INodeExecutionData[]> {
|
||||
const tableName = this.getNodeParameter('tableName', index) as string;
|
||||
const columnsData = this.getNodeParameter('columns.column', index, []) as Array<
|
||||
Pick<CreateDataTableColumnOptions, 'name' | 'type'>
|
||||
>;
|
||||
const options = this.getNodeParameter('options', index, {}) as {
|
||||
createIfNotExists?: boolean;
|
||||
};
|
||||
|
||||
const aggregateProxy = await getDataTableAggregateProxy(this);
|
||||
|
||||
// If "Create If Not Exists" is enabled, check if table already exists
|
||||
if (options.createIfNotExists) {
|
||||
const existingTables = await aggregateProxy.getManyAndCount({
|
||||
filter: { name: tableName },
|
||||
take: 1,
|
||||
});
|
||||
|
||||
// If a table with exact name match exists, return it
|
||||
if (existingTables.data.length > 0 && existingTables.data[0].name === tableName) {
|
||||
return [{ json: existingTables.data[0] }];
|
||||
}
|
||||
}
|
||||
|
||||
const columns: CreateDataTableColumnOptions[] = columnsData.map((col, idx) => ({
|
||||
name: col.name,
|
||||
type: col.type,
|
||||
index: idx,
|
||||
}));
|
||||
|
||||
const result = await aggregateProxy.createDataTable({
|
||||
name: tableName,
|
||||
columns,
|
||||
});
|
||||
|
||||
return [{ json: result }];
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
import type {
|
||||
IDisplayOptions,
|
||||
IExecuteFunctions,
|
||||
INodeExecutionData,
|
||||
INodeProperties,
|
||||
} from 'n8n-workflow';
|
||||
|
||||
import { DATA_TABLE_ID_FIELD } from '../../common/fields';
|
||||
import { getDataTableProxyExecute } from '../../common/utils';
|
||||
|
||||
export const FIELD = 'delete';
|
||||
|
||||
const displayOptions: IDisplayOptions = {
|
||||
show: {
|
||||
resource: ['table'],
|
||||
operation: [FIELD],
|
||||
},
|
||||
};
|
||||
|
||||
export const description: INodeProperties[] = [
|
||||
{
|
||||
displayName:
|
||||
'This will permanently delete the data table and all its data. This action cannot be undone.',
|
||||
name: 'deleteWarning',
|
||||
type: 'notice',
|
||||
default: '',
|
||||
displayOptions,
|
||||
},
|
||||
];
|
||||
|
||||
export async function execute(
|
||||
this: IExecuteFunctions,
|
||||
index: number,
|
||||
): Promise<INodeExecutionData[]> {
|
||||
const dataTableId = this.getNodeParameter(DATA_TABLE_ID_FIELD, index, undefined, {
|
||||
extractValue: true,
|
||||
}) as string;
|
||||
|
||||
const dataTableProxy = await getDataTableProxyExecute(this, index);
|
||||
|
||||
const success = await dataTableProxy.deleteDataTable();
|
||||
|
||||
return [{ json: { success, deletedTableId: dataTableId } }];
|
||||
}
|
||||
@@ -0,0 +1,146 @@
|
||||
import type {
|
||||
IDisplayOptions,
|
||||
IExecuteFunctions,
|
||||
INodeExecutionData,
|
||||
INodeProperties,
|
||||
ListDataTableOptions,
|
||||
} from 'n8n-workflow';
|
||||
|
||||
import { ROWS_LIMIT_DEFAULT } from '../../common/constants';
|
||||
import { getDataTableAggregateProxy } from '../../common/utils';
|
||||
|
||||
export const FIELD = 'list';
|
||||
|
||||
const displayOptions: IDisplayOptions = {
|
||||
show: {
|
||||
resource: ['table'],
|
||||
operation: [FIELD],
|
||||
},
|
||||
};
|
||||
|
||||
export const description: INodeProperties[] = [
|
||||
{
|
||||
displayName: 'Return All',
|
||||
name: 'returnAll',
|
||||
type: 'boolean',
|
||||
default: true,
|
||||
description: 'Whether to return all results or only up to a given limit',
|
||||
displayOptions,
|
||||
},
|
||||
{
|
||||
displayName: 'Limit',
|
||||
name: 'limit',
|
||||
type: 'number',
|
||||
default: ROWS_LIMIT_DEFAULT,
|
||||
description: 'Max number of results to return',
|
||||
typeOptions: {
|
||||
minValue: 1,
|
||||
},
|
||||
displayOptions: {
|
||||
show: {
|
||||
...displayOptions.show,
|
||||
returnAll: [false],
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
displayName: 'Options',
|
||||
name: 'options',
|
||||
type: 'collection',
|
||||
placeholder: 'Add Option',
|
||||
default: {},
|
||||
displayOptions,
|
||||
options: [
|
||||
{
|
||||
displayName: 'Filter by Name',
|
||||
name: 'filterName',
|
||||
type: 'string',
|
||||
default: '',
|
||||
description: 'Filter data tables by name (case-insensitive)',
|
||||
},
|
||||
{
|
||||
displayName: 'Sort Field',
|
||||
name: 'sortField',
|
||||
type: 'options',
|
||||
default: 'name',
|
||||
options: [
|
||||
{ name: 'Created', value: 'createdAt' },
|
||||
{ name: 'Name', value: 'name' },
|
||||
{ name: 'Updated', value: 'updatedAt' },
|
||||
],
|
||||
description: 'Field to sort by',
|
||||
},
|
||||
{
|
||||
displayName: 'Sort Direction',
|
||||
name: 'sortDirection',
|
||||
type: 'options',
|
||||
default: 'asc',
|
||||
options: [
|
||||
{ name: 'Ascending', value: 'asc' },
|
||||
{ name: 'Descending', value: 'desc' },
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
];
|
||||
|
||||
export async function execute(
|
||||
this: IExecuteFunctions,
|
||||
index: number,
|
||||
): Promise<INodeExecutionData[]> {
|
||||
const returnAll = this.getNodeParameter('returnAll', index) as boolean;
|
||||
const limit = this.getNodeParameter('limit', index, ROWS_LIMIT_DEFAULT) as number;
|
||||
const options = this.getNodeParameter('options', index, {}) as {
|
||||
filterName?: string;
|
||||
sortField?: string;
|
||||
sortDirection?: 'asc' | 'desc';
|
||||
};
|
||||
|
||||
const aggregateProxy = await getDataTableAggregateProxy(this);
|
||||
|
||||
const queryOptions: ListDataTableOptions = {};
|
||||
|
||||
if (options.sortField && options.sortDirection) {
|
||||
queryOptions.sortBy =
|
||||
`${options.sortField}:${options.sortDirection}` as ListDataTableOptions['sortBy'];
|
||||
}
|
||||
|
||||
if (options.filterName) {
|
||||
queryOptions.filter = { name: options.filterName.toLowerCase() };
|
||||
}
|
||||
|
||||
const results: INodeExecutionData[] = [];
|
||||
|
||||
if (returnAll) {
|
||||
let skip = 0;
|
||||
const take = 100;
|
||||
let hasMore = true;
|
||||
|
||||
while (hasMore) {
|
||||
const response = await aggregateProxy.getManyAndCount({
|
||||
...queryOptions,
|
||||
skip,
|
||||
take,
|
||||
});
|
||||
|
||||
for (const table of response.data) {
|
||||
results.push({ json: table });
|
||||
}
|
||||
|
||||
skip += take;
|
||||
hasMore = response.data.length === take && results.length < response.count;
|
||||
}
|
||||
} else {
|
||||
const response = await aggregateProxy.getManyAndCount({
|
||||
...queryOptions,
|
||||
skip: 0,
|
||||
take: limit,
|
||||
});
|
||||
|
||||
for (const table of response.data) {
|
||||
results.push({ json: table });
|
||||
}
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
import type {
|
||||
IDisplayOptions,
|
||||
IExecuteFunctions,
|
||||
INodeExecutionData,
|
||||
INodeProperties,
|
||||
} from 'n8n-workflow';
|
||||
|
||||
import { getDataTableProxyExecute } from '../../common/utils';
|
||||
|
||||
export const FIELD = 'update';
|
||||
|
||||
const displayOptions: IDisplayOptions = {
|
||||
show: {
|
||||
resource: ['table'],
|
||||
operation: [FIELD],
|
||||
},
|
||||
};
|
||||
|
||||
export const description: INodeProperties[] = [
|
||||
{
|
||||
displayName: 'New Name',
|
||||
name: 'newName',
|
||||
type: 'string',
|
||||
required: true,
|
||||
default: '',
|
||||
placeholder: 'e.g. Renamed Data Table',
|
||||
description: 'The new name for the data table',
|
||||
displayOptions,
|
||||
},
|
||||
];
|
||||
|
||||
export async function execute(
|
||||
this: IExecuteFunctions,
|
||||
index: number,
|
||||
): Promise<INodeExecutionData[]> {
|
||||
const newName = this.getNodeParameter('newName', index) as string;
|
||||
|
||||
const dataTableProxy = await getDataTableProxyExecute(this, index);
|
||||
|
||||
const success = await dataTableProxy.updateDataTable({ name: newName });
|
||||
|
||||
return [{ json: { success, name: newName } }];
|
||||
}
|
||||
Reference in New Issue
Block a user