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(lib-dynamodb): add support for imprecise numbers and custom number retrieval #6644

Merged
merged 3 commits into from
Nov 11, 2024

Conversation

RanVaknin
Copy link
Contributor

Issue

#6571
The SDK was throwing errors when handling large numbers (>MAX_SAFE_INTEGER) that worked in v2. This affected both:

  1. Marshalling: marshall({ foo: 1.23e+40 }) would throw an error
  2. Unmarshalling: unmarshall({ foo: { N: "1.23e+40" } }) required explicit handling

Description of changes

  1. Added marshallOptions.allowImpreciseNumbers

    • When true: Allows storing numbers > MAX_SAFE_INTEGER with potential precision loss (matches v2 behavior)
    • When false (default): Maintains existing precision protection
  2. expanded unmarshallOptions.wrapNumbers - Now accepts a function: (value: string) => number | bigint | NumberValue where customers can provide their own custom unmarshalling logic.

  3. Added unit test to cover the new marshal option and the expanded unamarshal option

  4. Updated @aws-sdk/lib-dynamodb readme docs to reflect the changes.

Driver code setup:

import { DynamoDBClient } from "@aws-sdk/client-dynamodb";
import { DynamoDBDocumentClient, PutCommand, GetCommand } from "@aws-sdk/lib-dynamodb";

const impreciseClient = DynamoDBDocumentClient.from(new DynamoDBClient({}), {
    marshallOptions: { allowImpreciseNumbers: true }
});

try {
  await impreciseClient.send(new PutCommand({
    TableName: "TestLargeNumbers",
    Item: { 
        id: 123,
        largeNumber: 34567890123456789012345678901234567890 // Store item with number larger than MAX_SAFE_INTEGER 
    }
}));

const result = await impreciseClient.send(new GetCommand({ 
    TableName: "TestLargeNumbers",
    Key: { id: 123 }
}));

console.log(result.Item);

} catch (error) {
  console.log(error);
}

Results in:

{ id: 123, largeNumber: 34567890123456790000000000000000000000n }

lib/lib-dynamodb/README.md Outdated Show resolved Hide resolved
packages/util-dynamodb/src/unmarshall.ts Outdated Show resolved Hide resolved
@kuhe kuhe marked this pull request as ready for review November 11, 2024 17:26
@kuhe kuhe requested a review from a team as a code owner November 11, 2024 17:26
@kuhe kuhe changed the title feat(lib-dynamodb): add support for imprecise numbers for large numbe… feat(lib-dynamodb): add support for imprecise numbers and custom number retrieval Nov 11, 2024
@kuhe kuhe merged commit 4e2f525 into aws:main Nov 11, 2024
4 checks passed
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.

2 participants