4

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?

carpiediem
  • 1,918
  • 22
  • 41

1 Answers1

3

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.

sgress454
  • 24,870
  • 4
  • 74
  • 92
  • Good to know. Thanks for the explanation. – carpiediem Oct 05 '15 at 08:23
  • 1
    Just a follow-up note, in case other people stumble on this. If you are using Model.query() to write your own SQL, you can use CAST(columnname as integer) to convert the numeric type to something more JavaScript-friendly. As far as I can tell, there's not an equivalent way to do this using Model.find(). – carpiediem Nov 17 '15 at 06:52