From cfe80b454da7e234a48b275eb5529dd75b8ea525 Mon Sep 17 00:00:00 2001 From: Michael Hirschler Date: Thu, 25 Mar 2021 15:50:35 +0100 Subject: [PATCH] dynamodb: request item --- .../nodes-base/nodes/Aws/AwsDynamoDB.node.ts | 149 ++++++++++++++++++ packages/nodes-base/nodes/Aws/dynamodb.svg | 13 ++ packages/nodes-base/package.json | 1 + 3 files changed, 163 insertions(+) create mode 100644 packages/nodes-base/nodes/Aws/AwsDynamoDB.node.ts create mode 100644 packages/nodes-base/nodes/Aws/dynamodb.svg diff --git a/packages/nodes-base/nodes/Aws/AwsDynamoDB.node.ts b/packages/nodes-base/nodes/Aws/AwsDynamoDB.node.ts new file mode 100644 index 0000000000..3000bd1d07 --- /dev/null +++ b/packages/nodes-base/nodes/Aws/AwsDynamoDB.node.ts @@ -0,0 +1,149 @@ +import {IExecuteFunctions} from 'n8n-core'; +import { + IDataObject, + INodeExecutionData, + INodeType, + INodeTypeDescription, +} from 'n8n-workflow'; + +import { awsApiRequestREST } from './GenericFunctions'; + +function decodeAttribute(type: string, attribute: any): any { + switch (type) { + case 'S': + return String(attribute); + case 'SS': + case 'M': + case 'L': + case 'NS': + return Array(...attribute); + case 'N': + return Number(attribute); + case 'BOOL': + return Boolean(attribute); + } +} + +export class AwsDynamoDB implements INodeType { + description: INodeTypeDescription = { + displayName: 'AWS DynamoDB', + name: 'awsDynamoDb', + icon: 'file:dynamodb.svg', + group: ['transform'], + version: 1, + description: 'Query data on AWS DynamoDB', + defaults: { + name: 'AWS DynamoDB', + color: '#2273b9', + }, + inputs: ['main'], + outputs: ['main'], + credentials: [ + { + name: 'aws', + required: true, + }, + ], + properties: [ + { + displayName: 'Resource', + name: 'resource', + type: 'options', + options: [ + { + name: 'Item', + value: 'item', + description: 'Query Items', + }, + { + name: 'Query', + value: 'query', + description: 'Invoke a function', + }, + ], + default: 'item', + description: 'The resource to operate on.', + }, + { + displayName: 'Operation', + name: 'operation', + type: 'options', + displayOptions: { + show: { + resource: ['item'], + } + }, + options: [ + { + name: 'GetItem', + value: 'get', + description: 'Read an item.', + }, + ], + default: 'get', + description: 'The operation to perform.', + }, + { + displayName: 'Table', + name: 'table', + type: 'string', + required: true, + displayOptions: { + show: { + resource: ['item', 'query'], + } + }, + default: '', + description: 'Specify the table you want to operate on', + }, + { + displayName: 'Item', + name: 'item', + type: 'string', + required: true, + displayOptions: { + show: { + resource: ['item'], + operation: ['get'], + } + }, + default: '', + description: 'Specify the Item you want to operate on', + } + ], + }; + + async execute(this: IExecuteFunctions): Promise { + const resource = this.getNodeParameter('resource', 0) as string; + const operation = this.getNodeParameter('operation', 0) as string; + + if (resource === 'item') { + if (operation === 'get') { + const table = this.getNodeParameter('table', 0) as string; + const item = this.getNodeParameter('item', 0) as string; + + const body = { + TableName: table, + Key: {id: {S: item}}, + }; + + const headers = { + 'X-Amz-Target': 'DynamoDB_20120810.GetItem', + 'Content-Type': 'application/x-amz-json-1.0' + }; + const response = await awsApiRequestREST.call(this, 'dynamodb', 'POST', '/', JSON.stringify(body), headers) + + const _item: any = {}; + for (const [attribute, value] of Object.entries(response.Item)) { + const [type, content] = Object.entries(value)[0]; + _item[attribute] = decodeAttribute(type, content); + } + + return [this.helpers.returnJsonArray(_item)]; + } + + } + + return [[]]; + } +} diff --git a/packages/nodes-base/nodes/Aws/dynamodb.svg b/packages/nodes-base/nodes/Aws/dynamodb.svg new file mode 100644 index 0000000000..7051ba38da --- /dev/null +++ b/packages/nodes-base/nodes/Aws/dynamodb.svg @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/packages/nodes-base/package.json b/packages/nodes-base/package.json index b4317f1f93..e9a74299d6 100644 --- a/packages/nodes-base/package.json +++ b/packages/nodes-base/package.json @@ -283,6 +283,7 @@ "dist/nodes/Autopilot/Autopilot.node.js", "dist/nodes/Autopilot/AutopilotTrigger.node.js", "dist/nodes/Aws/AwsLambda.node.js", + "dist/nodes/Aws/AwsDynamoDB.node.js", "dist/nodes/Aws/Comprehend/AwsComprehend.node.js", "dist/nodes/Aws/Rekognition/AwsRekognition.node.js", "dist/nodes/Aws/S3/AwsS3.node.js",