postgrest-client
This library allows you to construct and execute requests in a typesafe manner, with
little boilerplate. Here's what Api.People might look like:
import Api.People.Decoders exposing (..)
import Api.People.Encoders exposing (..)
import Api.People.Types exposing (..)
import Json.Decode exposing (..)
import Postgrest.Client as P
endpoint : P.Endpoint Person
endpoint =
P.endpoint
(postgrestURL "/people")
decodeUnit
primaryKey : P.PrimaryKey PersonID
primaryKey =
P.primaryKey ( "id", P.int << personID )
getMany : P.Params -> P.Request (List Person)
getMany params =
P.getMany endpoint
|> P.setParams params
delete : PersonID -> P.Request PersonID
delete =
P.deleteByPrimaryKey endpoint primaryKey
post : PersonSubmission -> P.Request Person
post submission =
P.postOne endpoint (encode submission)
Here's how you could use it:
import Api.People as People
import Postgrest.Client as P
jwt =
P.jwt "myjwt"
cmdExamples =
[ People.post
{ firstName = "Yasujirō"
, lastName = "Ozu"
}
|> P.toCmd jwt PersonCreated
, People.getMany
[ P.order [ P.asc "firstName" ], P.limit 10 ]
|> P.toCmd jwt PeopleLoaded
, Person.delete personID
|> P.toCmd jwt PersonDeleted
]
Request Construction and Modification
Think of an Endpoint as a combination between a base url like
/schoolsand an elm/jsonDecoder. The endpoint can be passed to other functions in this library, sometimes along with PrimaryKey to make constructing certain types of requests easier.