@@ -120,15 +120,26 @@ export interface marshallOptions {
120120 * but false if directly using the marshall function (backwards compatibility).
121121 */
122122 convertTopLevelContainer? : boolean ;
123+ /**
124+ * Whether to allow numbers beyond Number.MAX_SAFE_INTEGER during marshalling.
125+ * When set to true, allows numbers that may lose precision when converted to JavaScript numbers.
126+ * When false (default), throws an error if a number exceeds Number.MAX_SAFE_INTEGER to prevent
127+ * unintended loss of precision. Consider using the NumberValue type from @aws-sdk/lib-dynamodb
128+ * for precise handling of large numbers.
129+ */
130+ allowImpreciseNumbers? : boolean ;
123131}
124132
125133export interface unmarshallOptions {
126134 /**
127- * Whether to return numbers as a string instead of converting them to native JavaScript numbers.
135+ * Whether to modify how numbers are unmarshalled from DynamoDB.
136+ * When set to true, returns numbers as NumberValue instances instead of native JavaScript numbers.
128137 * This allows for the safe round-trip transport of numbers of arbitrary size.
138+ *
139+ * If a function is provided, it will be called with the string representation of numbers to handle
140+ * custom conversions (e.g., using BigInt or decimal libraries).
129141 */
130- wrapNumbers? : boolean ;
131-
142+ wrapNumbers? : boolean | ((value : string ) => number | bigint | NumberValue | any );
132143 /**
133144 * When true, skip wrapping the data in `{ M: data }` before converting.
134145 *
@@ -235,10 +246,59 @@ const response = await client.get({
235246const value = response .Item .bigNumber ;
236247```
237248
249+ You can also provide a custom function to handle number conversion during unmarshalling:
250+
251+ ``` typescript
252+ const client = DynamoDBDocument .from (new DynamoDB ({}), {
253+ unmarshallOptions: {
254+ // Use BigInt for all numbers
255+ wrapNumbers : (str ) => BigInt (str ),
256+ },
257+ });
258+
259+ const response = await client .get ({
260+ Key: { id: 1 },
261+ });
262+
263+ // Numbers in response will be BigInt instead of NumberValue or regular numbers
264+ ```
265+
238266` NumberValue ` does not provide a way to do mathematical operations on itself.
239267To do mathematical operations, take the string value of ` NumberValue ` by calling
240268` .toString() ` and supply it to your chosen big number implementation.
241269
270+ The client protects against precision loss by throwing an error on large numbers, but you can either
271+ allow imprecise values with ` allowImpreciseNumbers ` or maintain exact precision using ` NumberValue ` .
272+
273+ ``` typescript
274+ const preciseValue = " 34567890123456789012345678901234567890" ;
275+
276+ // 1. Default behavior - will throw error
277+ await client .send (
278+ new PutCommand ({
279+ TableName: " Table" ,
280+ Item: {
281+ id: " 1" ,
282+ number: Number (preciseValue ), // Throws error: Number is greater than Number.MAX_SAFE_INTEGER
283+ },
284+ })
285+ );
286+
287+ // 2. Using allowImpreciseNumbers - will store but loses precision (mimics the v2 implicit behavior)
288+ const impreciseClient = DynamoDBDocumentClient .from (new DynamoDBClient ({}), {
289+ marshallOptions: { allowImpreciseNumbers: true },
290+ });
291+ await impreciseClient .send (
292+ new PutCommand ({
293+ TableName: " Table" ,
294+ Item: {
295+ id: " 2" ,
296+ number: Number (preciseValue ), // Loses precision 34567890123456790000000000000000000000n
297+ },
298+ })
299+ );
300+ ```
301+
242302### Client and Command middleware stacks
243303
244304As with other AWS SDK for JavaScript v3 clients, you can apply middleware functions
0 commit comments