maca / postgrest-admin-preview / PostgRestAdmin.Client

Client


type alias Client =
Internal.Client.Client

Represents a client for a PostgREST instance, including authentication params.

See Config and Config.FormAuth for authentication configuration options.

toHostUrl : Client -> Url

Obtain the PostgREST instance url.

Table


type alias Table =
Internal.Schema.Table

Represents a PostgREST table.

getTable : String -> Client -> Maybe Table

Obtain a table from the table name.

tableName : Table -> String

Obtain the name of a table

Requests

Note that the request functions do not produce a vanilla Elm Cmd but a PostgRestAdmin.Cmd.

fetchRecord : { client : Client, table : Table, id : String, expect : Result Error PostgRestAdmin.Record.Record -> msg } -> PostgRestAdmin.Cmd.Cmd msg

Fetches a record for a given table. expect param requires a function that returns a Msg.

import PostgRestAdmin.Cmd as AppCmd

fetchOne : (Result Error Record -> msg) -> String -> Client -> AppCmd.Cmd Msg
fetchOne tagger tableName client =
    case getTable tableName client of
        Just table ->
            fetchRecord
                { client = client
                , table = table
                , params = []
                , expect = tagger
                }

        Nothing ->
            AppCmd.none

fetchRecordList : { client : Client, table : Table, queryString : String, expect : Result Error (Collection PostgRestAdmin.Record.Record) -> msg } -> PostgRestAdmin.Cmd.Cmd msg

Fetches a list of records for a given table. expect param requires a function that returns a Msg.

import PostgRestAdmin.Cmd as AppCmd

fetchList : (Result Error (List Record) -> Msg) -> String -> Client -> AppCmd.Cmd Msg
fetchList tagger tableName client =
    case getTable tableName client of
        Just table ->
            fetchRecordList
                { client = client
                , table = table
                , params = []
                , expect = tagger
                }

        Nothing ->
            AppCmd.none

saveRecord : { client : Client, record : PostgRestAdmin.Record.Record, id : Maybe String, expect : Result Error PostgRestAdmin.Record.Record -> msg } -> PostgRestAdmin.Cmd.Cmd msg

Saves a record. expect param requires a function that returns a Msg.

You can use expectRecord to interpret the result as a Record.

import PostgRestAdmin.Cmd as AppCmd

save : (Result Error () -> Msg) -> Record -> Maybe String -> Client -> AppCmd.Cmd Msg
save tagger record id client =
    saveRecord
        { client = client
        , record = record
        , id = id
        , expect = tagger
        }

deleteRecord : { record : PostgRestAdmin.Record.Record, expect : Result Error () -> msg } -> Client -> PostgRestAdmin.Cmd.Cmd msg

Deletes a record. expect param requires a function that returns a Msg.

import PostgRestAdmin.Cmd as AppCmd

delete : (Result Error Record -> Msg) -> Record -> Client -> AppCmd.Cmd Msg
delete tagger record client =
    deleteRecord
        { client = client
        , record = record
        , expect = tagger
        }

request : { client : Client, method : String, headers : List Http.Header, path : String, body : Http.Body, decoder : Json.Decode.Decoder a, expect : Result Error a -> msg } -> PostgRestAdmin.Cmd.Cmd msg

Perform a request

requestMany : { client : Client, method : String, headers : List Http.Header, path : String, body : Http.Body, decoder : Json.Decode.Decoder a, expect : Result Error (Collection a) -> msg } -> PostgRestAdmin.Cmd.Cmd msg


type alias Collection a =
{ from : Basics.Int
, to : Basics.Int
, total : Basics.Int
, records : List a 
}


type alias Error =
Internal.Http.Error

Request error.

errorToString : Error -> String

Transform Error to an error explanation.

oneResolver : Http.Resolver Error Internal.Http.Response

manyResolver : Http.Resolver Error Internal.Http.Response

noneResolver : Http.Resolver Error Internal.Http.Response

task : { client : Client, method : String, headers : List Http.Header, path : String, body : Http.Body, resolver : Http.Resolver Error body, timeout : Maybe Basics.Float } -> Task Error body

Task to perform a request to a PostgREST instance resource.

attempt : (Result Error Internal.Http.Response -> msg) -> Task Error Internal.Http.Response -> Internal.Cmd.Cmd msg

decodeOne : Json.Decode.Decoder a -> Result Error Internal.Http.Response -> Result Error a

decodeMany : Json.Decode.Decoder a -> Result Error Internal.Http.Response -> Result Error (Collection a)

Authentication

isAuthenticated : Client -> Basics.Bool

Does the client has a valid JWT?

toJwtString : Client -> Maybe String

Obtain the JWT as a string.

authHeader : Client -> Maybe Http.Header

Generate an authorization header.