Giter Site home page Giter Site logo

Comments (4)

andylash avatar andylash commented on September 27, 2024

Sorry this reply too so long. I do store money objects in collections all the time. But I assign a transform to all my collections, so that when they come out of the find they are typed objects and then the getAmount() method will work. Did that make sense?

from meteor-money.

funkybunky avatar funkybunky commented on September 27, 2024

Hi, thanks for answering.
I ended up storing prices as integers manually using a constant multiplier (as it is recommended in the official Mongo docs) and convert it back only to show it in the UI. That way I can query for price using Collection.find( { price: XXXX } ) easily to get all items with the price XX.XX
I actually don't quite get what you mean. How do you use .find() to query for prices when they are stored as money objects? As far as I understand the docs (I may be wrong), the transform function just modifies what is returned by .find() ("Documents will be passed through this function before being returned from fetch or findOne"), so I guess this approach doesn't help for querying, right?

from meteor-money.

andylash avatar andylash commented on September 27, 2024

OK, now I understand. Yeah if you want to do database queries (or even Meteor collection queries) you do need to query against how it's stored. But you can still use the money object and just query against the amount field.

Assuming you've got a Meteor Collection that stored Money objects called Moneys:

//return all Money objects that have a price of $US 10.25
Moneys.find( { 'amount': 1025, currency: 'USD'})
//If you aren't worried about different currencies, you could even do:
Moneys.find( { 'amount': 1025})

The downside here is you're basically encoding how many significant digits are stored for USD (this is different for other currencies like JPY). I suppose you could do something like this to avoid retaining that knowledge in your calling code:

//return all Money objects that have a price of $US 10.25
Moneys.find( { 'amount': new Money(10.25, 'USD').amount), currency: 'USD' })

from meteor-money.

funkybunky avatar funkybunky commented on September 27, 2024

Thanks for the detailed answer!
Yes, that looks like a good approach, especially that last line where you avoid the hardcoding of the multiplier (significant digits).
I guess I won't change my implementation now again, but if I need money support in future projects, I will consider this approach. And perhaps others who had the same problem can be inspired, too.
Thanks again for the package :)

from meteor-money.

Related Issues (1)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.