schema

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 23, 2026 License: MIT Imports: 0 Imported by: 0

Documentation

Overview

Package schema provides the DSL for defining resource schemas. This is the public API surface used by developers in resources/*/schema.go files.

Schemas defined using this package are parseable by go/ast without requiring the gen/ directory to exist, supporting Forge's bootstrapping approach.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Definition

type Definition struct {
	// contains filtered or unexported fields
}

Definition represents a complete resource schema definition.

func Define

func Define(name string, items ...SchemaItem) *Definition

Define creates a new schema definition with the given name and items. Items can include fields, relationships, options, and the timestamps marker. All items implement the SchemaItem interface.

This function exists so developer code compiles, but the AST parser extracts definitions statically — Define() is never called at parse time.

func (*Definition) Fields

func (d *Definition) Fields() []*Field

Fields returns all fields in the definition.

func (*Definition) HasTimestamps

func (d *Definition) HasTimestamps() bool

HasTimestamps returns whether the definition includes timestamps.

func (*Definition) Name

func (d *Definition) Name() string

Name returns the resource name.

func (*Definition) Options

func (d *Definition) Options() []*Option

Options returns all options in the definition.

func (*Definition) Relationships

func (d *Definition) Relationships() []*Relationship

Relationships returns all relationships in the definition.

type Field

type Field struct {
	// contains filtered or unexported fields
}

Field represents a field in a schema definition.

func BigInt

func BigInt(name string) *Field

BigInt creates a big integer field with the given name.

func Bool

func Bool(name string) *Field

Bool creates a boolean field with the given name.

func Date

func Date(name string) *Field

Date creates a date field with the given name.

func DateTime

func DateTime(name string) *Field

DateTime creates a date-time field with the given name.

func Decimal

func Decimal(name string) *Field

Decimal creates a decimal field with the given name.

func Email

func Email(name string) *Field

Email creates an email field with the given name.

func Enum

func Enum(name string, values ...string) *Field

Enum creates an enum field with the given name and allowed values.

func Int

func Int(name string) *Field

Int creates an integer field with the given name.

func JSON

func JSON(name string) *Field

JSON creates a JSON field with the given name.

func Slug

func Slug(name string) *Field

Slug creates a slug field with the given name.

func String

func String(name string) *Field

String creates a string field with the given name.

func Text

func Text(name string) *Field

Text creates a text field with the given name.

func URL

func URL(name string) *Field

URL creates a URL field with the given name.

func UUID

func UUID(name string) *Field

UUID creates a UUID field with the given name.

func (*Field) Default

func (f *Field) Default(v interface{}) *Field

Default sets the default value for the field.

func (*Field) EnumValues

func (f *Field) EnumValues() []string

EnumValues returns the allowed values for enum fields.

func (*Field) Filterable

func (f *Field) Filterable() *Field

Filterable marks the field as filterable in list views.

func (*Field) Help

func (f *Field) Help(s string) *Field

Help sets the help text for the field.

func (*Field) Immutable

func (f *Field) Immutable() *Field

Immutable marks the field as immutable after creation.

func (*Field) Index

func (f *Field) Index() *Field

Index marks the field for database indexing.

func (*Field) Label

func (f *Field) Label(s string) *Field

Label sets the human-readable label for the field.

func (*Field) MaxLen

func (f *Field) MaxLen(n int) *Field

MaxLen sets the maximum length for string fields.

func (*Field) MinLen

func (f *Field) MinLen(n int) *Field

MinLen sets the minimum length for string fields.

func (*Field) Modifiers

func (f *Field) Modifiers() []Modifier

Modifiers returns all modifiers applied to this field.

func (*Field) Mutability

func (f *Field) Mutability(role string) *Field

Mutability restricts field mutability to the given role.

func (*Field) Name

func (f *Field) Name() string

Name returns the field name.

func (*Field) Optional

func (f *Field) Optional() *Field

Optional marks the field as optional (nullable).

func (*Field) Placeholder

func (f *Field) Placeholder(s string) *Field

Placeholder sets the placeholder text for the field.

func (*Field) PrimaryKey

func (f *Field) PrimaryKey() *Field

PrimaryKey marks the field as the primary key.

func (*Field) Required

func (f *Field) Required() *Field

Required marks the field as required (not nullable).

func (*Field) Searchable

func (f *Field) Searchable() *Field

Searchable marks the field as searchable in full-text search.

func (*Field) Sortable

func (f *Field) Sortable() *Field

Sortable marks the field as sortable in list views.

func (*Field) Type

func (f *Field) Type() FieldType

Type returns the field type.

func (*Field) Unique

func (f *Field) Unique() *Field

Unique marks the field as requiring unique values.

func (*Field) Visibility

func (f *Field) Visibility(role string) *Field

Visibility restricts field visibility to the given role.

type FieldType

type FieldType int

FieldType represents the type of a field in a schema.

const (
	TypeUUID FieldType = iota
	TypeString
	TypeText
	TypeInt
	TypeBigInt
	TypeDecimal
	TypeBool
	TypeDateTime
	TypeDate
	TypeEnum
	TypeJSON
	TypeSlug
	TypeEmail
	TypeURL
)

func (FieldType) String

func (ft FieldType) String() string

String returns the string representation of the field type.

type Hooks

type Hooks struct {
	AfterCreate []JobRef // Jobs to enqueue after resource creation
	AfterUpdate []JobRef // Jobs to enqueue after resource update
}

Hooks declares lifecycle job enqueueing for a resource. AfterCreate jobs are enqueued in the same transaction as the CREATE operation. AfterUpdate jobs are enqueued in the same transaction as the UPDATE operation.

type HooksItem

type HooksItem struct {
	Hooks Hooks
}

HooksItem wraps Hooks and implements the SchemaItem interface so it can be passed as a variadic argument to schema.Define().

func WithHooks

func WithHooks(h Hooks) *HooksItem

WithHooks creates a HooksItem that declares lifecycle River job enqueueing for the enclosing resource definition.

Example:

schema.Define("Product",
    schema.WithHooks(schema.Hooks{
        AfterCreate: []schema.JobRef{
            {Kind: "notify_new_product", Queue: "notifications"},
        },
    }),
)

type JobRef

type JobRef struct {
	Kind  string // River job worker kind (e.g., "notify_new_product")
	Queue string // River queue name (e.g., "notifications")
}

JobRef identifies a River job to enqueue after a resource lifecycle event. Kind maps to the job's worker kind string; Queue names the River queue.

Example usage:

schema.WithHooks(schema.Hooks{
    AfterCreate: []schema.JobRef{
        {Kind: "notify_new_product", Queue: "notifications"},
    },
})

type Modifier

type Modifier struct {
	Type  ModifierType
	Value interface{} // Used for MaxLen, MinLen, Default, Label, Placeholder, Help
}

Modifier represents a modification or constraint applied to a field.

type ModifierType

type ModifierType int

ModifierType represents the type of modifier applied to a field.

const (
	ModRequired ModifierType = iota
	ModOptional
	ModMaxLen
	ModMinLen
	ModSortable
	ModFilterable
	ModSearchable
	ModUnique
	ModIndex
	ModDefault
	ModImmutable
	ModLabel
	ModPlaceholder
	ModHelp
	ModPrimaryKey
	ModVisibility // field-level visibility role restriction
	ModMutability // field-level mutability role restriction
)

type OnDeleteAction

type OnDeleteAction int

OnDeleteAction represents the action to take when a referenced record is deleted.

const (
	Cascade OnDeleteAction = iota
	SetNull
	Restrict
	NoAction
)

func (OnDeleteAction) String

func (a OnDeleteAction) String() string

String returns the string representation of the on delete action.

type Option

type Option struct {
	// contains filtered or unexported fields
}

Option represents a resource-level option.

func Auditable

func Auditable() *Option

Auditable enables audit fields (created_by, updated_by, deleted_by).

func Searchable

func Searchable() *Option

Searchable enables full-text search indexing.

func SoftDelete

func SoftDelete() *Option

SoftDelete enables soft-delete functionality (deleted_at timestamp).

func TenantScoped

func TenantScoped() *Option

TenantScoped enables multi-tenancy scoping (tenant_id foreign key).

func (*Option) Type

func (o *Option) Type() OptionType

Type returns the option type.

type OptionType

type OptionType int

OptionType represents the type of resource-level option.

const (
	OptSoftDelete OptionType = iota
	OptAuditable
	OptTenantScoped
	OptSearchable
)

func (OptionType) String

func (ot OptionType) String() string

String returns the string representation of the option type.

type PermissionItem

type PermissionItem struct {
	Operation string   // One of: "list", "read", "create", "update", "delete"
	Roles     []string // Roles allowed to perform the operation
}

PermissionItem represents a resource-level permission rule. It specifies which roles are allowed to perform a given operation.

func Permission

func Permission(operation string, roles ...string) *PermissionItem

Permission creates a resource-level permission rule restricting the given operation to the specified roles.

Example: schema.Permission("list", "admin", "editor")

type RelationType

type RelationType int

RelationType represents the type of relationship between resources.

const (
	RelBelongsTo RelationType = iota
	RelHasMany
	RelHasOne
	RelManyToMany
)

func (RelationType) String

func (rt RelationType) String() string

String returns the string representation of the relationship type.

type Relationship

type Relationship struct {
	// contains filtered or unexported fields
}

Relationship represents a relationship between resources.

func BelongsTo

func BelongsTo(name, table string) *Relationship

BelongsTo creates a belongs-to relationship.

func HasMany

func HasMany(name, table string) *Relationship

HasMany creates a has-many relationship.

func HasOne

func HasOne(name, table string) *Relationship

HasOne creates a has-one relationship.

func ManyToMany

func ManyToMany(name, table string) *Relationship

ManyToMany creates a many-to-many relationship.

func (*Relationship) IsOptional

func (r *Relationship) IsOptional() bool

IsOptional returns whether the relationship is optional.

func (*Relationship) Name

func (r *Relationship) Name() string

Name returns the relationship name.

func (*Relationship) OnDelete

func (r *Relationship) OnDelete(action OnDeleteAction) *Relationship

OnDelete sets the action to take when the referenced record is deleted.

func (*Relationship) OnDeleteAction

func (r *Relationship) OnDeleteAction() OnDeleteAction

OnDeleteAction returns the on delete action.

func (*Relationship) Optional

func (r *Relationship) Optional() *Relationship

Optional marks the relationship as optional (nullable foreign key).

func (*Relationship) RelType

func (r *Relationship) RelType() RelationType

RelType returns the relationship type.

func (*Relationship) Table

func (r *Relationship) Table() string

Table returns the related table name.

type SchemaItem

type SchemaItem interface {
	// contains filtered or unexported methods
}

SchemaItem is the common interface for all items that can be passed to Define(). Fields, relationships, options, and timestamps all implement this interface.

type TimestampsItem

type TimestampsItem struct{}

TimestampsItem represents the Timestamps() marker.

func Timestamps

func Timestamps() *TimestampsItem

Timestamps signals that created_at and updated_at fields should be auto-added.

Jump to

Keyboard shortcuts

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