I'm using Mongoose as part of a package , saving heaps of time.
The package I am using is https://github.com/florianholzapfel/express-restify-mongoose. When I query Mongodb , and I have a Decimal128 type , it returns those values like this:
{
"sku": "shGHYB12-60-LOZ",
"name": "Prd",
"size": "60",
"buyPrice": {
"$numberDecimal": "12.55"
}
}
Which is fine when it's in a Mongoose object , but when I expose it over the Rest API ( this package uses restify ) , it is just a json representation.
If I post it back through the REST Api in a Patch or Put request - just as a string or number ( see below ) , it works fine and stores it in the database correctly translating it using the Mongoose model.
{
"sku": "shGHYB12-60-LOZ",
"name": "Prd",
"size": "60",
"buyPrice": "12.5995"
}
The Part of my mongoose model is like this
"buyPrice": {
"type": "Decimal"
}
How can I store it as a decimal type in MongoDb but expose it as a number over REST? I'm just not sure how to go about it.
The 3 areas it could happen I guess are
- The Mongoose model
- Restify
- Some sort of "hook" in Mongoose.
I'm not overly familiar with either. Thanks for your time.