Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unmarshing DynamoStream events #2634

Open
maxm450 opened this issue Aug 2, 2021 · 11 comments
Open

Unmarshing DynamoStream events #2634

maxm450 opened this issue Aug 2, 2021 · 11 comments
Labels
feature-request New feature or enhancement. May require GitHub community feedback. p3 This is a minor priority issue

Comments

@maxm450
Copy link

maxm450 commented Aug 2, 2021

Describe the bug

TS types seems to cause an issue when unmarshing a dynamoStream

Argument of type '{ [key: string]: AWSLambda.AttributeValue; }' is not assignable to parameter of type '{ [key: string]: import("/node_modules/@aws-sdk/client-dynamodb/dist/types/models/models_0").AttributeValue; }'.

Your environment

SDK version number

@aws-sdk/[email protected]

Steps to reproduce

import { DynamoDBStreamEvent } from 'aws-lambda'
import { unmarshall } from "@aws-sdk/util-dynamodb"

export const handler =  async (event) => {
  for (const record of event.Records) {
    if (record.dynamodb?.NewImage) {
      const streamItem = record.dynamodb?.NewImage
      const item = unmarshall(streamItem)
      console.log(item)
    }
  }
}

Observed behavior

receiving typing issue for AttributeValue type.

Expected behavior

should be able to unmarshall a Dynamo object, wherever the object comes from a stream event or a getItem action from the DynamoClient

@maxm450 maxm450 added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Aug 2, 2021
@monken
Copy link

monken commented Aug 18, 2021

unmarshall will also not convert binary attributes properly. The DynamoDB stream will provide a base64 encoded presentation of the binary attribute and unmarshall does not base64-decode the value.

@sekhavati
Copy link

sekhavati commented Oct 4, 2021

I'm still seeing this issue in 3.34.0.

Interestingly there are no type errors if my package only contains @aws-sdk/util-dynamodb. But as soon as I install @aws-sdk/client-dynamodb the aforementioned TypeScript errors pop up.

From my brief testing with unmarshall it behaved as expected prior to installing @aws-sdk/client-dynamodb , so as a workaround I've set a @ts-ignore on the problematic line of code for now.

@ajredniwja ajredniwja added needs-review This issue/pr needs review from an internal developer. and removed needs-triage This issue or PR still needs to be triaged. labels Dec 13, 2021
@ajredniwja ajredniwja added needs-triage This issue or PR still needs to be triaged. and removed needs-review This issue/pr needs review from an internal developer. labels Jun 10, 2022
@ajredniwja
Copy link
Contributor

Hey everyone, apologies the issue fell out of queue. A lot of changes and features were added to the latest package. Is it still an persisting issue with the latest version. I was able to use unmarshall correctly used: https://www.npmjs.com/package/@aws-sdk/lib-dynamodb.

@ajredniwja ajredniwja added feature-request New feature or enhancement. May require GitHub community feedback. and removed bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Aug 22, 2022
@faridnsh
Copy link

Yes, this is still an issue as the types are not compatible. More info about it in DefinitelyTyped/DefinitelyTyped#51331

@RanVaknin RanVaknin added the p3 This is a minor priority issue label Feb 21, 2023
@nikiitat
Copy link

Any updates on this one?
I think this is also related to: DefinitelyTyped/DefinitelyTyped#51331

@aBurmeseDev aBurmeseDev added the queued This issues is on the AWS team's backlog label Mar 6, 2024
@obones
Copy link

obones commented May 22, 2024

unmarshall will also not convert binary attributes properly. The DynamoDB stream will provide a base64 encoded presentation of the binary attribute and unmarshall does not base64-decode the value.

I'm facing the same behavior here and I'm not sure what to do here.
Should this be reported in a separate issue?

@jimmyn
Copy link

jimmyn commented Oct 29, 2024

Any update on this? I'm facing the same issue with the latest version

@zshzbh
Copy link
Contributor

zshzbh commented Oct 29, 2024

Hey everyone,

I want to provide this workaround so you can decode the binary attributes.

import { DynamoDBClient } from "@aws-sdk/client-dynamodb";
import { unmarshall } from "@aws-sdk/util-dynamodb";

const dynamoClient = new DynamoDBClient({ region: "us-west-2" });

function customUnmarshall(item) {
  const unmarshalled = unmarshall(item);
  
  for (const [key, value] of Object.entries(unmarshalled)) {
    if (item[key] && item[key].B) {
      // Decode the base64 string to a UTF-8 string
      unmarshalled[key] = Buffer.from(item[key].B, 'base64').toString('utf-8');
    }
  }
  
  return unmarshalled;
}

