Skip to content

Feature request: parser helper to work with DynamoDB data structure #3194

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

Closed
1 of 2 tasks
dreamorosi opened this issue Oct 11, 2024 · 4 comments
Closed
1 of 2 tasks
Assignees
Labels
completed This item is complete and has been merged/shipped feature-request This item refers to a feature request for an existing or new utility parser This item relates to the Parser Utility

Comments

@dreamorosi
Copy link
Contributor

Use case

When working with Amazon DynamoDB Stream events, the service sends the DynamoDB items in the database-specific format rather than regular JSON objects.

For example, a string might be represented as { "S": "foo" } rather than just foo. This makes parsing the actual changes within an event hard to parse and validate since it requires customers writing a Zod schema with this in mind.

We should work on adding a helper that eases the authoring of schemas by abstracting away the transformation (unmarshalling).

Solution/User Experience

The helper will need to depend on the @aws-sdk/util-dynamodb package, which should be an optional peer dependency for the Parser utility.

For this reason, we'll need to be intentional in how we expose the helper to avoid forcing the dependency on every Parser customer.

import { unmarshall } from '@aws-sdk/util-dynamodb';

const DynamoDBMarshalled = <T extends ZodTypeAny>(schema: T) =>
  z
    .record(z.string(), z.unknown())
    .transform((str, ctx) => {
      try {
        return unmarshall(str);
      } catch (err) {
        ctx.addIssue({
          code: 'custom',
          message: 'Could not unmarshall DynamoDB stream record',
          fatal: true,
        });

        return z.NEVER;
      }
    })
    .pipe(schema);

Which would then be used as:

import { DDBUnmarshalled } from '@aws-lambda-powertools/parser/helpers/dynamodb'; // <- new sub-path export
import { DynamoDBStreamSchema } from '@aws-lambda-powertools/parser/schemas/dynamodb';
import { z } from 'zod';

const MyItemSchema = z.object({
  foo: z.string(),
  bar: z.number().optional()
})

const EventSchema = DynamoDBStreamSchema.extend({
  dynamodb: z.object({
    NewImage: DDBUnmarshalled(MyItemSchema).optional(),
  }),
})

Note that we are also introducing a new sub-path export dedicated to this feature. By exporting the helper in its own path rather than in the main @aws-lambda-powertools/parser/helpers path we can make sure that only customers who actually want to use the helper need to install the extra peer dependency.

Alternative solutions

No response

Acknowledgment

Future readers

Please react with 👍 and your use case to help us understand customer demand.

@dreamorosi dreamorosi added confirmed The scope is clear, ready for implementation feature-request This item refers to a feature request for an existing or new utility help-wanted We would really appreciate some support from community for this one parser This item relates to the Parser Utility labels Oct 11, 2024
@dreamorosi dreamorosi moved this from Triage to Backlog in Powertools for AWS Lambda (TypeScript) Oct 11, 2024
@am29d am29d self-assigned this Oct 14, 2024
@arnabrahman
Copy link
Contributor

I see a help-wanted label. Can I work on this or is this already WIP? @am29d

@dreamorosi
Copy link
Contributor Author

Hi @arnabrahman, you can work on this.

I think @am29d's working on another issue.

Thanks for asking and nice to see you here!

@dreamorosi dreamorosi assigned arnabrahman and unassigned am29d Dec 15, 2024
@dreamorosi dreamorosi moved this from Backlog to Working on it in Powertools for AWS Lambda (TypeScript) Dec 15, 2024
@github-actions github-actions bot added pending-release This item has been merged and will be released soon and removed help-wanted We would really appreciate some support from community for this one confirmed The scope is clear, ready for implementation labels Jan 14, 2025
@github-project-automation github-project-automation bot moved this from Working on it to Coming soon in Powertools for AWS Lambda (TypeScript) Jan 16, 2025
Copy link
Contributor

⚠️ COMMENT VISIBILITY WARNING ⚠️

This issue is now closed. Please be mindful that future comments are hard for our team to see.

If you need more assistance, please either tag a team member or open a new issue that references this one.

If you wish to keep having a conversation with other community members under this issue feel free to do so.

Copy link
Contributor

This is now released under v2.13.1 version!

@github-actions github-actions bot added completed This item is complete and has been merged/shipped and removed pending-release This item has been merged and will be released soon labels Jan 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
completed This item is complete and has been merged/shipped feature-request This item refers to a feature request for an existing or new utility parser This item relates to the Parser Utility
Projects
Development

No branches or pull requests

3 participants