diff --git a/packages/nodes-base/nodes/Microsoft/CosmosDB/GenericFunctions.ts b/packages/nodes-base/nodes/Microsoft/CosmosDB/GenericFunctions.ts index b4de6a5591..d19f58843c 100644 --- a/packages/nodes-base/nodes/Microsoft/CosmosDB/GenericFunctions.ts +++ b/packages/nodes-base/nodes/Microsoft/CosmosDB/GenericFunctions.ts @@ -6,6 +6,7 @@ import type { IExecuteSingleFunctions, IHttpRequestOptions, ILoadOptionsFunctions, + IN8nHttpFullResponse, INodeExecutionData, INodeListSearchItems, INodeListSearchResult, @@ -344,6 +345,7 @@ export async function formatCustomProperties( requestOptions: IHttpRequestOptions, ): Promise { const rawCustomProperties = this.getNodeParameter('customProperties', '{}') as string; + const newId = this.getNodeParameter('newId') as string; let parsedProperties: Record; try { @@ -371,7 +373,7 @@ export async function formatCustomProperties( requestOptions.body = {}; } - Object.assign(requestOptions.body as Record, parsedProperties); + Object.assign(requestOptions.body as Record, { id: newId }, parsedProperties); return requestOptions; } @@ -418,6 +420,32 @@ export async function formatJSONFields( return requestOptions; } +export async function processResponse( + this: IExecuteSingleFunctions, + items: INodeExecutionData[], + response: IN8nHttpFullResponse, +): Promise { + if (!response || typeof response !== 'object' || !Array.isArray(items)) { + throw new ApplicationError('Invalid response format from Cosmos DB.'); + } + + const extractedDocuments: IDataObject[] = items.flatMap((item) => { + if ( + item.json && + typeof item.json === 'object' && + 'Documents' in item.json && + Array.isArray(item.json.Documents) + ) { + return item.json.Documents as IDataObject[]; + } + + return []; + }); + + return extractedDocuments; +} + +//WIP export async function mapOperationsToRequest( this: IExecuteSingleFunctions, requestOptions: IHttpRequestOptions, @@ -458,7 +486,6 @@ export async function mapOperationsToRequest( return formattedOperation; }); - // Assign the formatted operations to the request body requestOptions.body = { operations: formattedOperations }; return requestOptions; diff --git a/packages/nodes-base/nodes/Microsoft/CosmosDB/descriptions/ItemDescription.ts b/packages/nodes-base/nodes/Microsoft/CosmosDB/descriptions/ItemDescription.ts index 13f5ce611f..868e8e0ce8 100644 --- a/packages/nodes-base/nodes/Microsoft/CosmosDB/descriptions/ItemDescription.ts +++ b/packages/nodes-base/nodes/Microsoft/CosmosDB/descriptions/ItemDescription.ts @@ -3,6 +3,7 @@ import type { INodeProperties } from 'n8n-workflow'; import { formatCustomProperties, handlePagination, + processResponse, validateOperations, validateQueryParameters, } from '../GenericFunctions'; @@ -32,8 +33,7 @@ export const itemOperations: INodeProperties[] = [ method: 'POST', url: '=/colls/{{ $parameter["collId"] }}/docs', headers: { - // 'x-ms-partitionkey': '=["{{$parameter["newId"]}}"]', - // 'x-ms-documentdb-partitionkey': '=["{{$parameter["newId"]}}"]', + 'x-ms-documentdb-partitionkey': '=["{{$parameter["newId"]}}"]', 'x-ms-documentdb-is-upsert': 'True', }, }, @@ -83,14 +83,7 @@ export const itemOperations: INodeProperties[] = [ url: '=/colls/{{ $parameter["collId"] }}/docs', }, output: { - postReceive: [ - { - type: 'rootProperty', - properties: { - property: 'json', - }, - }, - ], + postReceive: [processResponse], }, }, action: 'Get many items', @@ -186,28 +179,28 @@ export const createFields: INodeProperties[] = [ }, ], }, - // { - // displayName: 'ID', - // name: 'newId', - // type: 'string', - // default: '', - // placeholder: 'e.g. AndersenFamily', - // description: "Item's ID", - // required: true, - // displayOptions: { - // show: { - // resource: ['item'], - // operation: ['create'], - // }, - // }, - // routing: { - // send: { - // type: 'body', - // property: 'id', - // value: '={{$value}}', - // }, - // }, - // }, + { + displayName: 'ID', + name: 'newId', + type: 'string', + default: '', + placeholder: 'e.g. AndersenFamily', + description: "Item's ID", + required: true, + displayOptions: { + show: { + resource: ['item'], + operation: ['create'], + }, + }, + routing: { + send: { + type: 'body', + property: 'id', + value: '={{$value}}', + }, + }, + }, { displayName: 'Custom Properties', name: 'customProperties',