Add ability to order by jsonb keys, fix #644
This commit is contained in:
@@ -14,6 +14,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
|
||||
- Implement support for singular representation responses for POST/PATCH requests - @ehamberg
|
||||
- Include RPC endpoints in OpenAPI output - @begriffs, @LogvinovLeon
|
||||
- Custom request validation with `--pre-request` argument - @begriffs
|
||||
- Ability to order by jsonb keys - @steve-chavez
|
||||
|
||||
### Fixed
|
||||
- Do not apply limit to parent items - @ruslantalpa
|
||||
|
||||
@@ -136,7 +136,7 @@ pOrder = lexeme pOrderTerm `sepBy` char ','
|
||||
pOrderTerm :: Parser OrderTerm
|
||||
pOrderTerm =
|
||||
try ( do
|
||||
c <- pFieldName
|
||||
c <- pField
|
||||
d <- optionMaybe (try $ pDelimiter *> (
|
||||
try(string "asc" *> pure OrderAsc)
|
||||
<|> try(string "desc" *> pure OrderDesc)
|
||||
@@ -147,4 +147,4 @@ pOrderTerm =
|
||||
))
|
||||
return $ OrderTerm c d nls
|
||||
)
|
||||
<|> OrderTerm <$> (toS <$> pFieldName) <*> pure Nothing <*> pure Nothing
|
||||
<|> OrderTerm <$> pField <*> pure Nothing <*> pure Nothing
|
||||
|
||||
@@ -306,7 +306,7 @@ requestToQuery schema isParent (DbRead (Node (Select colSelects tbls conditions
|
||||
clause = intercalate "," (map queryTerm ts)
|
||||
queryTerm :: OrderTerm -> Text
|
||||
queryTerm t = " "
|
||||
<> toS (pgFmtColumn qi $ otTerm t) <> " "
|
||||
<> toS (pgFmtField qi $ otTerm t) <> " "
|
||||
<> maybe "" show (otDirection t) <> " "
|
||||
<> maybe "" show (otNullOrder t) <> " "
|
||||
(joins, selects) = foldr getQueryParts ([],[]) forest
|
||||
|
||||
@@ -77,7 +77,7 @@ instance Show OrderNulls where
|
||||
show OrderNullsLast = "nulls last"
|
||||
|
||||
data OrderTerm = OrderTerm {
|
||||
otTerm :: Text
|
||||
otTerm :: Field
|
||||
, otDirection :: Maybe OrderDirection
|
||||
, otNullOrder :: Maybe OrderNulls
|
||||
} deriving (Show, Eq)
|
||||
|
||||
@@ -374,6 +374,14 @@ spec = do
|
||||
, matchHeaders = ["Content-Range" <:> "0-2/*"]
|
||||
}
|
||||
|
||||
it "by a json column property asc" $
|
||||
get "/json?order=data->>id.asc" `shouldRespondWith`
|
||||
[json| [{"data": {"id": 0}}, {"data": {"id": 1, "foo": {"bar": "baz"}}}, {"data": {"id": 3}}] |]
|
||||
|
||||
it "by a json column with two level property nulls first" $
|
||||
get "/json?order=data->foo->>bar.nullsfirst" `shouldRespondWith`
|
||||
[json| [{"data": {"id": 3}}, {"data": {"id": 0}}, {"data": {"id": 1, "foo": {"bar": "baz"}}}] |]
|
||||
|
||||
it "without other constraints" $
|
||||
get "/items?order=id.asc" `shouldRespondWith` 200
|
||||
|
||||
|
||||
Vendored
+2
@@ -213,6 +213,8 @@ SELECT pg_catalog.setval('items_id_seq', 15, true);
|
||||
|
||||
TRUNCATE TABLE json CASCADE;
|
||||
INSERT INTO json VALUES ('{"foo":{"bar":"baz"},"id":1}');
|
||||
INSERT INTO json VALUES ('{"id":3}');
|
||||
INSERT INTO json VALUES ('{"id":0}');
|
||||
|
||||
|
||||
--
|
||||
|
||||
Reference in New Issue
Block a user