Serialization for bigInteger

I have a model that has a column in DB with type bigInteger, when i call model.toJSON() this column is string. Where can I override it to be a Number

This is a solution: https://github.com/tgriesser/knex/issues/387#issuecomment-51554522
And thanks to @Melchyore for explanations.

At which level do you need that transformation ? Number or big integer value ?

1 Like

First of all I want to understand a reason why model.toJSON converts bigInteger to string.
I want always model.toJSON() with bigInteger to be a Number at any model.
For a model specific I can override toObject method, but what I have to do to stop converting bigInteger to string for any model?

All you can do is to convert the data on the client side.

1 Like

If you’re using PostgreSQL: https://github.com/brianc/node-postgres/pull/353

1 Like

Hi!

You can create custom serializer or getter depending on your needs.

https://adonisjs.com/docs/4.0/serializers#_creating_serializer

1 Like

Custom serializers are about data format, not data types. @McSneaky

1 Like

Thanks a lot

1 Like

He can also do type casting in there :wink:

Yes, we can do that (as well as a bunch of other operations) but the thing is: when it comes to data types, if we cast on the backend it will be useless because once sent over HTTP to the client side, we will have to cast again as HTTP does not know about data types.

Actually I do not see how the accepted answer helps the question, that is not what I read through the question.

He converts it to JSON. JSON has data types :wink:

(string, number, object, array, boolean, null)

https://www.json.org/

1 Like

Relevant info from Knex docs:

Note that bigint data is returned as a string in queries because JavaScript may be unable to parse them without loss of precision.

2 Likes