export const handler = async (event) => {
  for (const record of event.Records) {
    if (record.dynamodb?.NewImage) {
      const streamItem = record.dynamodb.NewImage;
      const item = customUnmarshall(streamItem);
      console.log("Unmarshalled item (including decoded binary attributes):", item);
      
      for (const [key, value] of Object.entries(item)) {
        if (typeof value === 'string' && item[key] && item[key].B) {
          console.log(`Decoded binary attribute ${key}:`, value);
        }
      }
    }
  }
};

@zshzbh
Copy link
Contributor

zshzbh commented Oct 29, 2024

Using the workaround above, I used the following test case of DynamoDB object and it's working :

{
  "Records": [
    {
      "eventID": "1",
      "eventName": "INSERT",
      "eventVersion": "1.0",
      "eventSource": "aws:dynamodb",
      "awsRegion": "us-west-2",
      "dynamodb": {
        "Keys": {
          "Id": {
            "N": "101"
          }
        },
        "NewImage": {
          "Id": {
            "N": "101"
          },
          "Name": {
            "S": "John Doe"
          },
          "BinaryData": {
            "B": "SGVsbG8gV29ybGQ="
          }
        },
        "SequenceNumber": "111",
        "SizeBytes": 26,
        "StreamViewType": "NEW_AND_OLD_IMAGES"
      },
      "eventSourceARN": "arn:aws:dynamodb:us-west-2:123456789012:table/YourTableName/stream/2023-05-11T00:00:00.000"
    },
    {
      "eventID": "2",
      "eventName": "MODIFY",
      "eventVersion": "1.0",
      "eventSource": "aws:dynamodb",
      "awsRegion": "us-west-2",
      "dynamodb": {
        "Keys": {
          "Id": {
            "N": "102"
          }
        },
        "NewImage": {
          "Id": {
            "N": "102"
          },
          "Name": {
            "S": "Jane Smith"
          },
          "BinaryData": {
            "B": "SGVsbG8gQWdhaW4="
          }
        },
        "SequenceNumber": "222",
        "SizeBytes": 28,
        "StreamViewType": "NEW_AND_OLD_IMAGES"
      },
      "eventSourceARN": "arn:aws:dynamodb:us-west-2:123456789012:table/YourTableName/stream/2023-05-11T00:00:00.000"
    }
  ]
}

This is the log I got "

START RequestId: 1c248220-6a2f-49e0-a15b-0d32ac45d714 Version: $LATEST
2024-10-29T16:03:16.831Z	1c248220-6a2f-49e0-a15b-0d32ac45d714	INFO	Unmarshalled item (including decoded binary attributes): { Id: 101, Name: 'John Doe', BinaryData: 'Hello World' }
2024-10-29T16:03:16.833Z	1c248220-6a2f-49e0-a15b-0d32ac45d714	INFO	Unmarshalled item (including decoded binary attributes): { Id: 102, Name: 'Jane Smith', BinaryData: 'Hello Again' }
END RequestId: 1c248220-6a2f-49e0-a15b-0d32ac45d714
REPORT RequestId: 1c248220-6a2f-49e0-a15b-0d32ac45d714	Duration: 303.34 ms	Billed Duration: 304 ms	Memory Size: 128 MB	Max Memory Used: 87 MB	

@jimmyn,
Could you please help me understand what issue you are facing now? Do you have any minimal code reproduction
and error msg?

Thanks!

@zshzbh zshzbh added response-requested Waiting on additional info and feedback. Will move to \"closing-soon\" in 7 days. and removed queued This issues is on the AWS team's backlog labels Oct 29, 2024
@zshzbh
Copy link
Contributor

zshzbh commented Oct 29, 2024

Just talked with the team, it seems the issue is caused by the type mismatch.

Please vote or reply if you are encountering this issue with aws-lambda pkg installed

@jimmyn
Copy link

jimmyn commented Oct 29, 2024

Just talked with the team, it seems the issue is caused by the type mismatch.

Please vote or reply if you are encountering this issue with aws-lambda pkg installed

Yes, this is exactly what happens.

@github-actions github-actions bot removed the response-requested Waiting on additional info and feedback. Will move to \"closing-soon\" in 7 days. label Oct 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature-request New feature or enhancement. May require GitHub community feedback. p3 This is a minor priority issue
Projects
None yet
Development

No branches or pull requests