Search code examples
postgresqlsails.jssails-postgresql

What's the best way to handle database columns with numeric data type in Sails.js?


I have an existing PostgreSQL database with lots of columns with data types like "numeric(27,10)" or "numeric(17,2)". In the Sails models I've set up for each table, I list the type of each attribute as either integer or float. However, when Sails outputs JSON for these tables, the data are represented as strings.

I can always run something like parseInt() on the client side, but is there a better way to fix this by reconfiguring sails?


Solution

  • is there a better way to fix this by reconfiguring sails?

    Not really. The issue is that the numeric data type in Postgres handles numbers with up to 131,072 digits before the decimal point--waayyyy bigger than any number your computer (or probably any computer) can handle in memory. So Waterline would have to decide what to do with cases where it couldn't cast the value to a float or integer (return an error, or a string?). Instead, it just takes the safe, predictable route and returns a string every time, which you'll have to handle in userland code.