Yagger/elm-odata4/2.0.1

elm-odata4

Build Open Data Protocol (OData v4) queries in Elm.

This package supports a subset of OData 4.01 query option, common expression, and primitive literal.

http://docs.oasis-open.org/odata/odata/v4.01/odata-v4.01-part1-protocol.html

http://docs.oasis-open.org/odata/odata/v4.01/odata-v4.01-part2-url-conventions.html

  • If you need a query option, common expression, or primitive literal that is missing in this package undeservedly, suggest a PR
  • If what you need is missing in this package and it's ok, or it deviates from the standard, or other, then for:
    • query option, append custom Url.Builder.QueryParameter (see example)
    • common expression, use plainStringOperator (see example)
    • primitive literal, use customValue (see example)

Example

import Time
import Url
import Url.Builder

-- Custom value function
dateAsString : Time.Posix -> Value
dateAsString posix =
    customValue (\s -> "'" ++ s ++ "'") (date posix)

[ filter
    ( or
        [ eq "City" (string "Tallinn")
        , eq "City" (string "Singapore")
        -- Custom operator
        , plainStringOperator "concat(concat(City,', '), Country) eq 'Berlin, Germany'"
        -- Custom value
        , ge "start/dateTime" (dateAsString (Time.millisToPosix 1631124861000))
        ]
    )
, top 20
, skip 40
, orderBy [ ("Created", Just desc), ("City", Just asc) ]
, select [ "Id", "City", "Created", "Body" ]
]
|> List.map toQueryParameter
-- Custom query option
|> List.append [ Url.Builder.string "$search" "blue OR green" ]
|> Url.Builder.toQuery >> Url.percentDecode
--> Just (String.join ""
--> [ "?$search=blue OR green&"
--> , "$filter=City eq 'Tallinn' or "
--> ,   "City eq 'Singapore' or "
--> ,   "concat(concat(City,', '), Country) eq 'Berlin, Germany' or "
--> ,   "start/dateTime ge '2021-09-08'&"
--> , "$top=20&"
--> , "$skip=40&"
--> , "$orderBy=Created desc,City asc&"
--> , "$select=Id,City,Created,Body"
--> ])

Installation

$ elm install Yagger/elm-odata4

Testing

npx elm-verify-examples --run-tests

License

Copyright © 2021 Jegor Uglov

Distributed under the MIT license.