How to specify decimal(length, scale) as a columnType in waterline orm for a currency column? I could find this question has been already asked. But the answer is not relevant to the question.
This is what the column attribute looks like. I'm using sails-Postgres.
attributes: {
price: {
type: 'number',
columnType: 'decimal(9,2)',
columnName: 'PRICE'
}
}
In sails documents they have mentioned that
Column types are entirely database-dependent. Be sure that the
columnTypeyou select corresponds to a data type that is valid for your database! If you don’t specify acolumnType, the adapter will choose one for you based on the attribute’stype.
But when I mentioned columnType: 'decimal(9,2)' (although it supports in Postgres) it does not properly work. When I retrieving data using blueprints it gives the price as a string.
{
"price": "10.00",
}
I have read that these data types string, text, integer, float, date, datetime, boolean, binary, array, json, email are supported by waterline. In that case what would be the best data type to store a currency value? If we choose the float data type, how to store the price as decimal(9,2)?
Any help would be much appriciated. Thank you.
What I do in this case is to specify the definition like so:
Since I'm specifying a default price in here, Waterline will create a
float4column type for that value.It is also worthy of note that I will be converting that amount to center(by multiplying by 100) so What the database will store will be
3499I find it okay to store this way because payment providers will be needing a cent amount any ways.On the conversion, I find it suitable to do in a
beforeCreateWaterline lifecycle callback. So something like this.