masumsoft / express-cassandra

Cassandra ORM/ODM/OGM for NodeJS with support for Apache Cassandra, ScyllaDB, Datastax Enterprise, Elassandra & JanusGraph.
http://express-cassandra.readthedocs.io
GNU Lesser General Public License v3.0
228 stars 67 forks source link

BigInt overflows and unability to switch to nodejs native BigInt #233

Closed QuentinFAIDIDE closed 2 years ago

QuentinFAIDIDE commented 3 years ago

Hello, I am experiencing overflow issues when using the goog.math.Long type for very large numbers this way:

row = {
  total_output: ExpressCassandra.datatypes.Long.fromInt(jsonObj.output_value)
};

Which make sense because my number is quite big. But the same integer in the data structure I send with raw nodejs integers to the drivers (represented as frozen lists of objects) does not overflow even though it's most of the time the same as the output value:

        let graphsenseObj = [];
        for(let i=0;i<etlObj.length;i++) {
            let adtypeid = 0;
            if(address_types.hasOwnProperty(etlObj[i].type)==true) {
                adtypeid = address_types[etlObj[i].type];
            }
            graphsenseObj.push({
                address: etlObj[i].addresses,
                value: etlObj[i].value,
                address_type: adtypeid
            });
        }

The value field of the frozen objects in the list is also a bigint in cassandra. Which makes me wonder why this happens in one but not the other.

The native datastax lib supports setting encodingOptions this way:

            encoding: {
                useBigIntAsLong: true
            }

But whenever I send bigint using the following:

let row = {
  total_outputs: BigInt(myStringifiedNumber)
};

I get a cryptic apollo error about the standard formatting for bigint in nodejs (number appended with n at the end):

apollo.model.validator.invalidvalue: Invalid Value: "120000000n" for Field: total_outputs (Type: bigint)

Is there a way to either pass the standard nodejs integer to cassandra to avoid the overflow, or to make the BigInt encoding work ?

Thank you and have a nice day.

masumsoft commented 2 years ago

Though it's a very late reply, it might help others who face the same issue. You may do something like the following:

row = {
  total_output: ExpressCassandra.datatypes.Long.fromString(mybignum.toString())
};