In Meteor, when you retrieve a record from a database, it's only a record. So if I have a collection called Dogs
, a dog
might have fur: 'brown'
or breath: 'stinky'
, but it doesn't have a bark()
method.
Obviously, I could create some functions that expect a dog
as an argument and then perform operations on that dog
. I could even encapsulate all these functions into a single constructor. I'm not crazy about this approach, but if someone's got a clean and sensible way to do it, I'm all ears.
Another thought I had was to wrap the dog
in a Backbone.Model
. This could be interesting, as fetch
and save
could be redefined to do find
and insert
or update
, and you can define all your behavior there as well, but I've read that this type of thing is generally discouraged.
Is there a right way to do it? Is there a Meteor.Model
officially in the works? How are others solving this problem?
Edit
For those coming to this question over a year after the accepted answer: At the time of this edit I am using Exygy's minimongoid mrt package, which has some improvements to haihappen's version that's mentioned in the blog post linked to by the accepted answer.
I'm currently contributing to the repository to make the result sets more relation-like. Hopefully others get use out of it and feel inclined to contribute helpful functionality.
Edit
Another answer suggested using the transform
property when creating the collection. While I'm definitely preferring something that I don't really need to build out myself, this feature adds a lot of possibilities, and I would hope that any teams that are working on an ORM for Meteor would take advantage of this at the core.
Here's a blog post explaining how to use the transform
property.
Also, minimongoid is now available as a Meteor package, and I am still using it. It has support for validation and for declaring relationships. I've added some functionality to this package as well, so if a board has many pieces, board.pieces().create(attributes)
will persist a new piece
record with the given attributes
and will automatically associate with the board
. Of the various solutions I've seen, this seems to be the most comprehensive.