maca/postgrest-admin-preview/15.0.0

PostgRestAdmin

type alias Model =
{ appUrl : AppUrl
, baseUrl :
{ protocol : Protocol
, host : String
, port_ : Maybe Int
}
, key : Key
, notification : Notification
, authFormUrl : Url
, authFormField : Field Never
, authFormStatus : AuthFormStatus
, mountPath : MountPath
, menuLinks : List ( String, String )
, loginBannerText : Maybe String
, recordsPerPage : Int
, formFields : Dict String (List String)
, configErrors : List String
, listing : Maybe Model
, detail : Maybe Model
, form : Maybe Model
, clientAttributes : ClientAttributes
, client : Client
}
type Msg
type Config msg
= Config (Params msg) (List String)
type alias Program =
Program Value Model Msg

An alias to elm's Platform.Program providing the type signature for a PostgRestAdmin program.

type alias AppParams model outerMsg =
{ init : Value -> Url -> Key -> ( model, Cmd outerMsg )
, view : model -> Html outerMsg
, update : Msg -> model -> ( model, Cmd outerMsg )
, subscriptions : model -> Sub outerMsg
, onUrlRequest : UrlRequest -> outerMsg
, onUrlChange : Url -> outerMsg
}

Creates a default configuration.

main : PostgRestAdmin.Program
main =
    PostgRestAdmin.configure
        |> PostgRestAdmin.host "http://localhost:3000"
        |> PostgRestAdmin.onLogin loginSuccess
        |> PostgRestAdmin.buildProgram
buildProgram : Config msg -> Program

Converts a Config into a Program.

{ toInnerModel : model -> Model
, toOuterModel : Model -> model -> model
, toOuterMsg : Msg -> outerMsg
, initModel : Value -> Model -> model
}
-> Config msg
-> AppParams model outerMsg
applyConfiguration : Config msg -> Model -> ( Model, Cmd Msg )
appUrl : Model -> AppUrl

Wiring

onLogin : (String -> Cmd msg) -> Config msg -> Config msg

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

port loginSuccess : String -> Cmd msg


main : PostgRestAdmin.Program
main =
    PostgRestAdmin.configuration
        |> PostgRestAdmin.onLogin loginSuccess
        |> PostgRestAdmin.buildProgram

Then subscribe to the corresponding port.

app.ports.loginSuccess.subscribe(jwt => {
  sessionStorage.setItem("jwt", jwt)
});
onAuthFailed : (String -> Cmd msg) -> Config msg -> Config msg

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

port authFailure : String -> Cmd msg


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


main : PostgRestAdmin.Program
main =
    PostgRestAdmin.configuration
        |> PostgRestAdmin.onAuthFailed authFailure
        |> PostgRestAdmin.onExternalLogin tokenReceiver
        |> PostgRestAdmin.buildProgram

Then wire to the corresponding ports.

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

Callback triggered when the user logs out. You can use this to perform cleanup or external logout operations.

port logout : () -> Cmd msg


main : PostgRestAdmin.Program
main =
    PostgRestAdmin.configuration
        |> PostgRestAdmin.onLogout logout
        |> PostgRestAdmin.buildProgram

Then subscribe to the corresponding port.

app.ports.logout.subscribe(_ => {
    externalLogout()
});
( ( { path : String
, accessToken : String
}
-> { path : String
, accessToken : String
}
)
-> Sub
{ path : String
, accessToken : String
}
)
-> Config msg
-> Config msg

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

See onAuthFailed.

Client

withHost : String -> Config msg -> Config msg

Specify the PostgREST host.

main : PostgRestAdmin.Program
main =
    PostgRestAdmin.configuration
        |> PostgRestAdmin.withHost "http://localhost:3000"
        |> PostgRestAdmin.buildProgram
withLoginUrl : String -> Config msg -> Config msg

Specify the login URL for form authentication.

main : PostgRestAdmin.Program
main =
    PostgRestAdmin.configuration
        |> PostgRestAdmin.withLoginUrl "http://localhost:3000/rpc/login"
        |> PostgRestAdmin.buildProgram
withJWT : String -> Config msg -> Config msg

Set a JWT to authenticate PostgREST requests. You can set an initial JWT using this attribute.

main : PostgRestAdmin.Program
main =
    PostgRestAdmin.configuration
        |> PostgRestAdmin.withJWT "8abf3a...9ac36d"
        |> PostgRestAdmin.buildProgram
withClientHeaders : List Header -> Config msg -> Config msg

Set default HTTP headers to be included in all Client requests. This is useful for setting headers like Accept-Profile or Content-Profile when working with PostgREST schemas.

main : PostgRestAdmin.Program
main =
    PostgRestAdmin.configuration
        |> PostgRestAdmin.withClientHeaders
            [ Http.header "Accept-Profile" "bluebox"
            , Http.header "Content-Profile" "bluebox"
            ]
        |> PostgRestAdmin.buildProgram
withFlags : Bool -> Config msg -> Config msg

If set to False no attempt to decode and apply flags will be made. Defaults to true.

main : PostgRestAdmin.Program
main =
    PostgRestAdmin.configuration
        |> PostgRestAdmin.withFlags False
        |> PostgRestAdmin.buildProgram


-- Flags are ignored

UI

withMountPath : String -> Config msg -> Config msg

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

main : PostgRestAdmin.Program
main =
    PostgRestAdmin.configuration
        |> PostgRestAdmin.withMountPath "/back-office"
        |> PostgRestAdmin.buildProgram
withRecordsPerPage : Int -> Config msg -> Config msg

Set the number of records to display per page in listing views.

main : PostgRestAdmin.Program
main =
    PostgRestAdmin.configuration
        |> PostgRestAdmin.withRecordsPerPage 100
        |> PostgRestAdmin.buildProgram
withFormFields : String -> List String -> Config msg -> Config msg

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

main : PostgRestAdmin.Program
main =
    PostgRestAdmin.configuration
        |> PostgRestAdmin.withFormFields "posts" [ "id", "title", "content" ]
        |> PostgRestAdmin.buildProgram
withTables : List String -> Config msg -> Config msg

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

main : PostgRestAdmin.Program
main =
    PostgRestAdmin.configuration
        |> PostgRestAdmin.withTables [ "posts", "comments" ]
        |> PostgRestAdmin.buildProgram
withTableAliases : Dict String String -> Config msg -> Config msg

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

main : PostgRestAdmin.Program
main =
    PostgRestAdmin.configuration
        |> PostgRestAdmin.withTableAliases
            (Dict.fromList [ ( "published_posts", "posts" ) ])
        |> PostgRestAdmin.buildProgram
withLoginBannerText : String -> Config msg -> Config msg

Display a banner text below the login form. The text is rendered as markdown.

main : PostgRestAdmin.Program
main =
    PostgRestAdmin.configuration
        |> PostgRestAdmin.withLoginBannerText "**Welcome!** Please login to continue."
        |> PostgRestAdmin.buildProgram