Feature/paired item support (#3869)

* Add paired item helper and implement it in some nodes
This commit is contained in:
Omar Ajoue
2022-08-30 17:55:33 +02:00
committed by GitHub
parent 087d3f99f1
commit b2c674591c
150 changed files with 4027 additions and 1625 deletions

View File

@@ -378,66 +378,63 @@ export class NocoDB implements INodeType {
const data = [];
const downloadAttachments = this.getNodeParameter('downloadAttachments', 0) as boolean;
try {
for (let i = 0; i < items.length; i++) {
requestMethod = 'GET';
for (let i = 0; i < items.length; i++) {
requestMethod = 'GET';
if (version === 1) {
endPoint = `/nc/${projectId}/api/v1/${table}`;
} else if (version === 2) {
endPoint = `/api/v1/db/data/noco/${projectId}/${table}`;
if (version === 1) {
endPoint = `/nc/${projectId}/api/v1/${table}`;
} else if (version === 2) {
endPoint = `/api/v1/db/data/noco/${projectId}/${table}`;
}
returnAll = this.getNodeParameter('returnAll', 0) as boolean;
qs = this.getNodeParameter('options', i, {}) as IDataObject;
if (qs.sort) {
const properties = (qs.sort as IDataObject).property as Array<{ field: string, direction: string }>;
qs.sort = properties.map(prop => `${prop.direction === 'asc' ? '' : '-'}${prop.field}`).join(',');
}
if (qs.fields) {
qs.fields = (qs.fields as IDataObject[]).join(',');
}
if (returnAll === true) {
responseData = await apiRequestAllItems.call(this, requestMethod, endPoint, {}, qs);
} else {
qs.limit = this.getNodeParameter('limit', 0) as number;
responseData = await apiRequest.call(this, requestMethod, endPoint, {}, qs);
if (version === 2) {
responseData = responseData.list;
}
}
const executionData = this.helpers.constructExecutionMetaData(
this.helpers.returnJsonArray(responseData),
{ itemData: { item: i } },
);
returnData.push(...executionData);
if (downloadAttachments === true) {
const downloadFieldNames = (this.getNodeParameter('downloadFieldNames', 0) as string).split(',');
const response = await downloadRecordAttachments.call(this, responseData, downloadFieldNames);
data.push(...response);
}
}
returnAll = this.getNodeParameter('returnAll', 0) as boolean;
qs = this.getNodeParameter('options', i, {}) as IDataObject;
if (qs.sort) {
const properties = (qs.sort as IDataObject).property as Array<{
field: string;
direction: string;
}>;
qs.sort = properties
.map((prop) => `${prop.direction === 'asc' ? '' : '-'}${prop.field}`)
.join(',');
if (downloadAttachments) {
return [data];
}
if (qs.fields) {
qs.fields = (qs.fields as IDataObject[]).join(',');
}
if (returnAll === true) {
responseData = await apiRequestAllItems.call(this, requestMethod, endPoint, {}, qs);
} catch (error) {
if (this.continueOnFail()) {
returnData.push({ json:{ error: error.toString() } });
} else {
qs.limit = this.getNodeParameter('limit', 0) as number;
responseData = await apiRequest.call(this, requestMethod, endPoint, {}, qs);
if (version === 2) {
responseData = responseData.list;
}
throw error;
}
returnData.push.apply(returnData, responseData);
if (downloadAttachments === true) {
const downloadFieldNames = (
this.getNodeParameter('downloadFieldNames', 0) as string
).split(',');
const response = await downloadRecordAttachments.call(
this,
responseData,
downloadFieldNames,
);
data.push(...response);
}
}
if (downloadAttachments) {
return [data];
}
} catch (error) {
if (this.continueOnFail()) {
returnData.push({ error: error.toString() });
}
throw error;
}
return this.prepareOutputData(returnData as INodeExecutionData[]);
}
if (operation === 'get') {
@@ -445,64 +442,66 @@ export class NocoDB implements INodeType {
const newItems: INodeExecutionData[] = [];
for (let i = 0; i < items.length; i++) {
try {
const id = this.getNodeParameter('id', i) as string;
try {
const id = this.getNodeParameter('id', i) as string;
if (version === 1) {
endPoint = `/nc/${projectId}/api/v1/${table}/${id}`;
} else if (version === 2) {
endPoint = `/api/v1/db/data/noco/${projectId}/${table}/${id}`;
}
responseData = await apiRequest.call(this, requestMethod, endPoint, {}, qs);
let newItem: INodeExecutionData = { json: {} };
if (version === 1) {
newItem = { json: responseData };
} else if (version === 2) {
if (Object.keys(responseData).length === 0) {
// Get did fail
const errorMessage = `The row with the ID "${id}" could not be queried. It probably doesn't exist.`;
if (this.continueOnFail()) {
newItem = {
json: { error: errorMessage },
};
if (version === 1) {
endPoint = `/nc/${projectId}/api/v1/${table}/${id}`;
} else if (version === 2) {
endPoint = `/api/v1/db/data/noco/${projectId}/${table}/${id}`;
}
throw new NodeApiError(
this.getNode(),
{ message: errorMessage },
{ message: errorMessage, itemIndex: i },
responseData = await apiRequest.call(this, requestMethod, endPoint, {}, qs);
if (version === 2 ) {
if (Object.keys(responseData).length === 0) {
// Get did fail
const errorMessage = `The row with the ID "${id}" could not be queried. It probably doesn't exist.`;
if (this.continueOnFail()) {
newItems.push({ json: {error: errorMessage }});
continue;
}
throw new NodeApiError(this.getNode(), { message: errorMessage }, { message: errorMessage, itemIndex: i });
}
}
const downloadAttachments = this.getNodeParameter('downloadAttachments', i) as boolean;
if (downloadAttachments === true) {
const downloadFieldNames = (this.getNodeParameter('downloadFieldNames', i) as string).split(',');
const data = await downloadRecordAttachments.call(this, [responseData], downloadFieldNames);
const newItem = {
binary: data[0].binary,
json: {},
};
const executionData = this.helpers.constructExecutionMetaData(
[newItem] as INodeExecutionData[],
{ itemData: { item: i } },
);
newItems.push(...executionData);
} else {
const executionData = this.helpers.constructExecutionMetaData(
this.helpers.returnJsonArray(responseData),
{ itemData: { item: i } },
);
newItems.push(...executionData);
}
} catch (error) {
if (this.continueOnFail()) {
const executionData = this.helpers.constructExecutionMetaData(
this.helpers.returnJsonArray({error: error.toString()}),
{ itemData: { item: i } },
);
} else {
// Get did work
newItem = { json: responseData };
}
}
// const newItem: INodeExecutionData = { json: responseData };
const downloadAttachments = this.getNodeParameter('downloadAttachments', i) as boolean;
if (downloadAttachments === true) {
const downloadFieldNames = (
this.getNodeParameter('downloadFieldNames', i) as string
).split(',');
const data = await downloadRecordAttachments.call(
this,
[responseData],
downloadFieldNames,
);
newItem.binary = data[0].binary;
newItems.push(...executionData);
continue;
}
throw new NodeApiError(this.getNode(), error, {itemIndex: i});
}
newItems.push(newItem);
} catch (error) {
if (this.continueOnFail()) {
newItems.push({ json: { error: error.toString() } });
continue;
}
throw new NodeApiError(this.getNode(), error);
}
}
return this.prepareOutputData(newItems);
}