-
Notifications
You must be signed in to change notification settings - Fork 223
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’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add reorder function #225
base: master
Are you sure you want to change the base?
Add reorder function #225
Conversation
I don't really see the need for this. Wouldn't it be simpler to omit (def query (-> (select* users)
(where {:username [= "foo"])))
(def results (-> query
(order :created)
(limit 10)
(offset 25)))
(def total (-> query
(aggregate (count :*) :total)
(order :total))) |
My use case that is motivating the need for a
I don't think the pagination component should need to care about whether the initial query is sorted or not. Since the count query cannot be ordered by an arbitrary field, the only alternative that I'm aware of is to pass the unordered query to the pagination component and then perform any sorting on the paged result query. I'd argue that needing to know the proper call order like this is more complex than using the I'm currently clearing the previous order clauses manually: (-> query
(assoc :order [])
(order :total)) But it would be great if there was a function to do this so my application doesn't need to reach into the internal query data structure (which doesn't seem to be part of Korma's public API). |
A nomenclature note, the term "reorder" was a bit confusing because you're using it to reset ordering if I understand the comments correctly. |
I just stole the name from ActiveRecord (http://apidock.com/rails/ActiveRecord/QueryMethods/reorder). You're understanding it correctly though, it does reset all existing order clauses. Perhaps another name is more apt. |
In your use case you will still run into trouble if the initial query selects only certain columns using |
@immoh That's a good point. Personally, I think the query is a nice level of abstraction for this sort of thing but I understand your reluctance to add more reset functions. If you were writing a pagination component, what would you pass instead of the query? A collection of query clauses? What if one of those clauses is an ordering? Regardless of how the passed query is represented, I see two high-level approaches:
Option 1 seems nicer to me. Is there a way to do that w/o reordering? |
The count returned by the paginator, why cannot it use ORDER BY? AFAIK it's valid SQL, isn't it? |
@bilus I don't think so. Here's the error I get from Postgres when executing the following query: SELECT COUNT(*) FROM users ORDER BY id;
|
@bilus it isn't. Moreover, it makes no sense: column @scttnlsn I see two possible options for this use case, without
I've been using Rails for a while now. Rails' default ORM, ActiveRecord, is built on top of Arel, which is a project similar to Korma in Ruby world. And ActiveRecord itself has a certain abstract on top of Arel queries. Basically, a map that contains values fed in by various methods like |
This pull-request adds a
reorder
function similar to the existingorder
function, however, it first clears any existing order clauses.Example:
This results in the following SQL:
As another example, consider the case where we have a sorted base query that we'd like to return along with the total count. In the following case the
reorder
is actually necessary (otherwise the count query fails).