2022-08-17 08:50:24 -07:00
|
|
|
import { IExecuteFunctions } from 'n8n-core';
|
2021-09-29 16:28:27 -07:00
|
|
|
|
|
|
|
import {
|
|
|
|
IDataObject,
|
|
|
|
ILoadOptionsFunctions,
|
|
|
|
INodeExecutionData,
|
|
|
|
INodePropertyOptions,
|
|
|
|
INodeType,
|
|
|
|
INodeTypeDescription,
|
|
|
|
NodeOperationError,
|
|
|
|
} from 'n8n-workflow';
|
|
|
|
|
|
|
|
import {
|
|
|
|
getTableColumns,
|
|
|
|
getTableViews,
|
|
|
|
rowExport,
|
|
|
|
rowFormatColumns,
|
|
|
|
rowMapKeyToName,
|
|
|
|
seaTableApiRequest,
|
|
|
|
setableApiRequestAllItems,
|
|
|
|
split,
|
|
|
|
updateAble,
|
|
|
|
} from './GenericFunctions';
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { rowFields, rowOperations } from './RowDescription';
|
2021-09-29 16:28:27 -07:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { TColumnsUiValues, TColumnValue } from './types';
|
2021-09-29 16:28:27 -07:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { ICtx, IRow, IRowObject } from './Interfaces';
|
2021-09-29 16:28:27 -07:00
|
|
|
|
|
|
|
export class SeaTable implements INodeType {
|
|
|
|
description: INodeTypeDescription = {
|
|
|
|
displayName: 'SeaTable',
|
|
|
|
name: 'seaTable',
|
|
|
|
icon: 'file:seaTable.svg',
|
|
|
|
group: ['input'],
|
|
|
|
version: 1,
|
|
|
|
subtitle: '={{$parameter["resource"] + ": " + $parameter["operation"]}}',
|
|
|
|
description: 'Consume the SeaTable API',
|
|
|
|
defaults: {
|
|
|
|
name: 'SeaTable',
|
|
|
|
},
|
|
|
|
inputs: ['main'],
|
|
|
|
outputs: ['main'],
|
|
|
|
credentials: [
|
|
|
|
{
|
|
|
|
name: 'seaTableApi',
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
properties: [
|
|
|
|
{
|
|
|
|
displayName: 'Resource',
|
|
|
|
name: 'resource',
|
|
|
|
type: 'options',
|
2022-05-20 14:47:24 -07:00
|
|
|
noDataExpression: true,
|
2021-09-29 16:28:27 -07:00
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'Row',
|
|
|
|
value: 'row',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
default: 'row',
|
|
|
|
},
|
|
|
|
...rowOperations,
|
|
|
|
...rowFields,
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
methods = {
|
|
|
|
loadOptions: {
|
|
|
|
async getTableNames(this: ILoadOptionsFunctions) {
|
|
|
|
const returnData: INodePropertyOptions[] = [];
|
2022-08-17 08:50:24 -07:00
|
|
|
const {
|
|
|
|
metadata: { tables },
|
|
|
|
} = await seaTableApiRequest.call(
|
|
|
|
this,
|
|
|
|
{},
|
|
|
|
'GET',
|
|
|
|
`/dtable-server/api/v1/dtables/{{dtable_uuid}}/metadata`,
|
|
|
|
);
|
2021-09-29 16:28:27 -07:00
|
|
|
for (const table of tables) {
|
|
|
|
returnData.push({
|
|
|
|
name: table.name,
|
|
|
|
value: table.name,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return returnData;
|
|
|
|
},
|
|
|
|
async getTableIds(this: ILoadOptionsFunctions) {
|
|
|
|
const returnData: INodePropertyOptions[] = [];
|
2022-08-17 08:50:24 -07:00
|
|
|
const {
|
|
|
|
metadata: { tables },
|
|
|
|
} = await seaTableApiRequest.call(
|
|
|
|
this,
|
|
|
|
{},
|
|
|
|
'GET',
|
|
|
|
`/dtable-server/api/v1/dtables/{{dtable_uuid}}/metadata`,
|
|
|
|
);
|
2021-09-29 16:28:27 -07:00
|
|
|
for (const table of tables) {
|
|
|
|
returnData.push({
|
|
|
|
name: table.name,
|
|
|
|
value: table._id,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return returnData;
|
|
|
|
},
|
|
|
|
|
|
|
|
async getTableUpdateAbleColumns(this: ILoadOptionsFunctions) {
|
|
|
|
const tableName = this.getNodeParameter('tableName') as string;
|
2022-08-17 08:50:24 -07:00
|
|
|
const columns = await getTableColumns.call(this, tableName);
|
|
|
|
return columns
|
|
|
|
.filter((column) => column.editable)
|
|
|
|
.map((column) => ({ name: column.name, value: column.name }));
|
2021-09-29 16:28:27 -07:00
|
|
|
},
|
|
|
|
async getAllSortableColumns(this: ILoadOptionsFunctions) {
|
|
|
|
const tableName = this.getNodeParameter('tableName') as string;
|
|
|
|
const columns = await getTableColumns.call(this, tableName);
|
2022-08-17 08:50:24 -07:00
|
|
|
return columns
|
|
|
|
.filter(
|
|
|
|
(column) =>
|
|
|
|
!['file', 'image', 'url', 'collaborator', 'long-text'].includes(column.type),
|
|
|
|
)
|
|
|
|
.map((column) => ({ name: column.name, value: column.name }));
|
2021-09-29 16:28:27 -07:00
|
|
|
},
|
|
|
|
async getViews(this: ILoadOptionsFunctions) {
|
|
|
|
const tableName = this.getNodeParameter('tableName') as string;
|
|
|
|
const views = await getTableViews.call(this, tableName);
|
2022-08-17 08:50:24 -07:00
|
|
|
return views.map((view) => ({ name: view.name, value: view.name }));
|
2021-09-29 16:28:27 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
|
|
|
const items = this.getInputData();
|
2022-08-30 08:55:33 -07:00
|
|
|
const returnData: INodeExecutionData[] = [];
|
2021-09-29 16:28:27 -07:00
|
|
|
let responseData;
|
|
|
|
|
|
|
|
const resource = this.getNodeParameter('resource', 0) as string;
|
|
|
|
const operation = this.getNodeParameter('operation', 0) as string;
|
|
|
|
|
|
|
|
const body: IDataObject = {};
|
|
|
|
const qs: IDataObject = {};
|
|
|
|
const ctx: ICtx = {};
|
|
|
|
|
|
|
|
if (resource === 'row') {
|
|
|
|
if (operation === 'create') {
|
|
|
|
// ----------------------------------
|
|
|
|
// row:create
|
|
|
|
// ----------------------------------
|
|
|
|
|
|
|
|
const tableName = this.getNodeParameter('tableName', 0) as string;
|
|
|
|
const tableColumns = await getTableColumns.call(this, tableName);
|
|
|
|
|
|
|
|
body.table_name = tableName;
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
const fieldsToSend = this.getNodeParameter('fieldsToSend', 0) as
|
|
|
|
| 'defineBelow'
|
|
|
|
| 'autoMapInputData';
|
2021-09-29 16:28:27 -07:00
|
|
|
let rowInput: IRowObject = {};
|
|
|
|
|
|
|
|
for (let i = 0; i < items.length; i++) {
|
|
|
|
rowInput = {} as IRowObject;
|
|
|
|
try {
|
|
|
|
if (fieldsToSend === 'autoMapInputData') {
|
|
|
|
const incomingKeys = Object.keys(items[i].json);
|
2022-08-17 08:50:24 -07:00
|
|
|
const inputDataToIgnore = split(
|
|
|
|
this.getNodeParameter('inputsToIgnore', i, '') as string,
|
|
|
|
);
|
2021-09-29 16:28:27 -07:00
|
|
|
for (const key of incomingKeys) {
|
|
|
|
if (inputDataToIgnore.includes(key)) continue;
|
|
|
|
rowInput[key] = items[i].json[key] as TColumnValue;
|
|
|
|
}
|
|
|
|
} else {
|
2022-08-17 08:50:24 -07:00
|
|
|
const columns = this.getNodeParameter(
|
|
|
|
'columnsUi.columnValues',
|
|
|
|
i,
|
|
|
|
[],
|
|
|
|
) as TColumnsUiValues;
|
2021-09-29 16:28:27 -07:00
|
|
|
for (const column of columns) {
|
|
|
|
rowInput[column.columnName] = column.columnValue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
body.row = rowExport(rowInput, updateAble(tableColumns));
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await seaTableApiRequest.call(
|
|
|
|
this,
|
|
|
|
ctx,
|
|
|
|
'POST',
|
|
|
|
`/dtable-server/api/v1/dtables/{{dtable_uuid}}/rows/`,
|
|
|
|
body,
|
|
|
|
);
|
2021-09-29 16:28:27 -07:00
|
|
|
|
|
|
|
const { _id: insertId } = responseData;
|
|
|
|
if (insertId === undefined) {
|
2022-08-17 08:50:24 -07:00
|
|
|
throw new NodeOperationError(
|
|
|
|
this.getNode(),
|
|
|
|
'SeaTable: No identity after appending row.',
|
|
|
|
{ itemIndex: i },
|
|
|
|
);
|
2021-09-29 16:28:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
const newRowInsertData = rowMapKeyToName(responseData, tableColumns);
|
|
|
|
|
|
|
|
qs.table_name = tableName;
|
|
|
|
qs.convert = true;
|
2022-08-17 08:50:24 -07:00
|
|
|
const newRow = await seaTableApiRequest.call(
|
|
|
|
this,
|
|
|
|
ctx,
|
|
|
|
'GET',
|
|
|
|
`/dtable-server/api/v1/dtables/{{dtable_uuid}}/rows/${encodeURIComponent(insertId)}/`,
|
|
|
|
body,
|
|
|
|
qs,
|
|
|
|
);
|
2021-09-29 16:28:27 -07:00
|
|
|
|
|
|
|
if (newRow._id === undefined) {
|
2022-08-17 08:50:24 -07:00
|
|
|
throw new NodeOperationError(
|
|
|
|
this.getNode(),
|
|
|
|
'SeaTable: No identity for appended row.',
|
|
|
|
{ itemIndex: i },
|
|
|
|
);
|
2021-09-29 16:28:27 -07:00
|
|
|
}
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
const row = rowFormatColumns(
|
|
|
|
{ ...newRowInsertData, ...newRow },
|
|
|
|
tableColumns.map(({ name }) => name).concat(['_id', '_ctime', '_mtime']),
|
|
|
|
);
|
2021-09-29 16:28:27 -07:00
|
|
|
|
2022-08-30 08:55:33 -07:00
|
|
|
const executionData = this.helpers.constructExecutionMetaData(
|
|
|
|
this.helpers.returnJsonArray(row),
|
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
|
|
|
|
returnData.push(...executionData);
|
2021-09-29 16:28:27 -07:00
|
|
|
} catch (error) {
|
|
|
|
if (this.continueOnFail()) {
|
2022-08-30 08:55:33 -07:00
|
|
|
const executionErrorData = this.helpers.constructExecutionMetaData(
|
|
|
|
this.helpers.returnJsonArray({ error: error.message }),
|
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
returnData.push(...executionErrorData);
|
2021-09-29 16:28:27 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (operation === 'get') {
|
|
|
|
for (let i = 0; i < items.length; i++) {
|
|
|
|
try {
|
|
|
|
const tableId = this.getNodeParameter('tableId', 0) as string;
|
|
|
|
const rowId = this.getNodeParameter('rowId', i) as string;
|
2022-08-17 08:50:24 -07:00
|
|
|
const response = (await seaTableApiRequest.call(
|
|
|
|
this,
|
|
|
|
ctx,
|
|
|
|
'GET',
|
|
|
|
`/dtable-server/api/v1/dtables/{{dtable_uuid}}/rows/${rowId}`,
|
|
|
|
{},
|
|
|
|
{ table_id: tableId, convert: true },
|
|
|
|
)) as IDataObject;
|
2022-08-30 08:55:33 -07:00
|
|
|
|
|
|
|
const executionData = this.helpers.constructExecutionMetaData(
|
|
|
|
this.helpers.returnJsonArray(response),
|
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
|
|
|
|
returnData.push(...executionData);
|
2021-09-29 16:28:27 -07:00
|
|
|
} catch (error) {
|
|
|
|
if (this.continueOnFail()) {
|
2022-08-30 08:55:33 -07:00
|
|
|
const executionErrorData = this.helpers.constructExecutionMetaData(
|
|
|
|
this.helpers.returnJsonArray({ error: error.message }),
|
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
returnData.push(...executionErrorData);
|
2021-09-29 16:28:27 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (operation === 'getAll') {
|
|
|
|
// ----------------------------------
|
|
|
|
// row:getAll
|
|
|
|
// ----------------------------------
|
|
|
|
|
|
|
|
const tableName = this.getNodeParameter('tableName', 0) as string;
|
|
|
|
const tableColumns = await getTableColumns.call(this, tableName);
|
|
|
|
|
2022-08-30 08:55:33 -07:00
|
|
|
for (let i = 0; i < items.length; i++) {
|
|
|
|
try {
|
2021-09-29 16:28:27 -07:00
|
|
|
const endpoint = `/dtable-server/api/v1/dtables/{{dtable_uuid}}/rows/`;
|
|
|
|
qs.table_name = tableName;
|
|
|
|
const filters = this.getNodeParameter('filters', i) as IDataObject;
|
|
|
|
const options = this.getNodeParameter('options', i) as IDataObject;
|
|
|
|
const returnAll = this.getNodeParameter('returnAll', 0) as boolean;
|
|
|
|
|
|
|
|
Object.assign(qs, filters, options);
|
|
|
|
|
|
|
|
if (returnAll) {
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await setableApiRequestAllItems.call(
|
|
|
|
this,
|
|
|
|
ctx,
|
|
|
|
'rows',
|
|
|
|
'GET',
|
|
|
|
endpoint,
|
|
|
|
body,
|
|
|
|
qs,
|
|
|
|
);
|
2021-09-29 16:28:27 -07:00
|
|
|
} else {
|
|
|
|
qs.limit = this.getNodeParameter('limit', 0) as number;
|
|
|
|
responseData = await seaTableApiRequest.call(this, ctx, 'GET', endpoint, body, qs);
|
|
|
|
responseData = responseData.rows;
|
|
|
|
}
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
const rows = responseData.map((row: IRow) =>
|
|
|
|
rowFormatColumns(
|
|
|
|
{ ...row },
|
|
|
|
tableColumns.map(({ name }) => name).concat(['_id', '_ctime', '_mtime']),
|
|
|
|
),
|
|
|
|
);
|
2021-09-29 16:28:27 -07:00
|
|
|
|
2022-08-30 08:55:33 -07:00
|
|
|
const executionData = this.helpers.constructExecutionMetaData(
|
|
|
|
this.helpers.returnJsonArray(rows),
|
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
|
|
|
|
returnData.push(...executionData);
|
|
|
|
} catch (error) {
|
|
|
|
if (this.continueOnFail()) {
|
|
|
|
const executionErrorData = this.helpers.constructExecutionMetaData(
|
|
|
|
this.helpers.returnJsonArray({ error: error.message }),
|
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
returnData.push(...executionErrorData);
|
|
|
|
}
|
|
|
|
throw error;
|
2021-09-29 16:28:27 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (operation === 'delete') {
|
|
|
|
for (let i = 0; i < items.length; i++) {
|
|
|
|
try {
|
|
|
|
const tableName = this.getNodeParameter('tableName', 0) as string;
|
|
|
|
const rowId = this.getNodeParameter('rowId', i) as string;
|
|
|
|
const body: IDataObject = {
|
|
|
|
table_name: tableName,
|
|
|
|
row_id: rowId,
|
|
|
|
};
|
2022-08-17 08:50:24 -07:00
|
|
|
const response = (await seaTableApiRequest.call(
|
|
|
|
this,
|
|
|
|
ctx,
|
|
|
|
'DELETE',
|
|
|
|
`/dtable-server/api/v1/dtables/{{dtable_uuid}}/rows/`,
|
|
|
|
body,
|
|
|
|
qs,
|
|
|
|
)) as IDataObject;
|
2022-08-30 08:55:33 -07:00
|
|
|
|
|
|
|
const executionData = this.helpers.constructExecutionMetaData(
|
|
|
|
this.helpers.returnJsonArray(response),
|
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
|
|
|
|
returnData.push(...executionData);
|
2021-09-29 16:28:27 -07:00
|
|
|
} catch (error) {
|
|
|
|
if (this.continueOnFail()) {
|
2022-08-30 08:55:33 -07:00
|
|
|
const executionErrorData = this.helpers.constructExecutionMetaData(
|
|
|
|
this.helpers.returnJsonArray({ error: error.message }),
|
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
returnData.push(...executionErrorData);
|
2021-09-29 16:28:27 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (operation === 'update') {
|
|
|
|
// ----------------------------------
|
|
|
|
// row:update
|
|
|
|
// ----------------------------------
|
|
|
|
|
|
|
|
const tableName = this.getNodeParameter('tableName', 0) as string;
|
|
|
|
const tableColumns = await getTableColumns.call(this, tableName);
|
|
|
|
|
|
|
|
body.table_name = tableName;
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
const fieldsToSend = this.getNodeParameter('fieldsToSend', 0) as
|
|
|
|
| 'defineBelow'
|
|
|
|
| 'autoMapInputData';
|
2021-09-29 16:28:27 -07:00
|
|
|
let rowInput: IRowObject = {};
|
|
|
|
|
|
|
|
for (let i = 0; i < items.length; i++) {
|
|
|
|
const rowId = this.getNodeParameter('rowId', i) as string;
|
|
|
|
rowInput = {} as IRowObject;
|
|
|
|
try {
|
|
|
|
if (fieldsToSend === 'autoMapInputData') {
|
|
|
|
const incomingKeys = Object.keys(items[i].json);
|
2022-08-17 08:50:24 -07:00
|
|
|
const inputDataToIgnore = split(
|
|
|
|
this.getNodeParameter('inputsToIgnore', i, '') as string,
|
|
|
|
);
|
2021-09-29 16:28:27 -07:00
|
|
|
for (const key of incomingKeys) {
|
|
|
|
if (inputDataToIgnore.includes(key)) continue;
|
|
|
|
rowInput[key] = items[i].json[key] as TColumnValue;
|
|
|
|
}
|
|
|
|
} else {
|
2022-08-17 08:50:24 -07:00
|
|
|
const columns = this.getNodeParameter(
|
|
|
|
'columnsUi.columnValues',
|
|
|
|
i,
|
|
|
|
[],
|
|
|
|
) as TColumnsUiValues;
|
2021-09-29 16:28:27 -07:00
|
|
|
for (const column of columns) {
|
|
|
|
rowInput[column.columnName] = column.columnValue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
body.row = rowExport(rowInput, updateAble(tableColumns));
|
|
|
|
body.table_name = tableName;
|
|
|
|
body.row_id = rowId;
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await seaTableApiRequest.call(
|
|
|
|
this,
|
|
|
|
ctx,
|
|
|
|
'PUT',
|
|
|
|
`/dtable-server/api/v1/dtables/{{dtable_uuid}}/rows/`,
|
|
|
|
body,
|
|
|
|
);
|
|
|
|
|
2022-08-30 08:55:33 -07:00
|
|
|
const executionData = this.helpers.constructExecutionMetaData(
|
|
|
|
this.helpers.returnJsonArray({ _id: rowId, ...responseData }),
|
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
|
|
|
|
returnData.push(...executionData);
|
2021-09-29 16:28:27 -07:00
|
|
|
} catch (error) {
|
|
|
|
if (this.continueOnFail()) {
|
2022-08-30 08:55:33 -07:00
|
|
|
const executionErrorData = this.helpers.constructExecutionMetaData(
|
|
|
|
this.helpers.returnJsonArray({ error: error.message }),
|
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
returnData.push(...executionErrorData);
|
2021-09-29 16:28:27 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
throw new NodeOperationError(this.getNode(), `The operation "${operation}" is not known!`);
|
|
|
|
}
|
|
|
|
}
|
2022-08-30 08:55:33 -07:00
|
|
|
return this.prepareOutputData(returnData);
|
2021-09-29 16:28:27 -07:00
|
|
|
}
|
|
|
|
}
|