package main import ( "fmt" "github.com/jinzhu/gorm" _ "github.com/jinzhu/gorm/dialects/sqlite" ) // Define the Box model type Box struct { gorm.Model Name string `json:"name"` } // Define the Tag model type Tag struct { gorm.Model Name string `json:"name" gorm:"uniqueIndex"` Description string `json:"description"` Color string `json:"color" gorm:"default:'#808080'"` // Hex color code Items []Item `gorm:"many2many:item_tags;"` } // ItemTag represents the many-to-many relationship between Items and Tags type ItemTag struct { ItemID uint `gorm:"primaryKey"` TagID uint `gorm:"primaryKey"` } // Define the Item model type Item struct { gorm.Model Name string `json:"name"` Description string `json:"description"` BoxID uint `json:"box_id"` ImagePath string `json:"image_path"` Tags []Tag `json:"tags" gorm:"many2many:item_tags;"` } // Define the User model type User struct { gorm.Model Username string `json:"username"` Password string `json:"password"` Email string `json:"email"` } func ConnectDB(dbPath string) (*gorm.DB, error) { db, err := gorm.Open("sqlite3", dbPath) if err != nil { return nil, fmt.Errorf("failed to connect to database: %v", err) } // set auto_vacuum mode to ON // this automagically removes old rows from the database when idle db.Exec("PRAGMA auto_vacuum = ON;") // AutoMigrate will create the tables if they don't exist db.AutoMigrate(&Box{}, &Item{}, &User{}, &Tag{}, &ItemTag{}) return db, nil }