diff --git a/CHANGELOG.md b/CHANGELOG.md index c91b0a953..a2a2824bb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ This project adheres to [Semantic Versioning](http://semver.org/). ### Fixed - Do not apply limit to parent items - @ruslantalpa - Customize content negotiation per route - @begriffs +- Allow using nulls order without explicit order direction - @steve-chavez ### Changed - Use HTTP 400 for raise\_exception - @begriffs diff --git a/src/PostgREST/Parsers.hs b/src/PostgREST/Parsers.hs index dd556fd25..4b5d6af8c 100644 --- a/src/PostgREST/Parsers.hs +++ b/src/PostgREST/Parsers.hs @@ -137,13 +137,14 @@ pOrderTerm :: Parser OrderTerm pOrderTerm = try ( do c <- pFieldName - _ <- pDelimiter - d <- (string "asc" *> pure OrderAsc) - <|> (string "desc" *> pure OrderDesc) + d <- optionMaybe (try $ pDelimiter *> ( + try(string "asc" *> pure OrderAsc) + <|> try(string "desc" *> pure OrderDesc) + )) nls <- optionMaybe (pDelimiter *> ( try(string "nullslast" *> pure OrderNullsLast) <|> try(string "nullsfirst" *> pure OrderNullsFirst) )) return $ OrderTerm c d nls ) - <|> OrderTerm <$> (toS <$> pFieldName) <*> pure OrderAsc <*> pure Nothing + <|> OrderTerm <$> (toS <$> pFieldName) <*> pure Nothing <*> pure Nothing diff --git a/src/PostgREST/QueryBuilder.hs b/src/PostgREST/QueryBuilder.hs index a84ce11ff..438b997c1 100644 --- a/src/PostgREST/QueryBuilder.hs +++ b/src/PostgREST/QueryBuilder.hs @@ -307,7 +307,7 @@ requestToQuery schema isParent (DbRead (Node (Select colSelects tbls conditions queryTerm :: OrderTerm -> Text queryTerm t = " " <> toS (pgFmtColumn qi $ otTerm t) <> " " - <> show (otDirection t) <> " " + <> maybe "" show (otDirection t) <> " " <> maybe "" show (otNullOrder t) <> " " (joins, selects) = foldr getQueryParts ([],[]) forest diff --git a/src/PostgREST/Types.hs b/src/PostgREST/Types.hs index 9aee6d9e5..337ee6bfe 100644 --- a/src/PostgREST/Types.hs +++ b/src/PostgREST/Types.hs @@ -78,7 +78,7 @@ instance Show OrderNulls where data OrderTerm = OrderTerm { otTerm :: Text -, otDirection :: OrderDirection +, otDirection :: Maybe OrderDirection , otNullOrder :: Maybe OrderNulls } deriving (Show, Eq) diff --git a/test/Feature/QuerySpec.hs b/test/Feature/QuerySpec.hs index ac39f60b6..1be41c015 100644 --- a/test/Feature/QuerySpec.hs +++ b/test/Feature/QuerySpec.hs @@ -333,6 +333,17 @@ spec = do , matchHeaders = ["Content-Range" <:> "0-1/*"] } + it "by a column with nulls first" $ + get "/no_pk?order=a.nullsfirst" + `shouldRespondWith` ResponseMatcher { + matchBody = Just [json| [{"a":null,"b":null}, + {"a":"1","b":"0"}, + {"a":"2","b":"0"} + ] |] + , matchStatus = 200 + , matchHeaders = ["Content-Range" <:> "0-2/*"] + } + it "by a column asc with nulls last" $ get "/no_pk?order=a.asc.nullslast" `shouldRespondWith` ResponseMatcher {