maca / postgrest-admin-preview / PostgRestAdmin.Config

Program configuration


type alias Config f m msg =
Json.Decode.Decoder (Internal.Config.Config f m msg)

PostgRestAdmin.application configuration params.

Init

init : Config f m msg

PostgRestAdmin.application decoder with defaults.

main : PostgRestAdmin.Program Never Never Never
main =
    PostgRestAdmin.application Config.init

Basics

host : String -> Config f m msg -> Config f m msg

Specify the postgREST host.

main : PostgRestAdmin.Program Never Never Never
main =
    Config.init
        |> Config.host "http://localhost:3000"
        |> PostgRestAdmin.application

Alternatively the host can be specified using flags, configuring using host. Program flags take precedence.

Elm.Main.init({ "flags" : { "host" : "http://localhost:3000" }})

mountPath : String -> Config f m msg -> Config f m msg

Specify a path prefix for all routes, in case the app is not mounted in the root path.

main : PostgRestAdmin.Program Never Never Never
main =
    Config.init
        |> Config.mountPath "/back-office"
        |> PostgRestAdmin.application

Alternatively the host can be specified using flags, configuring using mountPath. Program flags take precedence.

Elm.Main.init({ "flags" : { "mountPath" : "/back-office" }})

formFields : String -> List String -> Config f m msg -> Config f m msg

Specify which fields should be present in the the edit and create forms, overriding the table schema. By default a primary key field is not present in the forms.

main : PostgRestAdmin.Program Never Never Never
main =
    Config.init
        |> Config.formFields "posts" [ "id", "title", "content" ]
        |> PostgRestAdmin.application

Alternatively this parameter can be configured passing the flag formFields. Program flags take precedence.

Elm.Main.init({
    "flags" : { "formFields" : { "posts" : [ "id", "title", "content" ] } }
})

detailActions : String -> List ( String, PostgRestAdmin.Record.Record -> String -> String ) -> Config f m msg -> Config f m msg

Specify a number of actions buttons to be shown in the detail page of a record along with Edit and Delete buttons.

detailActions expect a dict where the keys correspond with the name of a table and the values are a list of tuples, the first element of the tuple corresponds to the button text and the second is a function that takes the id of the resource and returns a url string.

import Url.Builder as Url

main : PostgRestAdmin.Program Never Never Never
main =
    Config.init
        |> Config.detailActions "posts"
            [ ( "View Comments"
              , \_ id -> Url.absolute [ "posts", id, "comments" ] []
              )
            ]
        |> PostgRestAdmin.application

menuLinks : List ( String, String ) -> Config f m msg -> Config f m msg

Pass a dict of links to display in the side menu. The list consists of a tuples of the link text and a url.

main : PostgRestAdmin.Program Never Never Never
main =
    Config.init
        |> Config.menuLinks [ ( "Api Docs", "/api/docs" ) ]
        |> PostgRestAdmin.application

Alternatively the menu links can be specified passing the flag menuLinks. Program flags take precedence.

Elm.Main.init({
    "menuLinks" : [
         { text : "Api Docs", url : "/api/docs" }
    ]
})

Tables

tables : List String -> Config f m msg -> Config f m msg

Pass a list of table names to restrict the editable resources, also sets the order of the left resources menu.

main : PostgRestAdmin.Program Never Never Never
main =
    Config.init
        |> Config.tables [ "posts", "comments" ]
        |> PostgRestAdmin.application

Alternatively the host can be specified passing the flag tables. Program flags take precedence.

Elm.Main.init({ "tables" : [ "posts", "comments" ]})

tableAliases : Dict String String -> Config f m msg -> Config f m msg

Rename a table referenced in a foreign key. PostgREST OpenApi genreated docs confuses tables with views when describing the foreign key for a resource, because of this some links might be incorrectly generated.

main : PostgRestAdmin.Program Never Never Never
main =
    Config.init
        |> Config.tableAliases
            (Dict.fromList [ ( "published_posts", "posts" ) ])
        |> PostgRestAdmin.application

Alternatively the host can be specified using flags, configuring using tableAliases. Program flags take precedence.

Elm.Main.init({
    tableAliases: { "published_posts" : "posts "}
})

Auth

formAuth : FormAuth -> Config f m msg -> Config f m msg

Enable user credentials form and configure the parameters. Credentials are be used to obtain a JWT.

See FormAuth for configuration options.

import PostgRestAdmin.Config.FormAuth as FormAuth

main : PostgRestAdmin.Program Never Never Never
main =
    Config.init
        |> Config.formAuth FormAuth.config
        |> PostgRestAdmin.application

jwt : String -> Config f m msg -> Config f m msg

Set a JWT to authenticate postgREST requests. Even when using formAuth it's possible to set an initial JWT.

