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

Extend ordering with null sorting #228

Merged
merged 5 commits into from
Sep 10, 2019
Merged

Extend ordering with null sorting #228

merged 5 commits into from
Sep 10, 2019

Conversation

jwoertink
Copy link
Member

@jwoertink jwoertink commented Sep 8, 2019

fixes #153

This adds the ability to order with NULLS FIRST or NULLS LAST by adding an argument to the asc_order and desc_order methods.

Note this is a breaking change since the query builder now takes an OrderBy instead of some symbols. Though, the Query object will still take symbols since this is the one really exposed to users

# Optional null sorting
UserQuery.new.name.asc_order(:nulls_first)

# This still works
UserQuery.new.order_by(:name, :asc)

# This is gone
QueryBuilder.new(table: :users).order_by(:name, :asc)

@jwoertink jwoertink marked this pull request as ready for review September 9, 2019 16:17
@jwoertink jwoertink changed the title [WIP] Extend ordering with null sorting Extend ordering with null sorting Sep 9, 2019
@jwoertink jwoertink requested a review from paulcsmith September 9, 2019 16:25
src/avram/query_builder.cr Outdated Show resolved Hide resolved
src/avram/query_builder.cr Outdated Show resolved Hide resolved
src/avram/queryable.cr Show resolved Hide resolved

it "returns a nice error when trying to order by a weird direction" do
expect_raises(Exception, /Accepted values are: :asc, :desc/) do
Post::BaseQuery.new.order_by(:published_at, :sideways)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should support sideways ordering in a future PR

rows
end

def asc_order : T
rows.query.order_by(column, :asc)
def asc_order(null_sorting : Avram::OrderBy::NullSorting = :default) : T
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love it!

@paulcsmith paulcsmith merged commit c4f0358 into master Sep 10, 2019
@paulcsmith paulcsmith deleted the features/153 branch September 10, 2019 14:25
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.

Add option for sorting nulls first/last
2 participants