8000 Fetching individual members from entities (or more generically, easier way to use SORM's SQL abstractions) · Issue #24 · sorm/sorm · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
This repository was archived by the owner on Aug 31, 2022. It is now read-only.

Fetching individual members from entities (or more generically, easier way to use SORM's SQL abstractions) #24< 8000 /span>

Open
danielkza opened this issue Jul 31, 2013 · 5 comments

Comments

@danielkza
Copy link
Contributor

After reading some of the questions and answers on StackOverflow while modelling a many-to-many relationship I decided to go with the relationship-as-a-class approach:

case class Post(title: String, content: String)
case class Tag(name: String)

case class PostTag(post: Post, tag: Tag)

Because I wanted to be able to fetch only part of the result set. With this I can do something like:

val someTag = db.query[Tag].whereEqual("name", "sorm")

db.query[PostTag].whereEqual("tag", someTag).limit(100)

But if I am not mistaken this will fetch the Tag again and construct an instance of it from the DB for every single row in the relationship. This seems really inefficient, so I'll probably have to resort to fetchWithSql, which is okay, but after writing the query I realized I'm hardcoding many of SORM's conventions, and that SORM already has a mechanism to map attributes to query paths (for the where clauses in queries).

So I ask if it would be too hard to use the existing filter infrastructure to fetch individual columns from the database.

Something like:

db.query[PostTag]("post").whereEqual(...).fetchColumn[Post]("post")

I would be willing to try and implement this myself with some guidance of where I need to look since this seems more complex than the other fixes.

.

@nikita-volkov
Copy link
Contributor

Concerning your model I'd strongly suggest simply referring to multiple tags from post with either a Seq or a Set, since it was one of the essential goals of SORM to make a solution to that problem really trivial. I.e.:

case class Post(title: String, content: String, tags: Set[Tag])
case class Tag(name: String)

Concerning your suggestion, something in the spirit of it really is a "must have" feature and it is planned to be implemented in 0.4 or later. If you implement it in 0.3, I'll gladly merge it. But I must warn you that it'll be really hard and will require you to dive very deeply into the current architecture of SORM.

@danielkza
Copy link
Contributor Author

I tried to use a generic example but ended up choosing one that didn't actually represent my problem.

case class Comment(replies: Set[Comment])

Does not work because recursive entities are not supported (it causes an infinite loop during runtime by the way). So I reverted to:

case class Comment

case class CommentChild(parent: Comment, child: Comment)

Because I wanted to be able to partially fetch for pagination, which I assume would not be possible with

case class CommentChildren(parent: Comment, children: Set[Comment])

What should I do then other than writing the query manually? Is there a way for me to use SORM's SQL abstractions to do it, or would it be too complicated?

Sorry for the long posts, maybe I should move this to a StackOverflow question.

@nikita-volkov
Copy link
Contributor

Yes, recursive entities are currently unsupported, but you've presented an example good enough for such support to be implemented. Since SORM works only with immutable values there's no way to pass a self-referring value to it, so it even looks like a safe thing. I'm seriously considering that.

For now you can go with this approach:

case class Comment
case class CommentChild(parent: Comment, child: Comment)

You can query for chunks like that:

val secondTenComments =
  db.query[CommentChild]
    .whereEqual("parent", parentComment).offset(10).limit(10)
    .fetch()
    .map(_.child)

But, of course, querying for deeper replies will require more queries.

Sorry for the long posts, maybe I should move this to a StackOverflow question.

No problem. Since such a question induces a discussion, here is a better place. StackOverflow is for specific "How to" questions, which don't induce prolonged discussion. I've also just created a sorm-framework@googlegroups.com mailing list for general discussions.

@danielkza
Copy link
Contributor Author

That is my current approach as well, but I have not yet found a way around it fetching the parent comment repeatedly with every CommentChild row.

@nikita-volkov
Copy link
Contributor

I'm afraid there is none yet, except for the manual SQL. That is of course until the partial fetching is realized.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants
0