crud

package
v0.1.136 Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package crud generates a resource from a schema.lidza model: the sqlc queries, the Create and Update types, a handlers file with the five typed routes, and the registration in routes.go. The output is ordinary app code, generated once and edited freely.

A resource is signed-in by default: its routes are registered on a group behind auth.Require(). An owned model (schema.Schema.Owner) is scoped to the signed-in user as well: every statement filters by the owner column, create sets it from auth.CurrentUser, the Create and Update types leave it out, and another user's row is a 404. A model marked @public (or generated with --public, which marks it) is neither.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Options

type Options struct {
	// Model is the model name in schema.lidza.
	Model string
	// Module is the app's Go module path.
	Module string
	// Force overwrites an existing handlers file.
	Force bool
	// Public marks the model @public in schema.lidza: its routes are open
	// to visitors and its rows are not scoped to a user.
	Public bool
	// Shared marks the model @shared: routes behind sign-in, rows every
	// signed-in user shares, not scoped to one.
	Shared bool
	// Auth is true when the app enables the auth pack, which a resource
	// that is not public needs.
	Auth bool
	// AppDir is lidza.json's appDir: the directory of the app's package,
	// whose routes.go gets the registration. Empty is the root.
	AppDir string
}

Options for Generate.

type Resource

type Resource struct {
	Schema *schema.Schema
	Model  *schema.Model
	Module string
	// Workspace is true when Owner is a workspace field (schema
	// Schema.Workspace): its value is auth.WorkspaceID(ctx), the routes
	// sit behind auth.RequireWorkspace().
	Workspace bool
	// Owner is the field scoping the rows to the signed-in user, nil for
	// a model that is not owned.
	Owner *schema.Field
}

Resource is one model with the derived names.

func (*Resource) Handlers

func (r *Resource) Handlers() string

Handlers renders handlers/<table>.go.

func (*Resource) Name

func (r *Resource) Name() string

Names.

func (*Resource) Path

func (r *Resource) Path() string

func (*Resource) Plural

func (r *Resource) Plural() string

func (*Resource) SQL

func (r *Resource) SQL() string

SQL renders db/queries/<table>.sql.

type Result

type Result struct {
	Files []string
	// RoutesLine is the registration code, one or two lines, for a
	// routes.go the generator could not edit.
	RoutesLine string
	Registered bool
	// Owner is the owner field of an owned model, "" otherwise; Public and
	// Shared are true for a @public or @shared model. Workspace is true
	// when Owner is the workspace field: rows scoped to the request's
	// workspace (auth.RequireWorkspace), not to the user.
	Owner          string
	Workspace      bool
	Public, Shared bool
	// StaleInputs are the Create and Update types in schema.lidza that
	// still take the owner field (written before the model was scoped);
	// the handlers ignore it, and the field should go.
	StaleInputs []string
}

Result lists what was written.

func Generate

func Generate(root string, opt Options) (*Result, error)

Generate writes the resource into root.

Jump to

Keyboard shortcuts

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