models

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 7, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func All

func All() []interface{}

All returns all model structs for GORM AutoMigrate. Add new models here as they are created.

func OnlyTrashed

func OnlyTrashed(db *gorm.DB) *gorm.DB

OnlyTrashed returns a GORM scope that returns only soft-deleted records.

db.Scopes(models.OnlyTrashed).Find(&users)

func WithTrashed

func WithTrashed(db *gorm.DB) *gorm.DB

WithTrashed returns a GORM scope that includes soft-deleted records.

db.Scopes(models.WithTrashed).Find(&users)

Types

type AuditLog

type AuditLog struct {
	ID        uint      `gorm:"primaryKey" json:"id"`
	UserID    uint      `gorm:"index;not null;default:0" json:"user_id"`
	Action    string    `gorm:"size:50;not null;index" json:"action"`
	ModelType string    `gorm:"size:100;not null;index" json:"model_type"`
	ModelID   uint      `gorm:"not null;index" json:"model_id"`
	OldValues string    `gorm:"type:text" json:"old_values,omitempty"`
	NewValues string    `gorm:"type:text" json:"new_values,omitempty"`
	Metadata  string    `gorm:"type:text" json:"metadata,omitempty"`
	CreatedAt time.Time `json:"created_at"`
}

AuditLog records a single auditable action on a model.

type BaseModel

type BaseModel struct {
	ID        uint           `gorm:"primaryKey" json:"id"`
	CreatedAt time.Time      `json:"created_at"`
	UpdatedAt time.Time      `json:"updated_at"`
	DeletedAt gorm.DeletedAt `gorm:"index" json:"deleted_at,omitempty"`
}

BaseModel provides common fields for all models. Embed this in your model structs to get ID, CreatedAt, UpdatedAt, and soft delete support via DeletedAt.

type Post

type Post struct {
	BaseModel
	Title  string `gorm:"size:255;not null" json:"title"`
	Slug   string `gorm:"size:255;uniqueIndex" json:"slug"`
	Body   string `gorm:"type:text" json:"body"`
	UserID uint   `gorm:"index;not null" json:"user_id"`
	User   User   `gorm:"foreignKey:UserID" json:"user,omitempty"`
}

Post represents a content entry authored by a user.

type User

type User struct {
	BaseModel
	Name            string     `gorm:"size:100;not null" json:"name"`
	Email           string     `gorm:"size:255;uniqueIndex;not null" json:"email"`
	Password        string     `gorm:"size:255;not null" json:"-"`
	Role            string     `gorm:"size:50;default:user" json:"role"`
	Active          bool       `gorm:"default:true" json:"active"`
	TOTPEnabled     bool       `gorm:"default:false" json:"totp_enabled"`
	TOTPSecret      string     `gorm:"size:512" json:"-"`
	TOTPVerifiedAt  *time.Time `json:"totp_verified_at,omitempty"`
	BackupCodesHash string     `gorm:"type:text" json:"-"`
	Posts           []Post     `gorm:"foreignKey:UserID" json:"posts,omitempty"`
}

User represents an application user.

func (*User) BeforeCreate

func (u *User) BeforeCreate(tx *gorm.DB) error

BeforeCreate hashes the password before inserting into the database. Skips if the password is already bcrypt-hashed (starts with "$2a$" or "$2b$").

Jump to

Keyboard shortcuts

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