geddy / model

Datastore-agnostic ORM in JavaScript
265 stars 55 forks source link

Eager Loading hasMany of same type Model #221

Closed danfinlay closed 10 years ago

danfinlay commented 10 years ago

Curious if anyone has gotten this working, or if it's a feature that would be nice to add:

Being able to set up recursive relationships (like a threaded comment system, where each comment has a parent comment), where a comment can be eagerly loaded with its children.

mde commented 10 years ago

Yes, I just posted the link of the other thread for what we call "reflexive associations" in the tests:

https://github.com/geddy/model/blob/master/test/integration/adapters/shared.js#L948

danfinlay commented 10 years ago

That's interesting, I'm surprised I don't see the inverse relationship declared, a 'getParent', maybe?

What does the adapter expect in this situation? Children have a child_id column?

mde commented 10 years ago

There's a Friendship join-model: https://github.com/geddy/model/blob/master/test/fixtures/person.js#L11

danfinlay commented 10 years ago

I figured the Friendship model was to enable many-to-many relationships. I definitely could do this, though, it's not that bad, but it seems like it should be unnecessary for a one-to-many, the way a reply comment is in a recursive comment system (each comment can only belong to a single comment).

mde commented 10 years ago

Oh, duh, yes. That's a different example, I'm sorry. So yes, it would be the Children association:

https://github.com/geddy/model/blob/master/test/fixtures/person.js#L13

The adapter looks for child_person_id, since it's named association. We use the name as a prefix for the actual model name.

danfinlay commented 10 years ago

Simple! Thanks!