Skip to content
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 order parameter #5989

Closed
wants to merge 1 commit into from
Closed

Add order parameter #5989

wants to merge 1 commit into from

Conversation

vadimdemedes
Copy link

This PR adds order parameter to browse endpoints.

Closes #5602.

Supplied input string (e.g. title desc) is converted to the same format as produced by orderDefaultOptions():

{
    title: 'DESC'
}

Example

PostAPI.browse({ order: 'title desc, updated_at asc' });

Upper-cased DESC and ASC are also allowed. Spaces after commas are not required, this input is also valid: title desc, updated_at asc.

Note: If order parameter contains attributes, that do not belong to the current model, they will be ignored: bunny desc, title asc (bunny is skipped).

Todo

  • test fields with dots in them, e.g. posts.title desc
  • tests for other models (not sure this is needed)
  • decide, if custom order option should completely overwrite or merge with default order options (orderDefaultOptions())

@sebgie
Copy link
Contributor

sebgie commented Oct 22, 2015

looks good 👍, @ErisDS does ordering interfere with filtering?

@vadimdemedes
Copy link
Author

Rebased latest changes, now ready to merge.

@ErisDS
Copy link
Member

ErisDS commented Oct 23, 2015

This doesn't interfere with filtering - it just needs to play nicely with it :) Using the model name prefix as mentioned here would help with that I think.

This is all looking great so far, it might be worth adding a test to show that you can order posts by author.name?

@vadimdemedes
Copy link
Author

Sure, will update (it's in PR's todo list) ;)

@vadimdemedes
Copy link
Author

So, while implementing support for ordering by rows from outside tables (when using join), I discovered an issue with the way Bookshelf fetches records (or how Ghost uses Bookshelf). It does not use join, but uses extra select query to fetch additional rows from other tables and then merges them into the final result.

For example, I want to fetch posts and include author information in them:

PostAPI.browse({include: 'author'});

I'd expect a select query with a join statement. But here's what happens instead:

screen shot 2015-10-26 at 12 56 04 pm

It finds posts, then looks for author_id and finds that there are 3 unique author_ids. After that, it executes one more select with those ids. When that extra query is finished, author results get merged into posts result (into author column).

Summary: Because of this issue, it is not possible to order by "joined" values, e.g. author.name.

refs #5602
- add "order" to default browse options
- accept & parse order parameter in Base model
- add tests for posts order
@vadimdemedes
Copy link
Author

We are going to merge this PR as it is, without support for ordering by joined columns. Work on that problem will continue in a separate issue/PR (will be linked here).

@cobbspur
Copy link
Member

Hey @vdemedes I have been trying to test this using the {{#get}} helper.

It is working nicely so far for posts but if I fetch users or tags then the ordering does not seem to be working.

Example

{{#get "tags" limit="8" page="1" order="slug asc" as |tags|}}
    {{#foreach tags}}
        <span>{{slug}}</span>
    {{/foreach}}
{{/get}}

Would you expect that to work/have I missed something?

@vadimdemedes
Copy link
Author

Hey @cobbspur, that's my oversight, thanks for pointing this out!

@vadimdemedes
Copy link
Author

@cobbspur I added ordering to Tag and User models ;) See #6019. I had to re-submit, because I removed my original fork.

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

Successfully merging this pull request may close these issues.

4 participants