agentsclimarketplace

Create live resource

Skill naymspace/backpex/skills/create-live-resource

Use when scaffolding a new Backpex LiveResource, setting up admin CRUD views, or configuring adapter_config, fields, filters, and routing for a resource.From its SKILL.md

Install
npx -y skills add naymspace/backpex --skill create-live-resource

Assembled from the repository path, not quoted from the project. Check it against their README if it does not work.

SKILL.md

6.7 KB, as published. Nobody here has run it

Creating a Backpex LiveResource

You are an expert at creating LiveResources for Backpex. When the user wants to create a new admin resource, follow this process:

  1. Identify the Ecto schema the resource will manage
  2. Generate the LiveResource module with adapter_config, fields, and callbacks
  3. Add the route to the router

LiveResource Module Structure

defmodule MyAppWeb.PostLive do
  use Backpex.LiveResource,
    adapter_config: [
      schema: MyApp.Post,
      repo: MyApp.Repo,
      update_changeset: &MyApp.Post.changeset/3,
      create_changeset: &MyApp.Post.changeset/3
    ]

  @impl Backpex.LiveResource
  def layout(_assigns), do: {MyAppWeb.Layouts, :admin}

  @impl Backpex.LiveResource
  def singular_name, do: "Post"

  @impl Backpex.LiveResource
  def plural_name, do: "Posts"

  @impl Backpex.LiveResource
  def fields do
    [
      title: %{
        module: Backpex.Fields.Text,
        label: "Title",
        searchable: true
      }
    ]
  end
end

use Backpex.LiveResource Options

OptionTypeDefaultDescription
adapter_configkeywordrequiredEcto adapter configuration (see below)
adapteratomBackpex.Adapters.EctoData layer adapter
primary_keyatom:idPrimary key field
per_page_optionslist[15, 50, 100]Selectable page sizes
per_page_defaultinteger15Default page size
init_ordermap or fn%{by: :id, direction: :asc}Initial sort order
fluid?booleanfalseFull-width layout
full_text_searchatomnilPostgreSQL tsvector column name
save_and_continue_button?booleanfalseShow "Save & Continue" button
pubsubkeywordnil (falls back to :backpex, :pubsub_server app config, topic defaults to module name)[server: MyApp.PubSub]
on_mountatom/listnilLiveView on_mount hooks

adapter_config (Ecto)

KeyRequiredDescription
schemayesEcto schema module
repoyesEcto repo module
update_changesetnofn item, attrs, metadata -> changeset
create_changesetnofn item, attrs, metadata -> changeset
item_querynofn query, live_action, assigns -> query

The metadata keyword list contains :assigns and :target (the form field that triggered the change).

The item_query function must build on the incoming query argument (use from p in query, ...), not the schema directly.

Required Callbacks

CallbackReturnsDescription
singular_name/0stringe.g. "Post"
plural_name/0stringe.g. "Posts"
fields/0keyword listField definitions
layout/1{module, :function} or fn assigns -> ...Layout to use

Optional Callbacks

CallbackDefaultDescription
can?/3always truefn assigns, action, item -> bool
filters/0[]Filter definitions
filters/1delegates to filters/0Assigns-aware variant for dynamic filters
panels/0[]Field grouping: [key: "Label"]
metrics/0[]Index page metrics
resource_actions/0[]Resource-level actions
item_actions/1returns default_actions unchangedModify default item actions
on_item_created/2noopfn socket, item -> socket
on_item_updated/2noopfn socket, item -> socket
on_item_deleted/2noopfn socket, item -> socket
return_to/5index pageCustom redirect after save
index_row_class/4nilCustom CSS for table rows
render_resource_slot/3default HTMLOverride UI slots
translate/1delegates to Backpex.translate/1Override UI strings

Router Setup

import Backpex.Router

scope "/admin", MyAppWeb do
  pipe_through :browser

  backpex_routes()

  live_session :admin, on_mount: Backpex.InitAssigns do
    live_resources "/posts", PostLive
    live_resources "/users", UserLive
    live_resources "/categories", CategoryLive, only: [:index, :show]
  end
end

backpex_routes() must appear once per scope. live_resources/3 generates routes for Index, Form (new/edit), and Show views.

Options for live_resources/3:

  • only: [:index, :show, :new, :edit] to restrict routes
  • except: [:new] to exclude specific routes

Complete Example

defmodule MyAppWeb.ProductLive do
  use Backpex.LiveResource,
    adapter_config: [
      schema: MyApp.Product,
      repo: MyApp.Repo,
      update_changeset: &MyApp.Product.changeset/3,
      create_changeset: &MyApp.Product.changeset/3,
      item_query: &__MODULE__.item_query/3
    ],
    per_page_default: 25,
    init_order: %{by: :inserted_at, direction: :desc}

  import Ecto.Query

  @impl Backpex.LiveResource
  def layout(_assigns), do: {MyAppWeb.Layouts, :admin}

  @impl Backpex.LiveResource
  def singular_name, do: "Product"

  @impl Backpex.LiveResource
  def plural_name, do: "Products"

  @impl Backpex.LiveResource
  def panels do
    [details: "Details", metadata: "Metadata"]
  end

  @impl Backpex.LiveResource
  def fields do
    [
      name: %{
        module: Backpex.Fields.Text,
        label: "Name",
        searchable: true,
        panel: :details
      },
      price: %{
        module: Backpex.Fields.Currency,
        label: "Price",
        panel: :details
      },
      category: %{
        module: Backpex.Fields.BelongsTo,
        label: "Category",
        display_field: :name,
        live_resource: MyAppWeb.CategoryLive,
        panel: :details
      },
      published: %{
        module: Backpex.Fields.Boolean,
        label: "Published",
        index_editable: true
      },
      inserted_at: %{
        module: Backpex.Fields.DateTime,
        label: "Created At",
        only: [:index, :show],
        panel: :metadata
      }
    ]
  end

  @impl Backpex.LiveResource
  def can?(_assigns, :delete, item), do: not item.published
  def can?(_assigns, _action, _item), do: true

  def item_query(query, _live_action, _assigns) do
    from p in query, where: is_nil(p.archived_at)
  end
end

Conventions

  • File location: lib/my_app_web/live/<resource>_live.ex
  • Module naming: MyAppWeb.<Resource>Live (e.g. MyAppWeb.ProductLive)
  • Changeset functions should accept 3 arguments: item, attrs, metadata
  • Use layout/1 callback instead of the layout: option (avoids compile-time dependencies)
  • Always build on the incoming query in item_query/3

Keep looking

Skills are one crate of 627,801. Ordering is by how many stacks a row turns up in, so the top of any crate is what has actually been picked rather than what has the most stars.