models

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package models holds the integration database's models, written the way an application would: BaseModel for the instance API, relations declared as typed fields.

Index

Constants

This section is empty.

Variables

View Source
var CommentResource = orm.NewResource(func(c *Comment, r *orm.R) {
	r.Set("id", c.ID)
	r.Set("body", c.Body)
})

CommentResource is the API shape of a comment.

View Source
var PostResource = orm.NewResource(func(p *Post, r *orm.R) {
	r.Set("id", p.ID)
	r.Set("slug", p.Slug)
	r.Set("title", p.Title)
	r.Set("published", p.Published)
	r.WhenLoaded("Comments", func() (any, error) { return CommentResource.List(p.Comments.Items()) })
})

PostResource is the API shape of a post; comments appear only when they were eager loaded.

View Source
var TokenKey = []byte("0123456789abcdef0123456789abcdef")

TokenKey encrypts users.api_token (AES-256: 32 bytes). Real applications read it from config.

View Source
var UserResource = orm.NewResource(func(u *User, r *orm.R) {
	r.Set("id", u.ID)
	r.Set("name", u.Name)
	r.Set("email", u.Email)
	r.When(u.Role == "admin", "role", u.Role)
	r.WhenLoaded("Posts", func() (any, error) { return PostResource.List(u.Posts.Items()) })
})

UserResource is the API shape of a user: no password, no token, the role only for admins.

Functions

func PostsBy

func PostsBy(userID uint) func(*orm.Query[Post]) *orm.Query[Post]

PostsBy keeps one author's posts (a scope with an argument).

func PublishedPosts

func PublishedPosts(q *orm.Query[Post]) *orm.Query[Post]

PublishedPosts keeps published posts.

func TitleContains

func TitleContains(term string) func(*orm.Query[Post]) *orm.Query[Post]

TitleContains keeps posts whose title contains term.

Types

type Category

type Category struct {
	orm.BaseModel
	ParentID *uint
	Name     string

	Parent   orm.BelongsTo[Category] `orm:"foreignKey:parent_id"`
	Children orm.HasMany[Category]   `orm:"foreignKey:parent_id"`
	Posts    orm.HasMany[Post]
}

type Comment

type Comment struct {
	orm.BaseModel
	UserID          uint
	CommentableType string
	CommentableID   uint
	Body            string

	User        orm.BelongsTo[User]
	Commentable orm.MorphTo `orm:"morph:commentable"`
}

type Country

type Country struct {
	orm.BaseModel
	Code string
	Name string

	Users orm.HasMany[User]
	Posts orm.HasManyThrough[Post, User]
}

type Order

type Order struct {
	orm.BaseModel
	UserID   uint
	Status   string
	Total    float64
	PlacedAt *time.Time

	User  orm.BelongsTo[User]
	Items orm.HasMany[OrderItem]
}

type OrderItem

type OrderItem struct {
	OrderID   uint `gorm:"primaryKey"`
	LineNo    uint `gorm:"primaryKey;autoIncrement:false"`
	ProductID uint
	Quantity  int
	UnitPrice float64

	Order   orm.BelongsTo[Order]
	Product orm.BelongsTo[Product]
}

OrderItem has a composite primary key (order_id, line_no), so it does not embed BaseModel; it still takes part in relations as the related side and as a belongs-to owner.

type Post

type Post struct {
	orm.BaseModel
	UserID     uint
	CategoryID *uint
	Slug       string
	Title      string
	Published  bool
	Archived   bool

	User     orm.BelongsTo[User]
	Category orm.BelongsTo[Category]
	Comments orm.MorphMany[Comment] `orm:"morph:commentable"`
	Tags     orm.MorphToMany[Tag]   `orm:"morph:taggable"`
}

func (Post) GlobalScopes

func (Post) GlobalScopes() []orm.NamedScope

GlobalScopes hides archived posts from every query, like an Eloquent global scope. Switch it off with Query().WithoutGlobalScope("not_archived").

type Product

type Product struct {
	orm.BaseModel
	SKU   string `gorm:"column:sku"`
	Name  string
	Price float64
	Stock int
}

type Profile

type Profile struct {
	orm.BaseModel
	UserID uint
	Bio    *string

	User orm.BelongsTo[User]
}

type Role

type Role struct {
	orm.BaseModel
	Name string

	Users orm.BelongsToMany[User]
}

type Tag

type Tag struct {
	orm.BaseModel
	Name string

	Posts  orm.MorphedByMany[Post]  `orm:"morph:taggable"`
	Videos orm.MorphedByMany[Video] `orm:"morph:taggable"`
}

type User

type User struct {
	orm.SoftDeleteModel
	CountryID uint
	Email     string
	Name      string
	Password  string
	Role      string `gorm:"default:member"`
	APIToken  string
	Birthday  *time.Time
	Settings  orm.JSONColumn[UserSettings]

	Country  orm.BelongsTo[Country]
	Profile  orm.HasOne[Profile]
	Posts    orm.HasMany[Post]
	Videos   orm.HasMany[Video]
	Comments orm.HasMany[Comment]
	Orders   orm.HasMany[Order]
	Roles    orm.BelongsToMany[Role]
}

func (User) Casts

func (User) Casts() map[string]orm.Cast

Casts are the model's mutators and accessors (Eloquent's $casts and Attribute::make).

func (User) Fillable

func (User) Fillable() []string

Fillable is the mass-assignment allow-list (Eloquent's $fillable).

func (User) Hidden

func (User) Hidden() []string

Hidden keeps secrets out of ToMap / JSON (Eloquent's $hidden).

type UserSettings

type UserSettings struct {
	Theme         string `json:"theme"`
	Notifications bool   `json:"notifications"`
}

UserSettings is the typed payload of users.settings.

type Video

type Video struct {
	orm.BaseModel
	UserID  uint
	Title   string
	Seconds int

	User     orm.BelongsTo[User]
	Comments orm.MorphMany[Comment] `orm:"morph:commentable"`
	Tags     orm.MorphToMany[Tag]   `orm:"morph:taggable"`
}

Jump to

Keyboard shortcuts

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