Hacker News new | past | comments | ask | show | jobs | submit login
Ember Data and MongoDB (dbushell.com)
23 points by dbushell on April 25, 2013 | hide | past | favorite | 8 comments

I think the most important takeaway from this is how malleable and extendable Ember Data and its RESTAdapter are. It doesn't always (or, to be honest, almost never) works out of the box, but it's very easy to tweak.

Anyways, as for the way you're handling relationships:

The RESTAdapter works like it does because it expects hasMany<->belongsTo relationships. In that relationship:

- It can load the child record from an array of IDs on the parent.

- When a child record is updated, it is saved back to the server without including the parent.

- When a child record is added, it only saves itself back to the server. The server is expected to handle adding that record to the parent resource.

- Similarly, when a child record is deleted, it is saved back to the server and the server should remove it from the parent resource.

Essentially, the client shouldn't need to save the list of children back to the server.

The downside of this is that this doesn't actually work in a hasMany<->hasMany relationship, because a record in that relationship does need to send back the list of resources - it's the same as the way a belongsTo record will include its parent.

Regardless, there's no obvious harm in overriding RESTAdapter to do this, and it shouldn't impact the way that a regular hasMany<->belongsTo relationship is handled. Just make sure that the client-side records always have a current representation of their relationships, or you might end up saving back incorrect versions of the records.

Thanks, great explanation. I'm finding Ember Data really nice to use from my experience so far. I look forward to seeing an official release and documentation. For the time being the source code is heavily commented if you want to see what can be extended.

I've been working on getting API documentation added to ember.js.com, fwiw: https://github.com/emberjs/website/pull/457


What's Ember? Do you mean Ember.js?

Ember Data and Ember.js are different projects, although strongly related. Ember Data can be used to render database objects on client side with ease.

OP: Nice post. I've actually spent the last few days on the very same topic. I had the same problem with MongoDB's _id and everything I found on Google seemed to show different approach, from which none worked. Ultimately I changed to Parse and everything worked out of the box.

I would be interested to see how you hooked up Ember Data to Parse.

I used this: https://github.com/clintjhill/ember-parse-adapter

Basically just link the JS and add your API keys.


Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact