LiveSchema

A comprehensive state management library for Phoenix LiveView with DSL, type checking, and deep Phoenix integration.

Features

Installation

Add live_schema to your list of dependencies in mix.exs:

def deps do
  [
    {:live_schema, "~> 0.0.1"}
  ]
end

Quick Start

Define a state schema:

defmodule MyAppWeb.PostsLive.State do
  use LiveSchema

  schema do
    field :posts, {:list, Post}, default: []
    field :selected, Post, null: true
    field :loading, :boolean, default: false

    embeds_one :filter do
      field :status, {:enum, [:all, :active, :archived]}, default: :all
      field :search, :string, default: ""
    end
  end

  action :select_post, [:id] do
    post = Enum.find(state.posts, &(&1.id == id))
    set_selected(state, post)
  end

  action :update_filter, [:field, :value] do
    update_in(state.filter, &Map.put(&1, field, value))
  end
end

Use in your LiveView:

defmodule MyAppWeb.PostsLive do
  use MyAppWeb, :live_view
  use LiveSchema.View, schema: __MODULE__.State

  def mount(_params, _session, socket) do
    {:ok, assign(socket, :state, State.new())}
  end

  def handle_event("select", %{"id" => id}, socket) do
    state = State.apply(socket.assigns.state, {:select_post, String.to_integer(id)})
    {:noreply, assign(socket, :state, state)}
  end
end

Documentation

License

MIT License - see LICENSE for details.