Skip to content

Commit 99e558d

Browse files
TechTeam12trivikr
authored andcommitted
fix: BigInt Support for DynamoDB Convert (#3019)
1 parent 98b098b commit 99e558d

File tree

4 files changed

+43
-2
lines changed

4 files changed

+43
-2
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"type": "feature",
3+
"category": "BigInt Support for DynamoDB Convert",
4+
"description": "Adding support for BigInt data type for DocumentDB Dynamo Client converter."
5+
}

lib/dynamodb/converter.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ AWS.DynamoDB.Converter = {
4949
return { BOOL: data };
5050
} else if (type === 'null') {
5151
return { NULL: true };
52+
} else if (type === 'BigInt') {
53+
return { N: data.toString() };
5254
} else if (type !== 'undefined' && type !== 'Function') {
5355
// this value has a custom constructor
5456
return formatMap(data, options);
@@ -221,7 +223,15 @@ function formatList(data, options) {
221223
* @param wrapNumbers [Boolean]
222224
*/
223225
function convertNumber(value, wrapNumbers) {
224-
return wrapNumbers ? new NumberValue(value) : Number(value);
226+
if (wrapNumbers) {
227+
return new NumberValue(value);
228+
}
229+
var numberValue = Number(value);
230+
try {
231+
return Number.isSafeInteger(numberValue) ? numberValue : BigInt(value);
232+
} catch (err) {
233+
return numberValue;
234+
}
225235
}
226236

227237
/**

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,4 +143,4 @@
143143
"helper-test": "mocha scripts/lib/test-helper.spec.js",
144144
"csm-functional-test": "mocha test/publisher/functional_test"
145145
}
146-
}
146+
}

test/dynamodb/converter.spec.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,17 @@ describe('AWS.DynamoDB.Converter', function() {
5757
it('should convert numbers to NumberAttributeValues', function() {
5858
expect(input(42)).to.deep.equal({N: '42'});
5959
});
60+
it('should convert bigint to NumberAttributeValues', function() {
61+
try {
62+
var largeInt = BigInt(1576706763859);
63+
if (largeInt) {
64+
expect(input(largeInt)).to.deep.equal({N: '1576706763859'});
65+
}
66+
} catch (err) {
67+
expect(err.message).to.equal('BigInt is not defined');
68+
expect(input(1576706763859)).to.deep.equal({N: '1576706763859'});
69+
}
70+
});
6071
});
6172

6273
describe('null', function() {
@@ -375,6 +386,21 @@ describe('AWS.DynamoDB.Converter', function() {
375386
expect(converted.toString()).to.equal(unsafeInteger);
376387
}
377388
);
389+
390+
it('should convert NumberAttributeValues to NumberValues of BigInt',
391+
function() {
392+
var unsafeInteger = '9007199254740991000';
393+
try {
394+
var largeInt = BigInt(unsafeInteger);
395+
var converted = output({N: unsafeInteger});
396+
expect(converted).to.equal(largeInt);
397+
expect(typeof converted).to.equal('bigint');
398+
} catch (err) {
399+
expect(err.message).to.equal('BigInt is not defined');
400+
expect(output({N: unsafeInteger}).toString()).to.equal(unsafeInteger);
401+
}
402+
}
403+
);
378404
});
379405

380406
describe('null', function() {

0 commit comments

Comments
 (0)