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 ¶
- Variables
- func PostsBy(userID uint) func(*orm.Query[Post]) *orm.Query[Post]
- func PublishedPosts(q *orm.Query[Post]) *orm.Query[Post]
- func TitleContains(term string) func(*orm.Query[Post]) *orm.Query[Post]
- type Category
- type Comment
- type Country
- type Order
- type OrderItem
- type Post
- type Product
- type Profile
- type Role
- type Tag
- type User
- type UserSettings
- type Video
Constants ¶
This section is empty.
Variables ¶
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.
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.
var TokenKey = []byte("0123456789abcdef0123456789abcdef")
TokenKey encrypts users.api_token (AES-256: 32 bytes). Real applications read it from config.
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 PublishedPosts ¶
PublishedPosts keeps published posts.
Types ¶
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 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 ¶
Casts are the model's mutators and accessors (Eloquent's $casts and Attribute::make).
type UserSettings ¶
UserSettings is the typed payload of users.settings.