solage v0.0.1 Solage.Serializer
Everything needed to serialize data and includes.
This makes few assumptions:
The render of the data structure is entirely in the client code.
We don’t implement the typical keys for a JSON API response. Rendering of the type, id, attributes
or relationships is the responsability of the client. This provides a powerful way
to implement features such as sparse fields.
By passing an optional Plug.Conn to the render function, we can implement
custom relationships links with no weird DSL forced by the module.
Only 2 functions required
By providing a module with a render/3 function and the necessary
relation_view/1 functions, we can have the results of what will be in a standard
JSON API response for the data and included keys.
Examples
Views:
defmodule PostView do
def render(data, _config, _conn) do
%{
id: data["id"],
relationships: %{
user: data["author_id"]
}
}
end
def relation_view(:author), do: UserView
end
defmodule UserView do
def render(data, _config, _conn) do
%{
id: data["id"],
name: data["fullname"]
}
end
end
Endpoint:
def show(conn, _) do
data = Solage.Serializer.render(PostView, conn.assigns[:post], conn.assigns[:jsonapi_query], conn)
included = Solage.Serializer.included(PostView, conn.assigns[:post], conn.assigns[:jsonapi_query], conn)
json(conn, 200, %{data: data, included: included})
end
Response:
{
"included": [
{
"id": "my-author-id",
"name": "Testy"
}
],
"data": [
{
"id": "my-post-id",
"relationships": {
"author": "my-author-id"
}
]
}
Summary
Functions
Recursively get include data from the Solage.QueryConfig include option
Proxy for the render call on the view module
Functions
Specs
included(atom, serializable, config, optional_conn) :: list
Recursively get include data from the Solage.QueryConfig include option.
If you’re using Ecto as the data, you will need the preload the assocations present
in the include option.