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 ¶
- type Definition
- type Field
- func BigInt(name string) *Field
- func Bool(name string) *Field
- func Date(name string) *Field
- func DateTime(name string) *Field
- func Decimal(name string) *Field
- func Email(name string) *Field
- func Enum(name string, values ...string) *Field
- func Int(name string) *Field
- func JSON(name string) *Field
- func Slug(name string) *Field
- func String(name string) *Field
- func Text(name string) *Field
- func URL(name string) *Field
- func UUID(name string) *Field
- func (f *Field) Default(v interface{}) *Field
- func (f *Field) EnumValues() []string
- func (f *Field) Filterable() *Field
- func (f *Field) Help(s string) *Field
- func (f *Field) Immutable() *Field
- func (f *Field) Index() *Field
- func (f *Field) Label(s string) *Field
- func (f *Field) MaxLen(n int) *Field
- func (f *Field) MinLen(n int) *Field
- func (f *Field) Modifiers() []Modifier
- func (f *Field) Mutability(role string) *Field
- func (f *Field) Name() string
- func (f *Field) Optional() *Field
- func (f *Field) Placeholder(s string) *Field
- func (f *Field) PrimaryKey() *Field
- func (f *Field) Required() *Field
- func (f *Field) Searchable() *Field
- func (f *Field) Sortable() *Field
- func (f *Field) Type() FieldType
- func (f *Field) Unique() *Field
- func (f *Field) Visibility(role string) *Field
- type FieldType
- type Hooks
- type HooksItem
- type JobRef
- type Modifier
- type ModifierType
- type OnDeleteAction
- type Option
- type OptionType
- type PermissionItem
- type RelationType
- type Relationship
- func (r *Relationship) IsOptional() bool
- func (r *Relationship) Name() string
- func (r *Relationship) OnDelete(action OnDeleteAction) *Relationship
- func (r *Relationship) OnDeleteAction() OnDeleteAction
- func (r *Relationship) Optional() *Relationship
- func (r *Relationship) RelType() RelationType
- func (r *Relationship) Table() string
- type SchemaItem
- type TimestampsItem
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) 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 (*Field) EnumValues ¶
EnumValues returns the allowed values for enum fields.
func (*Field) Filterable ¶
Filterable marks the field as filterable in list views.
func (*Field) Mutability ¶
Mutability restricts field mutability to the given role.
func (*Field) Placeholder ¶
Placeholder sets the placeholder text for the field.
func (*Field) PrimaryKey ¶
PrimaryKey marks the field as the primary key.
func (*Field) Searchable ¶
Searchable marks the field as searchable in full-text search.
func (*Field) Visibility ¶
Visibility restricts field visibility to the given role.
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().
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 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).
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 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.