From 778cfb9a5a30ddb3ea51a3549340b18efbf9c14d Mon Sep 17 00:00:00 2001 From: Kyle Gray Date: Sat, 26 Oct 2024 21:12:15 -0400 Subject: [PATCH 1/2] feat(util-dynamodb): add option for using native array over sets --- .../util-dynamodb/src/convertToNative.spec.ts | 36 ++++++++++++++----- packages/util-dynamodb/src/convertToNative.ts | 9 +++++ packages/util-dynamodb/src/unmarshall.ts | 5 +++ 3 files changed, 42 insertions(+), 8 deletions(-) diff --git a/packages/util-dynamodb/src/convertToNative.spec.ts b/packages/util-dynamodb/src/convertToNative.spec.ts index c235feae2eb0..4e0d46f2f2bd 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(input); + }); }); - 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 })).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 })).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; } /** From 037b213c4a50de9a56edbf2a0934fc4418af47c8 Mon Sep 17 00:00:00 2001 From: Kyle Gray Date: Sat, 26 Oct 2024 21:42:11 -0400 Subject: [PATCH 2/2] test(util-dynamodb): update tests --- packages/util-dynamodb/src/convertToNative.spec.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/util-dynamodb/src/convertToNative.spec.ts b/packages/util-dynamodb/src/convertToNative.spec.ts index 4e0d46f2f2bd..b2a157fd1c54 100644 --- a/packages/util-dynamodb/src/convertToNative.spec.ts +++ b/packages/util-dynamodb/src/convertToNative.spec.ts @@ -244,7 +244,7 @@ describe("convertToNative", () => { }); it("with options.preferNativeArrays=true", () => { - expect(convertToNative({ NS: input }, { preferNativeArrays: true })).toEqual(input); + expect(convertToNative({ NS: input }, { preferNativeArrays: true })).toEqual([1, 2, BigInt(9007199254740996)]); }); }); @@ -260,7 +260,7 @@ describe("convertToNative", () => { const uint8Arr1 = new Uint8Array([...Array(4).keys()]); const uint8Arr2 = new Uint8Array([...Array(2).keys()]); const input = [uint8Arr1, uint8Arr2]; - expect(convertToNative({ BS: input })).toEqual(input); + expect(convertToNative({ BS: input }, { preferNativeArrays: true })).toEqual(input); }); }); @@ -272,7 +272,7 @@ describe("convertToNative", () => { it("with options.preferNativeArrays=true", () => { const input = ["one", "two", "three"]; - expect(convertToNative({ SS: input })).toEqual(input); + expect(convertToNative({ SS: input }, { preferNativeArrays: true })).toEqual(input); }); }); });