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

feat(util-dynamodb): add option for using native array over sets #6591

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

GoPro16
Copy link

@GoPro16 GoPro16 commented Oct 27, 2024

Description

By default when an dynamo object is unmarshalled, sets are converted to native javascript sets.

This is fine for some use cases, however for other cases that rely on JSON.stringify whether its for an API call or mapping somewhere else sets are converted to undefined in this process.

This change gives the ability to override the usage of sets and prefer to use native arrays. I am not sure if this name is perfect but also up for anything here.

Syntax

const input = {
 NS: [1,2,3,4]
};

// without option
const obj = unmarshall(input)
// Output - new Set() {1,2,3,4}

// with option enabled
const obj = unmarshall(input, { preferNativeArrays: true})
// Output - [1,2,3,4]

Testing

Unit tests updated to account for use case.

Additional context

Some alternatives that I have used in the past for this are to manually clone deep with the modification like below.

import cloneDeepWith from 'lodash.clonedeepwith'

cloneDeepWith(unmarshall(record.dynamodb!.NewImage! as never), (val: unknown) => {
          // Sets dont stringify at all. Thanks AWS
          if (val instanceof Set) {
            return [...val];
          }
        });

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@GoPro16 GoPro16 requested a review from a team as a code owner October 27, 2024 01:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant