Postgres Agregates returning String

I’ve changed from Mysql to Postgres and Knexjs sends a String value when using serializers as count.

This means that when using

        .withCount('employers as isEmployer', (builder)=>{

Lucid is sending as result:
{ isEmployer:"0"}

instead of
{ isEmployer:0}

This is the knexjs documentation about this:


Performs a count on the specified column or array of columns (note that some drivers do not support multiple columns). Also accepts raw expressions. Note that in Postgres, count returns a bigint type which will be a String and not a Number (more info).

Is it possible to change the value to int before sending the response?


Your question is unclear, but I think what you are looking for is called getters.

Hi @begueradj

I updated the question. I saw the getters documentation and if is possible I don’t know how to do it.

I thought that maybe Serializers might be the solution but I can’t figure out how.

I did not try, but a quick hint would be to call toJSON() on the result of your query.

Actually if you write response.send(queryResult), the serialization will be applied automatically.

Hi. @begueradj

It doesnt work

tried with

await companies.fetch()
const result = companies.toJSON()

and with
const result = await companies.toJSON()

"companies.toJSON is not a function"

I read your edited question. So you want to convert the ID to integer before sending it to the client ? Well, do not care about that because HTTP is not a programming language, it does not know about data types.

Hi @begueradj

Well, Vue does recognize if the value is a String or Number, so its an issue.

Vue is JavaScript,
JavaScript is programming language, so it can recognize data types and convert/cast them.
HTTP treats integers as strings
So just send your data using response and convert it to whatever you want on the client side using Vue.js

He is sending JSON and it has data types. String, integer, boolean, array and object. If parsing request on client side JSON.parse will honor data types.

Coming back to question I think serializer will solve this problem. Never used custom serializers tho…

Or hackish method would be looping over results in controller and casting values

@begueradj simple explanation for you. He wants getCount to return a Number type, not String.

JS only supports 53 bit long integers, but postgres can return a 64 bit integer

There should be an option in the model to force the conversion.

Infact, I suggest using pg-types and do it at the driver level