lykmapipo / mongoose-money Goto Github PK
View Code? Open in Web Editor NEWMoney schema type for mongoose
Money schema type for mongoose
Hi,
I found a bug in your code that allows 0.0 amount to be stored in the database, but when you retrieve the value, a casting exception is thrown. This occurs because you have a place in index.js (at line 147) where you test for "value.amount", which in the case of a zero value will be false. I have fixed this by changing the test to "(value.amount !== undefined)", which does fix the problem. Here is the full code:
MongooseMoney.prototype.cast = function(value /,doc , init/ ) {
//TODO make use of base currency to cast value to
//money
// console.log(value);
if (null === value) {
return value;
}
if ('' === value) {
return null;
}
if (value instanceof Money) {
return value;
}
if (value && (value.amount !== undefined) && value.currency) {
if (value.time && value.time instanceof Date) {
return new Money(
value.amount,
Money[value.currency],
value.time
);
} else if (value.time && value.time instanceof String) {
return new Money(
value.amount,
Money[value.currency],
new Date(value.time)
);
} else {
return new Money(value.amount, Money[value.currency]);
}
}
throw new SchemaType.CastError('MongooseMoney', value);
};
I am using this in a new project, and was wondering if you could please put this change in soon so I can get it in "official" form? Hopefully this will make it super easy for you to do so :)
Cheers,
Ken
When storing values on mongo, the money amount
is stored as a Double
value, losing precission, which is not admissible for money.
See https://docs.mongodb.com/v3.2/tutorial/model-monetary-data/ for ideas on how to store money in mongodb.
This is extra infuriating because moneyjs
, the library this one uses to model Money internally, uses big.js
to model money amounts, which correctly stores arbitrary precission, but at some point that is converted to a regular JS Number
and finally stored as a Double
in mongo.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.