main : PostgRestAdmin.Program Never Never Never
main =
    Config.init
        |> Config.jwt "8abf3a...9ac36d"
        |> PostgRestAdmin.application

Alternatively the token can be passed using flags, configuring using jwt. Program flags take precedence.

Elm.Main.init({
    "flags" : { "jwt" : sessionStorage.getItem("jwt") }
 })

onLogin : (String -> Platform.Cmd.Cmd msg) -> Config f m msg -> Config f m msg

Callback triggered with a JWT string on successful login. Tipically used to persist the JWT to session storage.

port loginSuccess : String -> Cmd msg

main : PostgRestAdmin.Program Never Never Never
main =
    Config.init
        |> Config.onLogin loginSuccess
        |> PostgRestAdmin.application

Then subscribe to the corresponding port.

app = Elm.Main.init({
  flags: { jwt: sessionStorage.getItem("jwt") }
})

app.ports.loginSuccess.subscribe(jwt => {
  sessionStorage.setItem("jwt", jwt)
});

onAuthFailed : (String -> Platform.Cmd.Cmd msg) -> Config f m msg -> Config f m msg

Callback triggered when authentication fails when attempting to perform a request. You can use to perform external authentication.

port authFailure : String -> Cmd msg

port tokenReceiver :
    ({ path : String, accessToken : String } -> msg)
    -> Sub msg

main : PostgRestAdmin.Program Never Never Never
main =
    Config.init
        |> Config.withAuthFailed authFailed
        |> Config.onExternalLogin tokenReceiver
        |> PostgRestAdmin.application

Then wire to the corresponding ports.

app = Elm.Main.init()

app.ports.authFailure.subscribe(requestedPath => {
    authenticate(requestedPath).then((accessToken) => {
        app.ports.tokenReceiver.send({
            path : requestedPath,
            accessToken : accessToken
        })
    })
});

onExternalLogin : (({ path : String, accessToken : String } -> { path : String, accessToken : String }) -> Platform.Sub.Sub { path : String, accessToken : String }) -> Config f m msg -> Config f m msg

Subscribe to receive a JWT and a redirect path when login with an external provider.

See onAuthFailed.

onLogout : (() -> Platform.Cmd.Cmd msg) -> Config f m msg -> Config f m msg

Callback triggered when authentication fails when attempting to perform a request. You can use to perform external authentication.

port logout : () -> Cmd msg

main : PostgRestAdmin.Program Never Never Never
main =
    Config.init
        |> Config.onLogout logout
        |> PostgRestAdmin.application

Then subscribe to the corresponding port.

app = Elm.Main.init()

app.ports.logout.subscribe(_ => {
    externalLogout()
});

Application mounting

routes : { init : { flags : flags, client : PostgRestAdmin.Client.Client, mountPath : PostgRestAdmin.MountPath.MountPath } -> Browser.Navigation.Key -> ( model, Internal.Cmd.Cmd msg ), view : model -> Html msg, update : msg -> model -> ( model, Internal.Cmd.Cmd msg ), subscriptions : model -> Platform.Sub.Sub msg, onLogin : PostgRestAdmin.Client.Client -> msg } -> Url.Parser.Parser (msg -> msg) msg -> Config flags model msg -> Config flags model msg

Mount an application on a give path using Url.Parser. This is usefull if you want to override an existing page or add additional behaviour.

The component specification is similar to the specification for Browser.element, with the addition of onLogin param for which a msg should be provided to be sent on successful login.

Note that the type signature changes from PostgRestAdmin.Program Never Nothing Nothing. Model and Msg are defined by your application.

The url parser should map to a Msg to be used to update your application when navigating to this route built the parameters that the parser defines, you can use Url.Parser.oneOf to parse many routes.

main : PostgRestAdmin.Program Never Model Msg
main =
    Config.init
        |> Config.routes
            { view = view
            , update = update
            , init = init
            , onLogin = LoggedIn
            }
            (Parser.map MyPostLoadedMsg
                (s "posts" </> Parser.string </> s "comments")
            )
        |> PostgRestAdmin.application

The application is initialized with a Client you can use to perform requests.

flagsDecoder : Json.Decode.Decoder flags -> Config flags model msg -> Config flags model msg

Decode flags to be passed to the init function for an application mounted using routes.

main : PostgRestAdmin.Program String Model Msg
main =
    Config.init
        |> Config.routes
            { view = view
            , update = update
            , init = init
            , onLogin = LoggedIn
            }
            (Parser.map MyPostLoadedMsg
                (s "posts" </> Parser.string </> s "comments")
            )
        |> Config.flagsDecoder (Decode.field "hostUrl" Decode.string)
        |> PostgRestAdmin.application

The application is initialized with a Client you can use to perform requests.