Search code examples
node.jsormmikro-orm

Why is Mikro-orm sending back numbers as strings?


I have a postCount property on a User entity that looks like this:

@Property({columnType: 'bigint', nullable: false, default: 0})
postsCount: number;

I expect it to come back as a number, but it returns a string.

I've tried using the serializer to convert it to a number, but for my use case I sometimes return a modified user entity from my api (which I believe bypasses the serializer).

Is there another way to return properties as they are defined?

Thanks in advance!


Solution

  • That is most likely the expected correct behaviour of the underlying db driver like in the case of Postgres

    If you use Postgres you can customize the mapping this way:

    // Converts floats coming from db to a number
    pg.types.setTypeParser(1700, function (val) {
        return parseFloat(val)
    })
    // Bigints as numbers not as strings
    pg.types.setTypeParser(20, 'text', parseInt)
    

    where reference to pg object can be obtained from import

    import pg from 'pg'
    

    but I don't believe that is the ideal way how to obtain it.