mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-22 04:10:01 +00:00
feat(Data Table Node): Add Data Table Node (no-changelog) (#18556)
This commit is contained in:
12
packages/nodes-base/nodes/DataTable/common/constants.ts
Normal file
12
packages/nodes-base/nodes/DataTable/common/constants.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import type { DataStoreColumnJsType } from 'n8n-workflow';
|
||||
|
||||
export const ANY_FILTER = 'anyFilter';
|
||||
export const ALL_FILTERS = 'allFilters';
|
||||
|
||||
export type FilterType = typeof ANY_FILTER | typeof ALL_FILTERS;
|
||||
|
||||
export type FieldEntry = {
|
||||
keyName: string;
|
||||
condition: 'eq' | 'neq';
|
||||
keyValue: DataStoreColumnJsType;
|
||||
};
|
||||
28
packages/nodes-base/nodes/DataTable/common/fields.ts
Normal file
28
packages/nodes-base/nodes/DataTable/common/fields.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import type { INodeProperties } from 'n8n-workflow';
|
||||
|
||||
export const DATA_TABLE_ID_FIELD = 'dataTableId';
|
||||
|
||||
export const COLUMNS: INodeProperties = {
|
||||
displayName: 'Columns',
|
||||
name: 'columns',
|
||||
type: 'resourceMapper',
|
||||
default: {
|
||||
mappingMode: 'defineBelow',
|
||||
value: null,
|
||||
},
|
||||
noDataExpression: true,
|
||||
required: true,
|
||||
typeOptions: {
|
||||
loadOptionsDependsOn: [`${DATA_TABLE_ID_FIELD}.value`],
|
||||
resourceMapper: {
|
||||
resourceMapperMethod: 'getDataTables',
|
||||
mode: 'add',
|
||||
fieldWords: {
|
||||
singular: 'column',
|
||||
plural: 'columns',
|
||||
},
|
||||
addAllFields: true,
|
||||
multiKeyMatch: true,
|
||||
},
|
||||
},
|
||||
};
|
||||
79
packages/nodes-base/nodes/DataTable/common/methods.ts
Normal file
79
packages/nodes-base/nodes/DataTable/common/methods.ts
Normal file
@@ -0,0 +1,79 @@
|
||||
import type {
|
||||
ILoadOptionsFunctions,
|
||||
INodeListSearchResult,
|
||||
INodePropertyOptions,
|
||||
ResourceMapperField,
|
||||
ResourceMapperFields,
|
||||
} from 'n8n-workflow';
|
||||
|
||||
import { getDataTableAggregateProxy, getDataTableProxyLoadOptions } from './utils';
|
||||
|
||||
// @ADO-3904: Pagination here does not work until a filter is entered or removed, suspected bug in ResourceLocator
|
||||
export async function tableSearch(
|
||||
this: ILoadOptionsFunctions,
|
||||
filterString?: string,
|
||||
prevPaginationToken?: string,
|
||||
): Promise<INodeListSearchResult> {
|
||||
const proxy = await getDataTableAggregateProxy(this);
|
||||
|
||||
const skip = prevPaginationToken === undefined ? 0 : parseInt(prevPaginationToken, 10);
|
||||
const take = 100;
|
||||
const filter = filterString === undefined ? {} : { filter: { name: filterString.toLowerCase() } };
|
||||
const result = await proxy.getManyAndCount({
|
||||
skip,
|
||||
take,
|
||||
...filter,
|
||||
});
|
||||
|
||||
const results = result.data.map((row) => {
|
||||
return {
|
||||
name: row.name,
|
||||
value: row.id,
|
||||
};
|
||||
});
|
||||
|
||||
const paginationToken = results.length === take ? `${skip + take}` : undefined;
|
||||
|
||||
return {
|
||||
results,
|
||||
paginationToken,
|
||||
};
|
||||
}
|
||||
|
||||
export async function getDataTableColumns(this: ILoadOptionsFunctions) {
|
||||
// eslint-disable-next-line n8n-nodes-base/node-param-display-name-miscased-id, n8n-nodes-base/node-param-display-name-miscased
|
||||
const returnData: INodePropertyOptions[] = [{ name: 'id - (string)', value: 'id' }];
|
||||
const proxy = await getDataTableProxyLoadOptions(this);
|
||||
const columns = await proxy.getColumns();
|
||||
for (const column of columns) {
|
||||
returnData.push({
|
||||
name: `${column.name} - (${column.type})`,
|
||||
value: column.name,
|
||||
});
|
||||
}
|
||||
return returnData;
|
||||
}
|
||||
|
||||
export async function getDataTables(this: ILoadOptionsFunctions): Promise<ResourceMapperFields> {
|
||||
const proxy = await getDataTableProxyLoadOptions(this);
|
||||
const result = await proxy.getColumns();
|
||||
|
||||
const fields: ResourceMapperField[] = [];
|
||||
|
||||
for (const field of result) {
|
||||
const type = field.type === 'date' ? 'dateTime' : field.type;
|
||||
|
||||
fields.push({
|
||||
id: field.name,
|
||||
displayName: field.name,
|
||||
required: false,
|
||||
defaultMatch: false,
|
||||
display: true,
|
||||
type,
|
||||
readOnly: false,
|
||||
removed: false,
|
||||
});
|
||||
}
|
||||
|
||||
return { fields };
|
||||
}
|
||||
100
packages/nodes-base/nodes/DataTable/common/selectMany.ts
Normal file
100
packages/nodes-base/nodes/DataTable/common/selectMany.ts
Normal file
@@ -0,0 +1,100 @@
|
||||
import {
|
||||
NodeOperationError,
|
||||
type IDisplayOptions,
|
||||
type IExecuteFunctions,
|
||||
type INodeProperties,
|
||||
} from 'n8n-workflow';
|
||||
|
||||
import type { FilterType } from './constants';
|
||||
import { DATA_TABLE_ID_FIELD } from './fields';
|
||||
import { buildGetManyFilter, isFieldArray, isMatchType } from './utils';
|
||||
|
||||
export function getSelectFields(displayOptions: IDisplayOptions): INodeProperties[] {
|
||||
return [
|
||||
{
|
||||
displayName: 'Must Match',
|
||||
name: 'matchType',
|
||||
type: 'options',
|
||||
options: [
|
||||
{
|
||||
name: 'Any Filter',
|
||||
value: 'anyFilter',
|
||||
},
|
||||
{
|
||||
name: 'All Filters',
|
||||
value: 'allFilters',
|
||||
},
|
||||
] satisfies Array<{ value: FilterType; name: string }>,
|
||||
displayOptions,
|
||||
default: 'anyFilter',
|
||||
},
|
||||
{
|
||||
displayName: 'Conditions',
|
||||
name: 'filters',
|
||||
type: 'fixedCollection',
|
||||
typeOptions: {
|
||||
multipleValues: true,
|
||||
},
|
||||
displayOptions,
|
||||
default: {},
|
||||
placeholder: 'Add Condition',
|
||||
options: [
|
||||
{
|
||||
displayName: 'Conditions',
|
||||
name: 'conditions',
|
||||
values: [
|
||||
{
|
||||
displayName: 'Field Name or ID',
|
||||
name: 'keyName',
|
||||
type: 'options',
|
||||
description:
|
||||
'Choose from the list, or specify an ID using an <a href="https://docs.n8n.io/code/expressions/">expression</a>',
|
||||
typeOptions: {
|
||||
loadOptionsDependsOn: [DATA_TABLE_ID_FIELD],
|
||||
loadOptionsMethod: 'getDataTableColumns',
|
||||
},
|
||||
default: 'id',
|
||||
},
|
||||
{
|
||||
displayName: 'Condition',
|
||||
name: 'condition',
|
||||
type: 'options',
|
||||
options: [
|
||||
{
|
||||
name: 'Equals',
|
||||
value: 'eq',
|
||||
},
|
||||
{
|
||||
name: 'Not Equals',
|
||||
value: 'neq',
|
||||
},
|
||||
],
|
||||
default: 'eq',
|
||||
},
|
||||
{
|
||||
displayName: 'Field Value',
|
||||
name: 'keyValue',
|
||||
type: 'string',
|
||||
default: '',
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
description: 'Filter to decide which rows get',
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
export function getSelectFilter(ctx: IExecuteFunctions, index: number) {
|
||||
const fields = ctx.getNodeParameter('filters.conditions', index, []);
|
||||
const matchType = ctx.getNodeParameter('matchType', index, []);
|
||||
|
||||
if (!isMatchType(matchType)) {
|
||||
throw new NodeOperationError(ctx.getNode(), 'unexpected match type');
|
||||
}
|
||||
if (!isFieldArray(fields)) {
|
||||
throw new NodeOperationError(ctx.getNode(), 'unexpected fields input');
|
||||
}
|
||||
|
||||
return buildGetManyFilter(fields, matchType);
|
||||
}
|
||||
112
packages/nodes-base/nodes/DataTable/common/utils.ts
Normal file
112
packages/nodes-base/nodes/DataTable/common/utils.ts
Normal file
@@ -0,0 +1,112 @@
|
||||
import type {
|
||||
IDataObject,
|
||||
INode,
|
||||
ListDataStoreContentFilter,
|
||||
IDataStoreProjectAggregateService,
|
||||
IDataStoreProjectService,
|
||||
IExecuteFunctions,
|
||||
ILoadOptionsFunctions,
|
||||
} from 'n8n-workflow';
|
||||
import { NodeOperationError } from 'n8n-workflow';
|
||||
|
||||
import type { FieldEntry, FilterType } from './constants';
|
||||
import { ALL_FILTERS, ANY_FILTER } from './constants';
|
||||
import { DATA_TABLE_ID_FIELD } from './fields';
|
||||
|
||||
// We need two functions here since the available getNodeParameter
|
||||
// overloads vary with the index
|
||||
export async function getDataTableProxyExecute(
|
||||
ctx: IExecuteFunctions,
|
||||
index: number = 0,
|
||||
): Promise<IDataStoreProjectService> {
|
||||
if (ctx.helpers.getDataStoreProxy === undefined)
|
||||
throw new NodeOperationError(
|
||||
ctx.getNode(),
|
||||
'Attempted to use Data Table node but the module is disabled',
|
||||
);
|
||||
|
||||
const dataStoreId = ctx.getNodeParameter(DATA_TABLE_ID_FIELD, index, undefined, {
|
||||
extractValue: true,
|
||||
}) as string;
|
||||
|
||||
return await ctx.helpers.getDataStoreProxy(dataStoreId);
|
||||
}
|
||||
|
||||
export async function getDataTableProxyLoadOptions(
|
||||
ctx: ILoadOptionsFunctions,
|
||||
): Promise<IDataStoreProjectService> {
|
||||
if (ctx.helpers.getDataStoreProxy === undefined)
|
||||
throw new NodeOperationError(
|
||||
ctx.getNode(),
|
||||
'Attempted to use Data Table node but the module is disabled',
|
||||
);
|
||||
|
||||
const dataStoreId = ctx.getNodeParameter(DATA_TABLE_ID_FIELD, undefined, {
|
||||
extractValue: true,
|
||||
}) as string;
|
||||
|
||||
return await ctx.helpers.getDataStoreProxy(dataStoreId);
|
||||
}
|
||||
|
||||
export async function getDataTableAggregateProxy(
|
||||
ctx: IExecuteFunctions | ILoadOptionsFunctions,
|
||||
): Promise<IDataStoreProjectAggregateService> {
|
||||
if (ctx.helpers.getDataStoreAggregateProxy === undefined)
|
||||
throw new NodeOperationError(
|
||||
ctx.getNode(),
|
||||
'Attempted to use Data Table node but the module is disabled',
|
||||
);
|
||||
|
||||
return await ctx.helpers.getDataStoreAggregateProxy();
|
||||
}
|
||||
|
||||
export function isFieldEntry(obj: unknown): obj is FieldEntry {
|
||||
if (obj === null || typeof obj !== 'object') return false;
|
||||
return 'keyName' in obj && 'condition' in obj && 'keyValue' in obj;
|
||||
}
|
||||
|
||||
export function isMatchType(obj: unknown): obj is FilterType {
|
||||
return typeof obj === 'string' && (obj === ANY_FILTER || obj === ALL_FILTERS);
|
||||
}
|
||||
|
||||
export function buildGetManyFilter(
|
||||
fieldEntries: FieldEntry[],
|
||||
matchType: FilterType,
|
||||
): ListDataStoreContentFilter {
|
||||
const filters = fieldEntries.map((x) => ({
|
||||
columnName: x.keyName,
|
||||
condition: x.condition,
|
||||
value: x.keyValue,
|
||||
}));
|
||||
return { type: matchType === 'allFilters' ? 'and' : 'or', filters };
|
||||
}
|
||||
|
||||
export function isFieldArray(value: unknown): value is FieldEntry[] {
|
||||
return (
|
||||
value !== null && typeof value === 'object' && Array.isArray(value) && value.every(isFieldEntry)
|
||||
);
|
||||
}
|
||||
|
||||
export function dataObjectToApiInput(data: IDataObject, node: INode, row: number) {
|
||||
return Object.fromEntries(
|
||||
Object.entries(data).map(([k, v]) => {
|
||||
if (v === undefined || v === null) return [k, null];
|
||||
|
||||
if (Array.isArray(v)) {
|
||||
throw new NodeOperationError(
|
||||
node,
|
||||
`unexpected array input '${JSON.stringify(v)}' in row ${row}`,
|
||||
);
|
||||
}
|
||||
|
||||
if (!(v instanceof Date) && typeof v === 'object') {
|
||||
throw new NodeOperationError(
|
||||
node,
|
||||
`unexpected object input '${JSON.stringify(v)}' in row ${row}`,
|
||||
);
|
||||
}
|
||||
|
||||
return [k, v];
|
||||
}),
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user