Pg-promise: 'select count(*) from foo' will return the count as a string

Created on 13 Apr 2016  路  8Comments  路  Source: vitaly-t/pg-promise

I am running the query 'select count(*) from foo' and it return the count as a string type. Is there a way that I can make the count as a number without explicitly casting it using Number().

question

Most helpful comment

When I call count(*) in postgres it returns a 64-bit bigint type number. But Javascirpt does not support 64-bit integers (which is also mentioned in pg-types). So it will be converted to string.

All 8 comments

Is there a way that I can make the count as a number without explicitly casting it using Number().

No.

See also in the demo: https://github.com/vitaly-t/pg-promise-demo/blob/master/JavaScript/db/repos/products.js

    total: () => rep.one("SELECT count(*) FROM Products")
            .then(data => parseInt(data.count))

that's the way to do it.

I see pg-promise uses node-postgres, is it the implementation of node-postgres or it is just the natural behavior of postgres itself?

I honestly don't know. You can check that yourself. All type conversions are done by pg-types.

Thanks, I will check it out there. :smiley:

@OnionCoder did you get to the bottom of it?

When I call count(*) in postgres it returns a 64-bit bigint type number. But Javascirpt does not support 64-bit integers (which is also mentioned in pg-types). So it will be converted to string.

Further development on the subject - BigInt Support.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ghost picture ghost  路  3Comments

blendsdk picture blendsdk  路  3Comments

cortopy picture cortopy  路  5Comments

realcarbonneau picture realcarbonneau  路  4Comments

paleite picture paleite  路  4Comments