2021-07-12 04:26:21 -07:00
|
|
|
import {
|
|
|
|
IDataObject,
|
|
|
|
ILoadOptionsFunctions,
|
|
|
|
INodeExecutionData,
|
|
|
|
INodeType,
|
|
|
|
INodeTypeDescription,
|
|
|
|
} from 'n8n-workflow';
|
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
import { IExecuteFunctions } from 'n8n-core';
|
2021-07-12 04:26:21 -07:00
|
|
|
|
|
|
|
import {
|
|
|
|
baserowApiRequest,
|
|
|
|
baserowApiRequestAllItems,
|
|
|
|
getJwtToken,
|
|
|
|
TableFieldMapper,
|
|
|
|
toOptions,
|
|
|
|
} from './GenericFunctions';
|
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
import { operationFields } from './OperationDescription';
|
2021-07-12 04:26:21 -07:00
|
|
|
|
|
|
|
import {
|
|
|
|
BaserowCredentials,
|
|
|
|
FieldsUiValues,
|
|
|
|
GetAllAdditionalOptions,
|
|
|
|
LoadedResource,
|
|
|
|
Operation,
|
|
|
|
Row,
|
|
|
|
} from './types';
|
|
|
|
|
|
|
|
export class Baserow implements INodeType {
|
|
|
|
description: INodeTypeDescription = {
|
|
|
|
displayName: 'Baserow',
|
|
|
|
name: 'baserow',
|
|
|
|
icon: 'file:baserow.svg',
|
|
|
|
group: ['output'],
|
|
|
|
version: 1,
|
|
|
|
description: 'Consume the Baserow API',
|
|
|
|
subtitle: '={{$parameter["operation"] + ":" + $parameter["resource"]}}',
|
|
|
|
defaults: {
|
|
|
|
name: 'Baserow',
|
|
|
|
},
|
|
|
|
inputs: ['main'],
|
|
|
|
outputs: ['main'],
|
|
|
|
credentials: [
|
|
|
|
{
|
|
|
|
name: 'baserowApi',
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
properties: [
|
|
|
|
{
|
|
|
|
displayName: 'Resource',
|
|
|
|
name: 'resource',
|
|
|
|
type: 'options',
|
2022-05-20 14:47:24 -07:00
|
|
|
noDataExpression: true,
|
2021-07-12 04:26:21 -07:00
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'Row',
|
|
|
|
value: 'row',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
default: 'row',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Operation',
|
|
|
|
name: 'operation',
|
|
|
|
type: 'options',
|
2022-05-20 14:47:24 -07:00
|
|
|
noDataExpression: true,
|
2021-07-12 04:26:21 -07:00
|
|
|
displayOptions: {
|
|
|
|
show: {
|
2022-08-01 13:47:55 -07:00
|
|
|
resource: ['row'],
|
2021-07-12 04:26:21 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'Create',
|
|
|
|
value: 'create',
|
|
|
|
description: 'Create a row',
|
2022-07-10 13:50:51 -07:00
|
|
|
action: 'Create a row',
|
2021-07-12 04:26:21 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Delete',
|
|
|
|
value: 'delete',
|
|
|
|
description: 'Delete a row',
|
2022-07-10 13:50:51 -07:00
|
|
|
action: 'Delete a row',
|
2021-07-12 04:26:21 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Get',
|
|
|
|
value: 'get',
|
|
|
|
description: 'Retrieve a row',
|
2022-07-10 13:50:51 -07:00
|
|
|
action: 'Get a row',
|
2021-07-12 04:26:21 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Get All',
|
|
|
|
value: 'getAll',
|
|
|
|
description: 'Retrieve all rows',
|
2022-07-10 13:50:51 -07:00
|
|
|
action: 'Get all rows',
|
2021-07-12 04:26:21 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Update',
|
|
|
|
value: 'update',
|
|
|
|
description: 'Update a row',
|
2022-07-10 13:50:51 -07:00
|
|
|
action: 'Update a row',
|
2021-07-12 04:26:21 -07:00
|
|
|
},
|
|
|
|
],
|
|
|
|
default: 'getAll',
|
|
|
|
},
|
|
|
|
...operationFields,
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
methods = {
|
|
|
|
loadOptions: {
|
|
|
|
async getDatabaseIds(this: ILoadOptionsFunctions) {
|
2022-08-01 13:47:55 -07:00
|
|
|
const credentials = (await this.getCredentials('baserowApi')) as BaserowCredentials;
|
2021-07-12 04:26:21 -07:00
|
|
|
const jwtToken = await getJwtToken.call(this, credentials);
|
|
|
|
const endpoint = '/api/applications/';
|
2022-08-01 13:47:55 -07:00
|
|
|
const databases = (await baserowApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
endpoint,
|
|
|
|
{},
|
|
|
|
{},
|
|
|
|
jwtToken,
|
|
|
|
)) as LoadedResource[];
|
2021-07-12 04:26:21 -07:00
|
|
|
return toOptions(databases);
|
|
|
|
},
|
|
|
|
|
|
|
|
async getTableIds(this: ILoadOptionsFunctions) {
|
2022-08-01 13:47:55 -07:00
|
|
|
const credentials = (await this.getCredentials('baserowApi')) as BaserowCredentials;
|
2021-07-12 04:26:21 -07:00
|
|
|
const jwtToken = await getJwtToken.call(this, credentials);
|
|
|
|
const databaseId = this.getNodeParameter('databaseId', 0) as string;
|
2022-07-15 08:13:06 -07:00
|
|
|
const endpoint = `/api/database/tables/database/${databaseId}/`;
|
2022-08-01 13:47:55 -07:00
|
|
|
const tables = (await baserowApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
endpoint,
|
|
|
|
{},
|
|
|
|
{},
|
|
|
|
jwtToken,
|
|
|
|
)) as LoadedResource[];
|
2021-07-12 04:26:21 -07:00
|
|
|
return toOptions(tables);
|
|
|
|
},
|
|
|
|
|
|
|
|
async getTableFields(this: ILoadOptionsFunctions) {
|
2022-08-01 13:47:55 -07:00
|
|
|
const credentials = (await this.getCredentials('baserowApi')) as BaserowCredentials;
|
2021-07-12 04:26:21 -07:00
|
|
|
const jwtToken = await getJwtToken.call(this, credentials);
|
|
|
|
const tableId = this.getNodeParameter('tableId', 0) as string;
|
|
|
|
const endpoint = `/api/database/fields/table/${tableId}/`;
|
2022-08-01 13:47:55 -07:00
|
|
|
const fields = (await baserowApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
endpoint,
|
|
|
|
{},
|
|
|
|
{},
|
|
|
|
jwtToken,
|
|
|
|
)) as LoadedResource[];
|
2021-07-12 04:26:21 -07:00
|
|
|
return toOptions(fields);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
|
|
|
const items = this.getInputData();
|
|
|
|
const mapper = new TableFieldMapper();
|
2022-08-30 08:55:33 -07:00
|
|
|
const returnData: INodeExecutionData[] = [];
|
2021-07-12 04:26:21 -07:00
|
|
|
const operation = this.getNodeParameter('operation', 0) as Operation;
|
|
|
|
|
|
|
|
const tableId = this.getNodeParameter('tableId', 0) as string;
|
2022-08-01 13:47:55 -07:00
|
|
|
const credentials = (await this.getCredentials('baserowApi')) as BaserowCredentials;
|
2021-07-12 04:26:21 -07:00
|
|
|
const jwtToken = await getJwtToken.call(this, credentials);
|
|
|
|
const fields = await mapper.getTableFields.call(this, tableId, jwtToken);
|
|
|
|
mapper.createMappings(fields);
|
|
|
|
|
|
|
|
for (let i = 0; i < items.length; i++) {
|
|
|
|
try {
|
|
|
|
if (operation === 'getAll') {
|
|
|
|
// ----------------------------------
|
|
|
|
// getAll
|
|
|
|
// ----------------------------------
|
|
|
|
|
|
|
|
// https://api.baserow.io/api/redoc/#operation/list_database_table_rows
|
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
const { order, filters, filterType, search } = this.getNodeParameter(
|
|
|
|
'additionalOptions',
|
|
|
|
0,
|
|
|
|
) as GetAllAdditionalOptions;
|
2021-07-12 04:26:21 -07:00
|
|
|
|
|
|
|
const qs: IDataObject = {};
|
|
|
|
|
|
|
|
if (order?.fields) {
|
|
|
|
qs['order_by'] = order.fields
|
|
|
|
.map(({ field, direction }) => `${direction}${mapper.setField(field)}`)
|
|
|
|
.join(',');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (filters?.fields) {
|
|
|
|
filters.fields.forEach(({ field, operator, value }) => {
|
|
|
|
qs[`filter__field_${mapper.setField(field)}__${operator}`] = value;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (filterType) {
|
|
|
|
qs.filter_type = filterType;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (search) {
|
|
|
|
qs.search = search;
|
|
|
|
}
|
|
|
|
|
|
|
|
const endpoint = `/api/database/rows/table/${tableId}/`;
|
2022-08-01 13:47:55 -07:00
|
|
|
const rows = (await baserowApiRequestAllItems.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
endpoint,
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
jwtToken,
|
|
|
|
)) as Row[];
|
2021-07-12 04:26:21 -07:00
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
rows.forEach((row) => mapper.idsToNames(row));
|
2022-08-30 08:55:33 -07:00
|
|
|
const executionData = this.helpers.constructExecutionMetaData(
|
|
|
|
this.helpers.returnJsonArray(rows),
|
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
returnData.push(...executionData);
|
2021-07-12 04:26:21 -07:00
|
|
|
} else if (operation === 'get') {
|
|
|
|
// ----------------------------------
|
|
|
|
// get
|
|
|
|
// ----------------------------------
|
|
|
|
|
|
|
|
// https://api.baserow.io/api/redoc/#operation/get_database_table_row
|
|
|
|
|
|
|
|
const rowId = this.getNodeParameter('rowId', i) as string;
|
|
|
|
const endpoint = `/api/database/rows/table/${tableId}/${rowId}/`;
|
|
|
|
const row = await baserowApiRequest.call(this, 'GET', endpoint, {}, {}, jwtToken);
|
|
|
|
|
|
|
|
mapper.idsToNames(row);
|
2022-08-30 08:55:33 -07:00
|
|
|
const executionData = this.helpers.constructExecutionMetaData(
|
|
|
|
this.helpers.returnJsonArray(row),
|
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
returnData.push(...executionData);
|
2021-07-12 04:26:21 -07:00
|
|
|
} else if (operation === 'create') {
|
|
|
|
// ----------------------------------
|
|
|
|
// create
|
|
|
|
// ----------------------------------
|
|
|
|
|
|
|
|
// https://api.baserow.io/api/redoc/#operation/create_database_table_row
|
|
|
|
|
|
|
|
const body: IDataObject = {};
|
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
const dataToSend = this.getNodeParameter('dataToSend', 0) as
|
|
|
|
| 'defineBelow'
|
|
|
|
| 'autoMapInputData';
|
2021-07-12 04:26:21 -07:00
|
|
|
|
2021-08-12 05:37:21 -07:00
|
|
|
if (dataToSend === 'autoMapInputData') {
|
2021-07-12 04:26:21 -07:00
|
|
|
const incomingKeys = Object.keys(items[i].json);
|
2021-08-12 05:37:21 -07:00
|
|
|
const rawInputsToIgnore = this.getNodeParameter('inputsToIgnore', i) as string;
|
2022-08-01 13:47:55 -07:00
|
|
|
const inputDataToIgnore = rawInputsToIgnore.split(',').map((c) => c.trim());
|
2021-07-12 04:26:21 -07:00
|
|
|
|
|
|
|
for (const key of incomingKeys) {
|
|
|
|
if (inputDataToIgnore.includes(key)) continue;
|
|
|
|
body[key] = items[i].json[key];
|
|
|
|
mapper.namesToIds(body);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
const fields = this.getNodeParameter('fieldsUi.fieldValues', i, []) as FieldsUiValues;
|
|
|
|
for (const field of fields) {
|
|
|
|
body[`field_${field.fieldId}`] = field.fieldValue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const endpoint = `/api/database/rows/table/${tableId}/`;
|
2022-08-01 13:47:55 -07:00
|
|
|
const createdRow = await baserowApiRequest.call(
|
|
|
|
this,
|
|
|
|
'POST',
|
|
|
|
endpoint,
|
|
|
|
body,
|
|
|
|
{},
|
|
|
|
jwtToken,
|
|
|
|
);
|
2021-07-12 04:26:21 -07:00
|
|
|
|
|
|
|
mapper.idsToNames(createdRow);
|
2022-08-30 08:55:33 -07:00
|
|
|
const executionData = this.helpers.constructExecutionMetaData(
|
|
|
|
this.helpers.returnJsonArray(createdRow),
|
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
returnData.push(...executionData);
|
2021-07-12 04:26:21 -07:00
|
|
|
} else if (operation === 'update') {
|
|
|
|
// ----------------------------------
|
|
|
|
// update
|
|
|
|
// ----------------------------------
|
|
|
|
|
|
|
|
// https://api.baserow.io/api/redoc/#operation/update_database_table_row
|
|
|
|
|
|
|
|
const rowId = this.getNodeParameter('rowId', i) as string;
|
|
|
|
|
|
|
|
const body: IDataObject = {};
|
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
const dataToSend = this.getNodeParameter('dataToSend', 0) as
|
|
|
|
| 'defineBelow'
|
|
|
|
| 'autoMapInputData';
|
2021-07-12 04:26:21 -07:00
|
|
|
|
|
|
|
if (dataToSend === 'autoMapInputData') {
|
|
|
|
const incomingKeys = Object.keys(items[i].json);
|
|
|
|
const rawInputsToIgnore = this.getNodeParameter('inputsToIgnore', i) as string;
|
2022-08-01 13:47:55 -07:00
|
|
|
const inputsToIgnore = rawInputsToIgnore.split(',').map((c) => c.trim());
|
2021-07-12 04:26:21 -07:00
|
|
|
|
|
|
|
for (const key of incomingKeys) {
|
|
|
|
if (inputsToIgnore.includes(key)) continue;
|
|
|
|
body[key] = items[i].json[key];
|
|
|
|
mapper.namesToIds(body);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
const fields = this.getNodeParameter('fieldsUi.fieldValues', i, []) as FieldsUiValues;
|
|
|
|
for (const field of fields) {
|
|
|
|
body[`field_${field.fieldId}`] = field.fieldValue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const endpoint = `/api/database/rows/table/${tableId}/${rowId}/`;
|
2022-08-01 13:47:55 -07:00
|
|
|
const updatedRow = await baserowApiRequest.call(
|
|
|
|
this,
|
|
|
|
'PATCH',
|
|
|
|
endpoint,
|
|
|
|
body,
|
|
|
|
{},
|
|
|
|
jwtToken,
|
|
|
|
);
|
2021-07-12 04:26:21 -07:00
|
|
|
|
|
|
|
mapper.idsToNames(updatedRow);
|
2022-08-30 08:55:33 -07:00
|
|
|
const executionData = this.helpers.constructExecutionMetaData(
|
|
|
|
this.helpers.returnJsonArray(updatedRow),
|
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
returnData.push(...executionData);
|
2021-07-12 04:26:21 -07:00
|
|
|
} else if (operation === 'delete') {
|
|
|
|
// ----------------------------------
|
|
|
|
// delete
|
|
|
|
// ----------------------------------
|
|
|
|
|
|
|
|
// https://api.baserow.io/api/redoc/#operation/delete_database_table_row
|
|
|
|
|
|
|
|
const rowId = this.getNodeParameter('rowId', i) as string;
|
|
|
|
|
|
|
|
const endpoint = `/api/database/rows/table/${tableId}/${rowId}/`;
|
|
|
|
await baserowApiRequest.call(this, 'DELETE', endpoint, {}, {}, jwtToken);
|
|
|
|
|
2022-08-30 08:55:33 -07:00
|
|
|
const executionData = this.helpers.constructExecutionMetaData(
|
|
|
|
[{ json: { success: true } }],
|
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
returnData.push(...executionData);
|
2021-07-12 04:26:21 -07:00
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
if (this.continueOnFail()) {
|
2022-08-30 08:55:33 -07:00
|
|
|
returnData.push({ error: error.message, json: {}, itemIndex: i });
|
2021-07-12 04:26:21 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-30 08:55:33 -07:00
|
|
|
return this.prepareOutputData(returnData);
|
2021-07-12 04:26:21 -07:00
|
|
|
}
|
|
|
|
}
|