dynamodb-toolbox / dynamodb-toolbox

Lightweight and type-safe query builder for DynamoDB and TypeScript
https://www.dynamodbtoolbox.com/
MIT License
1.81k stars 168 forks source link

Formatter - support for BigInt #922

Open aaarichter opened 3 days ago

aaarichter commented 3 days ago

I'm using the dynamoDB geo library in combination with dynamoDB toolbox. The geo library is saving hash values like 5159980795834925053 in the geohash attribute. When I query the table entry, the geohash attribute value is returned as geohash: 5159984851327668183n, by the documentClient. Later in the queryCommand code, the formatter.format(item, { attributes }) function fails with the follow error

"Error: Invalid attribute detected while formatting: 'geohash'. Should be a number. Partition key: 515998",
        "    at formatPrimitiveAttrRawValue (/var/task/index.js:30641:11)",
        "    at formatAttrRawValue (/var/task/index.js:30745:14)",
        "    at /var/task/index.js:30781:32",
        "    at Array.forEach (<anonymous>)",
        "    at formatSchemaRawValue (/var/task/index.js:30771:38)",
        "    at Formatter.format (/var/task/index.js:30799:14)",
        "    at EntityFormatter.format (/var/task/index.js:31627:31)",
        "    at _QueryCommand2.send (/var/task/index.js:32099:39)",

A similar issue occurs when putting an item with a large number. DynamoDB toolbox throws here as well

aaarichter commented 2 days ago

https://github.com/dynamodb-toolbox/dynamodb-toolbox/issues/588

ThomasAribart commented 2 days ago

Wow I didn't even know bigint were a thing in DynamoDB 🤦‍♂️ Thanks for reaching out!

I have two ideas in mind:

I think it will depend on:

What do you think?

EDIT:

Well this definitely answers the question: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-2.html#bigint 😅 Will have to create a new primitive

ThomasAribart commented 17 hours ago

Actually, because DynamoDB doesn't really differ numbers from bigint, I'll with a number().big() option to allow for big ints.