@aws-sdk/util-dynamodb 3.686.0 → 3.689.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist-cjs/index.js CHANGED
@@ -117,7 +117,7 @@ var convertToAttr = /* @__PURE__ */ __name((data, options) => {
117
117
  } else if (typeof data === "boolean" || ((_d = data == null ? void 0 : data.constructor) == null ? void 0 : _d.name) === "Boolean") {
118
118
  return { BOOL: data.valueOf() };
119
119
  } else if (typeof data === "number" || ((_e = data == null ? void 0 : data.constructor) == null ? void 0 : _e.name) === "Number") {
120
- return convertToNumberAttr(data);
120
+ return convertToNumberAttr(data, options);
121
121
  } else if (data instanceof NumberValue) {
122
122
  return data.toAttributeValue();
123
123
  } else if (typeof data === "bigint") {
@@ -157,7 +157,7 @@ var convertToSetAttr = /* @__PURE__ */ __name((set, options) => {
157
157
  };
158
158
  } else if (typeof item === "number") {
159
159
  return {
160
- NS: Array.from(setToOperate).map(convertToNumberAttr).map((item2) => item2.N)
160
+ NS: Array.from(setToOperate).map((num) => convertToNumberAttr(num, options)).map((item2) => item2.N)
161
161
  };
162
162
  } else if (typeof item === "bigint") {
163
163
  return {
@@ -207,13 +207,15 @@ var convertToBigIntAttr = /* @__PURE__ */ __name((data) => ({ N: data.toString()
207
207
  var validateBigIntAndThrow = /* @__PURE__ */ __name((errorPrefix) => {
208
208
  throw new Error(`${errorPrefix} Use NumberValue from @aws-sdk/lib-dynamodb.`);
209
209
  }, "validateBigIntAndThrow");
210
- var convertToNumberAttr = /* @__PURE__ */ __name((num) => {
210
+ var convertToNumberAttr = /* @__PURE__ */ __name((num, options) => {
211
211
  if ([Number.NaN, Number.POSITIVE_INFINITY, Number.NEGATIVE_INFINITY].map((val) => val.toString()).includes(num.toString())) {
212
212
  throw new Error(`Special numeric value ${num.toString()} is not allowed`);
213
- } else if (num > Number.MAX_SAFE_INTEGER) {
214
- validateBigIntAndThrow(`Number ${num.toString()} is greater than Number.MAX_SAFE_INTEGER.`);
215
- } else if (num < Number.MIN_SAFE_INTEGER) {
216
- validateBigIntAndThrow(`Number ${num.toString()} is lesser than Number.MIN_SAFE_INTEGER.`);
213
+ } else if (!(options == null ? void 0 : options.allowImpreciseNumbers)) {
214
+ if (num > Number.MAX_SAFE_INTEGER) {
215
+ validateBigIntAndThrow(`Number ${num.toString()} is greater than Number.MAX_SAFE_INTEGER.`);
216
+ } else if (num < Number.MIN_SAFE_INTEGER) {
217
+ validateBigIntAndThrow(`Number ${num.toString()} is lesser than Number.MIN_SAFE_INTEGER.`);
218
+ }
217
219
  }
218
220
  return { N: num.toString() };
219
221
  }, "convertToNumberAttr");
@@ -275,6 +277,9 @@ var convertToNative = /* @__PURE__ */ __name((data, options) => {
275
277
  throw new Error(`No value defined: ${JSON.stringify(data)}`);
276
278
  }, "convertToNative");
277
279
  var convertNumber = /* @__PURE__ */ __name((numString, options) => {
280
+ if (typeof (options == null ? void 0 : options.wrapNumbers) === "function") {
281
+ return options == null ? void 0 : options.wrapNumbers(numString);
282
+ }
278
283
  if (options == null ? void 0 : options.wrapNumbers) {
279
284
  return NumberValue.from(numString);
280
285
  }
@@ -29,7 +29,7 @@ export const convertToAttr = (data, options) => {
29
29
  return { BOOL: data.valueOf() };
30
30
  }
31
31
  else if (typeof data === "number" || data?.constructor?.name === "Number") {
32
- return convertToNumberAttr(data);
32
+ return convertToNumberAttr(data, options);
33
33
  }
34
34
  else if (data instanceof NumberValue) {
35
35
  return data.toAttributeValue();
@@ -74,7 +74,7 @@ const convertToSetAttr = (set, options) => {
74
74
  else if (typeof item === "number") {
75
75
  return {
76
76
  NS: Array.from(setToOperate)
77
- .map(convertToNumberAttr)
77
+ .map((num) => convertToNumberAttr(num, options))
78
78
  .map((item) => item.N),
79
79
  };
80
80
  }
@@ -133,17 +133,19 @@ const convertToBigIntAttr = (data) => ({ N: data.toString() });
133
133
  const validateBigIntAndThrow = (errorPrefix) => {
134
134
  throw new Error(`${errorPrefix} Use NumberValue from @aws-sdk/lib-dynamodb.`);
135
135
  };
136
- const convertToNumberAttr = (num) => {
136
+ const convertToNumberAttr = (num, options) => {
137
137
  if ([Number.NaN, Number.POSITIVE_INFINITY, Number.NEGATIVE_INFINITY]
138
138
  .map((val) => val.toString())
139
139
  .includes(num.toString())) {
140
140
  throw new Error(`Special numeric value ${num.toString()} is not allowed`);
141
141
  }
142
- else if (num > Number.MAX_SAFE_INTEGER) {
143
- validateBigIntAndThrow(`Number ${num.toString()} is greater than Number.MAX_SAFE_INTEGER.`);
144
- }
145
- else if (num < Number.MIN_SAFE_INTEGER) {
146
- validateBigIntAndThrow(`Number ${num.toString()} is lesser than Number.MIN_SAFE_INTEGER.`);
142
+ else if (!options?.allowImpreciseNumbers) {
143
+ if (num > Number.MAX_SAFE_INTEGER) {
144
+ validateBigIntAndThrow(`Number ${num.toString()} is greater than Number.MAX_SAFE_INTEGER.`);
145
+ }
146
+ else if (num < Number.MIN_SAFE_INTEGER) {
147
+ validateBigIntAndThrow(`Number ${num.toString()} is lesser than Number.MIN_SAFE_INTEGER.`);
148
+ }
147
149
  }
148
150
  return { N: num.toString() };
149
151
  };
@@ -31,6 +31,9 @@ export const convertToNative = (data, options) => {
31
31
  throw new Error(`No value defined: ${JSON.stringify(data)}`);
32
32
  };
33
33
  const convertNumber = (numString, options) => {
34
+ if (typeof options?.wrapNumbers === "function") {
35
+ return options?.wrapNumbers(numString);
36
+ }
34
37
  if (options?.wrapNumbers) {
35
38
  return NumberValue.from(numString);
36
39
  }
@@ -25,6 +25,14 @@ export interface marshallOptions {
25
25
  * but false if directly using the marshall function (backwards compatibility).
26
26
  */
27
27
  convertTopLevelContainer?: boolean;
28
+ /**
29
+ * Whether to allow numbers beyond Number.MAX_SAFE_INTEGER during marshalling.
30
+ * When set to true, allows numbers that may lose precision when converted to JavaScript numbers.
31
+ * When false (default), throws an error if a number exceeds Number.MAX_SAFE_INTEGER to prevent
32
+ * unintended loss of precision. Consider using the NumberValue type from @aws-sdk/lib-dynamodb
33
+ * for precise handling of large numbers.
34
+ */
35
+ allowImpreciseNumbers?: boolean;
28
36
  }
29
37
  /**
30
38
  * Convert a JavaScript object into a DynamoDB record.
@@ -6,6 +6,7 @@ export interface marshallOptions {
6
6
  removeUndefinedValues?: boolean;
7
7
  convertClassInstanceToMap?: boolean;
8
8
  convertTopLevelContainer?: boolean;
9
+ allowImpreciseNumbers?: boolean;
9
10
  }
10
11
  export declare function marshall(
11
12
  data: null,
@@ -1,7 +1,10 @@
1
1
  import { AttributeValue } from "@aws-sdk/client-dynamodb";
2
2
  import { NativeAttributeValue } from "./models";
3
+ import { NumberValue } from "./NumberValue";
3
4
  export interface unmarshallOptions {
4
- wrapNumbers?: boolean;
5
+ wrapNumbers?:
6
+ | boolean
7
+ | ((value: string) => number | bigint | NumberValue | any);
5
8
  convertWithoutMapWrapper?: boolean;
6
9
  }
7
10
  export declare const unmarshall: (
@@ -1,14 +1,19 @@
1
1
  import { AttributeValue } from "@aws-sdk/client-dynamodb";
2
2
  import { NativeAttributeValue } from "./models";
3
+ import { NumberValue } from "./NumberValue";
3
4
  /**
4
5
  * An optional configuration object for `convertToNative`
5
6
  */
6
7
  export interface unmarshallOptions {
7
8
  /**
8
- * Whether to return numbers as a string instead of converting them to native JavaScript numbers.
9
+ * Whether to modify how numbers are unmarshalled from DynamoDB.
10
+ * When set to true, returns numbers as NumberValue instances instead of native JavaScript numbers.
9
11
  * This allows for the safe round-trip transport of numbers of arbitrary size.
12
+ *
13
+ * If a function is provided, it will be called with the string representation of numbers to handle
14
+ * custom conversions (e.g., using BigInt or decimal libraries).
10
15
  */
11
- wrapNumbers?: boolean;
16
+ wrapNumbers?: boolean | ((value: string) => number | bigint | NumberValue | any);
12
17
  /**
13
18
  * When true, skip wrapping the data in `{ M: data }` before converting.
14
19
  *
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aws-sdk/util-dynamodb",
3
- "version": "3.686.0",
3
+ "version": "3.689.0",
4
4
  "scripts": {
5
5
  "build": "concurrently 'yarn:build:cjs' 'yarn:build:es' 'yarn:build:types'",
6
6
  "build:cjs": "node ../../scripts/compilation/inline util-dynamodb",
@@ -25,7 +25,7 @@
25
25
  "tslib": "^2.6.2"
26
26
  },
27
27
  "devDependencies": {
28
- "@aws-sdk/client-dynamodb": "3.686.0",
28
+ "@aws-sdk/client-dynamodb": "3.687.0",
29
29
  "@tsconfig/recommended": "1.0.1",
30
30
  "concurrently": "7.0.0",
31
31
  "downlevel-dts": "0.10.1",
@@ -33,7 +33,7 @@
33
33
  "typescript": "~4.9.5"
34
34
  },
35
35
  "peerDependencies": {
36
- "@aws-sdk/client-dynamodb": "^3.686.0"
36
+ "@aws-sdk/client-dynamodb": "^3.687.0"
37
37
  },
38
38
  "engines": {
39
39
  "node": ">=16.0.0"