-
-
Notifications
You must be signed in to change notification settings - Fork 153
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Count method #351
Comments
Not sure if I follow the reasoning. ROM already provides a |
/cc @hanami/core-team, @hanami/contributors, @solnic and @flash-gordon |
After a little discussion with @flash-gordon I want to update my point of view. Of course I can call |
@davydovanton Note that in the latest version of hanami-model there is no publicly available |
@beauby yep, I think that |
@davydovanton Right. What I meant was more: would you expect this |
@beauby good question. I can't answer this question. Now I want to see a simple implementation of |
@davydovanton That's what I thought. The philosophy of the new hanami-model, though, is to not expose such query methods externally, and instead have you define intent-stating methods. |
It probably can be done via blocks, like |
Please cast your vote here. What do you think about this feature? It would be: class BookRepository < Hanami::Repository
def on_sale
books.where(on_sale: true)
end
end
repository = BookRepository.new
repository.count # => 100
repository.on_sale.count # => 12 |
@jodosha awesome 馃憤 |
So basically it's an ActiveRecord scope? Because then you could do Basically, either you need the records, and you get no penalty from materializing the collection, or you don't need the records, and you might as well define an actual repository method for getting the count. |
@beauby relation API is not exposed via repos so it'd be something like |
FWIW I don't think repos should expose such a db-specific API as |
I forgot about this aspect, thank you @solnic to reminding us. Folks, I think this changes the direction of this discussion. We can't have |
Folks, I've implemented a proposal at #354, can you please have a look? Thanks. |
Now that we have a more clear picture of both the implications and the solutions, I think we can close this. "Count" is a concept not generally available to all the databases. SQL databases have it, but others don't. If you're using a SQL database you can expose a method for that. class BookRepository < Hanami::Repository
def count
books.count
end
end If you want to count the records, under specific conditions: class BookRepository < Hanami::Repository
# ...
def on_sale_count
books.where(on_sale: true).count
end
end If you want to use raw SQL you can do: class BookRepository < Hanami::Repository
# ...
def old_books_count
books.read("SELECT id FROM books WHERE created_at < (NOW() - 1 * interval '1 year')").count
end
end We should document this. I'm gonna create an issue over our website repository. |
wisely it should be external plugin / gem and not in core modules. It is classic issue in ActiveRecord at beginning and till now, we can not combine mongoose, mongoid, oracle or any else into the same active record, the solution is remove active record and use external plugin or create an enhanced adapter. It should be the same game for hanami. |
I think it will be awesome to create
COUNT
command for repository object. What do you think?If you agree I can send PR 馃槉
The text was updated successfully, but these errors were encountered: