modeladmin

package
v0.0.0-...-9c7a04a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 8, 2026 License: EUPL-1.2 Imports: 18 Imported by: 0

Documentation

Overview

Package modeladmin provides a generic, Django-style ModelAdmin for auto-generating CRUD admin views from Bun models.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ColumnValue

func ColumnValue(item any, field string) template.HTML

ColumnValue extracts a display value for a list column from an item.

func ColumnValueFunc

func ColumnValueFunc(t func(string) string) func(any, string) template.HTML

ColumnValueFunc returns a columnValue function that uses the given translator for bool rendering (modeladmin-yes / modeladmin-no).

func FieldValue

func FieldValue(item any, field string) any

FieldValue extracts a field value from a struct by field name. Returns the value as any, or nil if the field is not found.

func PopulateFromForm

func PopulateFromForm[T any](r *http.Request, item *T) error

PopulateFromForm fills a struct's fields from form POST values. Only exported, non-skipped fields are populated.

Types

type ActiveChoice

type ActiveChoice struct {
	Value    string
	Label    string
	URL      string // pre-built URL with this filter applied
	IsActive bool
}

ActiveChoice represents a single filter option with its computed URL.

type ActiveFilter

type ActiveFilter struct {
	Field     string
	Label     string
	Choices   []ActiveChoice
	HasActive bool // true if any choice is selected
}

ActiveFilter holds filter state for rendering in the list template.

type Choice

type Choice struct {
	Value    string
	Label    string
	LabelKey string // i18n key; translated via i18n.T at request time
}

Choice represents a single option in a select field.

type FilterDef

type FilterDef struct {
	Field    string   // database column name
	Label    string   // human-readable label
	LabelKey string   // i18n key for the label; translated via i18n.T at request time
	Type     string   // "select", "bool", "date_range"
	Choices  []Choice // for select filters
}

FilterDef describes a filter available in the admin list view.

type FormField

type FormField struct {
	Name     string   // Go struct field name
	Label    string   // Human-readable label
	Type     string   // "text", "number", "email", "textarea", "select", "checkbox", "date", "hidden"
	Value    any      // Current value
	Required bool     // Whether the field is required
	Choices  []Choice // For select fields
}

FormField describes a single form field derived from struct introspection.

func AutoFields

func AutoFields[T any](item *T) []FormField

AutoFields extracts form fields from a struct using bun and form tags. Fields with form:"-" are skipped. Auto-increment PKs are skipped in create mode (item == nil).

func (FormField) FormName

func (f FormField) FormName() string

FormName returns the HTML form field name for a FormField.

type ItemActions

type ItemActions struct {
	Actions []RenderAction
}

ItemActions holds the actions available for a specific item.

type ModelAdmin

type ModelAdmin[T any] struct {
	// Slug is the URL path segment, e.g. "articles".
	Slug string
	// DisplayName is the singular human-readable name, also used as i18n key, e.g. "Article".
	DisplayName string
	// DisplayPluralName is the plural human-readable name, also used as i18n key, e.g. "Articles".
	DisplayPluralName string
	// DB is the Bun database connection.
	DB *bun.DB
	// Renderer renders list, detail, and form views.
	Renderer Renderer[T]
	// IDFunc extracts the primary key from the request URL.
	// Defaults to chi.URLParam(r, "id").
	IDFunc func(*http.Request) string
	// CanCreate enables the create action. Default: false.
	CanCreate bool
	// CanEdit enables the update action. Default: false.
	CanEdit bool
	// CanDelete enables the delete action. Default: false.
	CanDelete bool
	// ListFields lists struct field names to show in the list view.
	ListFields []string
	// OrderBy is the default ORDER BY clause, e.g. "created_at DESC".
	OrderBy string
	// Relations lists Bun .Relation() names to eager-load.
	Relations []string
	// PageSize is the number of items per page. Default: 25.
	PageSize int
	// SearchFields lists database column names to full-text search across.
	SearchFields []string
	// Filters defines the sidebar filters for the list view.
	Filters []FilterDef
	// SortFields lists database column names that support column sorting.
	SortFields []string
	// RowActions defines custom per-row actions for the list/detail views.
	RowActions []RowAction
	// EmptyMessage is shown when the list has no items. Default: "No items found."
	EmptyMessage string
	// EmptyMessageKey is the i18n key for the empty-list message.
	// Translated via i18n.T at request time.
	EmptyMessageKey string
}

ModelAdmin provides generic CRUD admin views for a Bun model. It is not a burrow.App itself — it's a helper that apps embed to delegate admin route handling.

func (*ModelAdmin[T]) HandleCreate

func (ma *ModelAdmin[T]) HandleCreate(w http.ResponseWriter, r *http.Request) error

HandleCreate processes the create form submission. Exported so apps can mount ModelAdmin create alongside custom handlers.

func (*ModelAdmin[T]) HandleDelete

func (ma *ModelAdmin[T]) HandleDelete(w http.ResponseWriter, r *http.Request) error

HandleDelete deletes an item by ID. Exported so apps can mount ModelAdmin delete alongside custom handlers.

func (*ModelAdmin[T]) HandleDetail

func (ma *ModelAdmin[T]) HandleDetail(w http.ResponseWriter, r *http.Request) error

HandleDetail renders the detail/edit form for an existing item. Exported so apps can mount ModelAdmin detail views alongside custom handlers.

func (*ModelAdmin[T]) HandleList

func (ma *ModelAdmin[T]) HandleList(w http.ResponseWriter, r *http.Request) error

HandleList renders the paginated list view. Exported so apps can mount ModelAdmin list views alongside custom handlers.

func (*ModelAdmin[T]) HandleNew

func (ma *ModelAdmin[T]) HandleNew(w http.ResponseWriter, r *http.Request) error

HandleNew renders the create form. Exported so apps can mount ModelAdmin form views alongside custom handlers.

func (*ModelAdmin[T]) HandleUpdate

func (ma *ModelAdmin[T]) HandleUpdate(w http.ResponseWriter, r *http.Request) error

HandleUpdate processes the edit form submission. Exported so apps can mount ModelAdmin update alongside custom handlers.

func (*ModelAdmin[T]) Routes

func (ma *ModelAdmin[T]) Routes(r chi.Router)

Routes mounts all CRUD routes for this ModelAdmin on the given router. Routes are mounted under /{slug} so the caller should mount this within the /admin route group.

type RenderAction

type RenderAction struct {
	Slug    string
	Label   string
	Icon    template.HTML
	Method  string
	Class   string
	Confirm string
}

RenderAction holds action metadata for template rendering (no handler/ShowWhen).

type RenderConfig

type RenderConfig struct {
	Slug              string
	DisplayName       string
	DisplayPluralName string
	CanCreate         bool
	CanEdit           bool
	CanDelete         bool
	ListFields        []string // Go struct field names (for columnValue/fieldValue lookups)
	ListFieldLabels   []string // translated column headers (parallel to ListFields)
	IDField           string   // struct field name for the primary key (default: "ID")
	Filters           []ActiveFilter
	RowActions        []RenderAction
	HasRowActions     bool
	ItemActionSets    [][]RenderAction // per-item action sets, parallel to items (ShowWhen-evaluated)
	EmptyMessage      string
}

RenderConfig holds display metadata passed to the renderer.

type Renderer

type Renderer[T any] interface {
	List(w http.ResponseWriter, r *http.Request, items []T, page burrow.PageResult, cfg RenderConfig) error
	Detail(w http.ResponseWriter, r *http.Request, item *T, cfg RenderConfig) error
	Form(w http.ResponseWriter, r *http.Request, item *T, fields []FormField, errors *burrow.ValidationError, cfg RenderConfig) error
	ConfirmDelete(w http.ResponseWriter, r *http.Request, item *T, cfg RenderConfig) error
}

Renderer defines how ModelAdmin views are rendered.

type RowAction

type RowAction struct {
	Slug     string              // URL segment: /admin/{model}/{id}/{slug}
	Label    string              // button text
	Icon     template.HTML       // icon HTML (optional)
	Method   string              // "POST" or "DELETE" (default: "POST")
	Class    string              // CSS class (default: "btn-outline-secondary")
	Confirm  string              // hx-confirm text (empty = no confirm)
	Handler  burrow.HandlerFunc  // the actual handler
	ShowWhen func(item any) bool // nil = always show
}

RowAction defines a custom per-row action in the admin list/detail views.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL