Skip to content

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 }

@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
@github-actions
Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs and link to relevant comments in this thread.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 27, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants