diff --git a/packages/util-dynamodb/src/convertToNative.spec.ts b/packages/util-dynamodb/src/convertToNative.spec.ts index c235feae2eb0..b2a157fd1c54 100644 --- a/packages/util-dynamodb/src/convertToNative.spec.ts +++ b/packages/util-dynamodb/src/convertToNative.spec.ts @@ -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); + }); }); }); diff --git a/packages/util-dynamodb/src/convertToNative.ts b/packages/util-dynamodb/src/convertToNative.ts index 3b140d5c8dc5..c26a998892d8 100644 --- a/packages/util-dynamodb/src/convertToNative.ts +++ b/packages/util-dynamodb/src/convertToNative.ts @@ -29,10 +29,19 @@ export const convertToNative = (data: AttributeValue, options?: unmarshallOption case "M": return convertMap(value as Record, 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}`); diff --git a/packages/util-dynamodb/src/unmarshall.ts b/packages/util-dynamodb/src/unmarshall.ts index d6256fdc81ff..223a85b134a5 100644 --- a/packages/util-dynamodb/src/unmarshall.ts +++ b/packages/util-dynamodb/src/unmarshall.ts @@ -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; } /**