Let's examine the persistence in the TODO app example (https://github.com/dillonkearns/elm-pages/tree/master/examples/todos).
Overall project structure
-
app/Route/files - one per page, haspages,data, andviewfunctions;pagesruns at build time (or on server);dataruns at build time (or on server);viewruns at build time (or on server) and on client (e.g. Visibility__.elm, Login.elm)-
datafunction can refer toBackend.Customtasks, which call out to functions exposed from local node js libraries. In particular, there is acustom-backend-task.tsfile which defines functions wrapping prisma db calls.
-
app/files contains sitewide utilities like layout, menu messages, and global stateprisma/schema.prismadefines the database schemasrc/*contains local elm libraries of functions that route modules and others can use. elm-pages-v3 recommends usingappfor well known files required by the framework, and usingsrcfor additional elm source code
Interaction with backend persistence
reads
Receive any necessary parameters as data from front end, encode as a parameter object and send to backend custom functions. (
Backend.Custom.run "getTodosBySession" (Encode.string parsedSession) resultDecoderwhereparsedSessionis the parameter in this case.)The custom backend function receives arguments, runs call, responds with result.
The response is decoded back to elm types, using the
resultDecoder. The result is used building out aDatainstance and eventually invokingServer.Response.renderwith thisDatainstance.
writes
something triggers an update action, which reaches
performActionreceive data from action, encode it and other arguments to send to backend custom function (
BackendTask.Custom.run "createTodo" (Encode.object [ ( "sessionId", sessionId |> Encode.string ) ] ) noopDecoder)the custom backend function receives arguments, runs call
the response is decoded to elm types, which results in no value in this case, and then
Server.Response.renderwith a populatedActionData

Top comments (0)