Skip to content

Commit

Permalink
fix: BigInt Support for DynamoDB Convert (#3019)
Browse files Browse the repository at this point in the history
  • Loading branch information
TechTeam12 authored and trivikr committed Jan 24, 2020
1 parent 98b098b commit 99e558d
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"type": "feature",
"category": "BigInt Support for DynamoDB Convert",
"description": "Adding support for BigInt data type for DocumentDB Dynamo Client converter."
}
12 changes: 11 additions & 1 deletion lib/dynamodb/converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ AWS.DynamoDB.Converter = {
return { BOOL: data };
} else if (type === 'null') {
return { NULL: true };
} else if (type === 'BigInt') {
return { N: data.toString() };
} else if (type !== 'undefined' && type !== 'Function') {
// this value has a custom constructor
return formatMap(data, options);
Expand Down Expand Up @@ -221,7 +223,15 @@ function formatList(data, options) {
* @param wrapNumbers [Boolean]
*/
function convertNumber(value, wrapNumbers) {
return wrapNumbers ? new NumberValue(value) : Number(value);
if (wrapNumbers) {
return new NumberValue(value);
}
var numberValue = Number(value);
try {
return Number.isSafeInteger(numberValue) ? numberValue : BigInt(value);
} catch (err) {
return numberValue;
}
}

/**
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,4 @@
"helper-test": "mocha scripts/lib/test-helper.spec.js",
"csm-functional-test": "mocha test/publisher/functional_test"
}
}
}
26 changes: 26 additions & 0 deletions test/dynamodb/converter.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,17 @@ describe('AWS.DynamoDB.Converter', function() {
it('should convert numbers to NumberAttributeValues', function() {
expect(input(42)).to.deep.equal({N: '42'});
});
it('should convert bigint to NumberAttributeValues', function() {
try {
var largeInt = BigInt(1576706763859);
if (largeInt) {
expect(input(largeInt)).to.deep.equal({N: '1576706763859'});
}
} catch (err) {
expect(err.message).to.equal('BigInt is not defined');
expect(input(1576706763859)).to.deep.equal({N: '1576706763859'});
}
});
});

describe('null', function() {
Expand Down Expand Up @@ -375,6 +386,21 @@ describe('AWS.DynamoDB.Converter', function() {
expect(converted.toString()).to.equal(unsafeInteger);
}
);

it('should convert NumberAttributeValues to NumberValues of BigInt',
function() {
var unsafeInteger = '9007199254740991000';
try {
var largeInt = BigInt(unsafeInteger);
var converted = output({N: unsafeInteger});
expect(converted).to.equal(largeInt);
expect(typeof converted).to.equal('bigint');
} catch (err) {
expect(err.message).to.equal('BigInt is not defined');
expect(output({N: unsafeInteger}).toString()).to.equal(unsafeInteger);
}
}
);
});

describe('null', function() {
Expand Down

0 comments on commit 99e558d

Please sign in to comment.