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
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 28 additions & 8 deletions packages/util-dynamodb/src/convertToNative.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,18 +242,38 @@ describe("convertToNative", () => {
new Set(input.map((numString) => ({ value: numString })))
);
});

it("with options.preferNativeArrays=true", () => {
expect(convertToNative({ NS: input }, { preferNativeArrays: true })).toEqual([1, 2, BigInt(9007199254740996)]);
});
});

it("binary set", () => {
const uint8Arr1 = new Uint8Array([...Array(4).keys()]);
const uint8Arr2 = new Uint8Array([...Array(2).keys()]);
const input = [uint8Arr1, uint8Arr2];
expect(convertToNative({ BS: input })).toEqual(new Set(input));
describe("binary set", () => {
it("without options.preferNativeArrays", () => {
const uint8Arr1 = new Uint8Array([...Array(4).keys()]);
const uint8Arr2 = new Uint8Array([...Array(2).keys()]);
const input = [uint8Arr1, uint8Arr2];
expect(convertToNative({ BS: input })).toEqual(new Set(input));
});

it("with options.preferNativeArrays=true", () => {
const uint8Arr1 = new Uint8Array([...Array(4).keys()]);
const uint8Arr2 = new Uint8Array([...Array(2).keys()]);
const input = [uint8Arr1, uint8Arr2];
expect(convertToNative({ BS: input }, { preferNativeArrays: true })).toEqual(input);
});
});

it("string set", () => {
const input = ["one", "two", "three"];
expect(convertToNative({ SS: input })).toEqual(new Set(input));
describe("string set", () => {
it("without options.preferNativeArrays", () => {
const input = ["one", "two", "three"];
expect(convertToNative({ SS: input })).toEqual(new Set(input));
});

it("with options.preferNativeArrays=true", () => {
const input = ["one", "two", "three"];
expect(convertToNative({ SS: input }, { preferNativeArrays: true })).toEqual(input);
});
});
});

Expand Down
9 changes: 9 additions & 0 deletions packages/util-dynamodb/src/convertToNative.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,19 @@ export const convertToNative = (data: AttributeValue, options?: unmarshallOption
case "M":
return convertMap(value as Record<string, AttributeValue>, options);
case "NS":
if (options?.preferNativeArrays) {
return (value as string[]).map((item) => convertNumber(item, options));
}
return new Set((value as string[]).map((item) => convertNumber(item, options)));
case "BS":
if (options?.preferNativeArrays) {
return (value as Uint8Array[]).map(convertBinary);
}
return new Set((value as Uint8Array[]).map(convertBinary));
case "SS":
if (options?.preferNativeArrays) {
return (value as string[]).map(convertString);
}
return new Set((value as string[]).map(convertString));
default:
throw new Error(`Unsupported type passed: ${key}`);
Expand Down
5 changes: 5 additions & 0 deletions packages/util-dynamodb/src/unmarshall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ export interface unmarshallOptions {
* but false if directly using the unmarshall function (backwards compatibility).
*/
convertWithoutMapWrapper?: boolean;

/**
* When true, return native JavaScript arrays instead of Sets.
*/
preferNativeArrays?: boolean;
}

/**
Expand Down