Lamdera Live Model sync to local model

For those of you who develop Lamdera, one of the harder things is to get test data on the local instance.

I’ve recently started implementing a way of outputting the backend model via Lamdera’s undocumented w3_encode_{type} and w3_decode_{type} functions.

This has been an enormous productivity boost for getting our product ready for use, since I can now pull in data we generated on the deployed environments, straight into my local instance. This saves a lot of time setting getting state ready for usage testing.

Basically the solution just uses w3_encode_BackendModel to encode the model from one Lamdera instance and w3_decode_BackendModel to decode that model from Bytes and read it into another instance.

Module to import the Backend Model:

fetchImportedModel : String -> String -> Task Http.Error BackendModel
fetchImportedModel url modelKey =
    Http.task
        { method = "POST"
        , headers =
            [ Http.header "Content-Type" "application/octet-stream"
            , Http.header "X-Lamdera-Model-Key" modelKey
            ]
        , url = url
        , body = Http.emptyBody
        , resolver =
            Http.bytesResolver <|
                \response ->
                    case response of
                        Http.GoodStatus_ _ body ->
                            case bytesDecode Types.w3_decode_BackendModel body of
                                Just model ->
                                    Ok model

                                Nothing ->
                                    Err (Http.BadBody "Bytes decode failed")

                        Http.BadStatus_ meta _ ->
                            Err (Http.BadStatus meta.statusCode)

                        Http.NetworkError_ ->
                            Err Http.NetworkError

                        Http.Timeout_ ->
                            Err Http.Timeout

                        Http.BadUrl_ url_ ->
                            Err (Http.BadUrl url_)
        , timeout = Nothing
        }

Function to export the Backend model

getModel : HttpRequest -> SessionId -> BackendModel -> Dict.Dict String String -> intput -> ( Result Http.Error BackendModel, BackendModel, Cmd msg )
getModel _ _ model headers _ =
    case headers |> Dict.get "X-Lamdera-Model-Key" of
        Just modelKey ->
            if Env.modelKey == modelKey then
                ( model |> Ok, model, Cmd.none )

            else
                ( Http.BadStatus 401 |> Err, model, Cmd.none )

        Nothing ->
            ( Http.BadStatus 401 |> Err, model, Cmd.none )

Here’s the link to the commit to the kitchen-sink Lamdera example project. The commit is a little busy as I had to also upgrade the LamderaRPC.elm to a newer version.

Future work:
We can now actually include the Types files in the Lamdera public folder for consumption by other projects so that they can have a type safe way of interacting with our Elm endpoints.

4 Likes

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.