Agent skill
gorm-repository
GORM ORM, SQLite, and repository patterns
Install this agent skill to your Project
npx add-skill https://github.com/majiayu000/claude-skill-registry/tree/main/skills/other/other/gorm-repository
SKILL.md
Skill: gorm-repository
What I do
I provide GORM repository expertise: model definitions, CRUD operations through the repository pattern, migrations, associations, query scopes, and SQLite-specific patterns for Go applications. I ensure maintainable data access layers by abstracting GORM behind clean interfaces and leveraging advanced ORM features.
When to use me
- Building Go applications with SQL databases (especially SQLite)
- Implementing the repository pattern over GORM ORM
- Defining GORM models with complex tags, constraints, and associations
- Writing reusable queries using chainable scopes and preloading
- Managing database migrations and soft deletes
- Implementing transactions for multi-step data consistency
- Performing complex queries with the GORM query builder or raw SQL
Core principles
- Repository Pattern - Abstract GORM implementation details behind domain-layer interfaces for testability and isolation.
- Model-Driven Design - Use struct tags to define schemas, constraints, and indices; follow GORM naming conventions.
- Query Optimisation - Prevent N+1 query problems using
PreloadandJoins; useSelectfor specific column fetching. - Transaction Consistency - Wrap all multi-step, related operations in
db.Transactionto ensure atomicity. - Typed Error Mapping - Check for GORM errors (e.g.,
gorm.ErrRecordNotFound) and map them to domain-specific errors.
Patterns & examples
Repository Interface & Implementation
type UserRepository interface {
FindByID(ctx context.Context, id string) (*User, error)
Create(ctx context.Context, user *User) error
}
type gormUserRepo struct { db *gorm.DB }
func (r *gormUserRepo) FindByID(ctx context.Context, id string) (*User, error) {
var user User
err := r.db.WithContext(ctx).Preload("Profile").First(&user, "id = ?", id).Error
if errors.Is(err, gorm.ErrRecordNotFound) { return nil, ErrUserNotFound }
return &user, err
}
Advanced Model & Scopes
type User struct {
gorm.Model
Email string `gorm:"uniqueIndex;not null"`
Active bool `gorm:"default:true;index"`
}
func IsActive(db *gorm.DB) *gorm.DB {
return db.Where("active = ?", true)
}
// Usage: db.Scopes(IsActive).Find(&users)
Transaction Pattern
err := db.Transaction(func(tx *gorm.DB) error {
if err := tx.Create(&order).Error; err != nil { return err }
return tx.Model(&user).Update("balance", gorm.Expr("balance - ?", total)).Error
})
Anti-patterns to avoid
- ❌ Leaking
*gorm.DBdirectly into service layers; always use an interface. - ❌ N+1 query problem by iterating and querying; use
Preload. - ❌ Ignoring database-level errors; always check
.Errorand useerrors.Is. - ❌ Missing indexes on frequently queried columns or foreign keys.
- ❌ Using
AutoMigratefor production environments; prefer versioned migrations.
KB Reference
~/vaults/baphled/3. Resources/Knowledge Base/AI Development System/Skills/Database-Persistence/GORM Repository.md
Related skills
db-operations- General database and transaction patternssql- SQL query optimisation and best practicesmigration-strategies- Safe schema evolution workflowserror-handling- Domain error mapping patternsarchitecture- Layer separation with repository pattern code
Recommended Agent Skills
Expand your agent's capabilities with these related and highly-rated skills.
agent-ops-spec
Manage specification documents in .agent/specs/. Use when user provides requirements, acceptance criteria, or feature descriptions that need to be tracked and validated against implementation.
agent-ops-state
Maintain .agent state files. Use at session start, after meaningful steps, and before concluding: read/update constitution/memory/focus/issues/baseline consistently.
agent-ops-spec
Manage specification documents in .agent/specs/. Use when user provides requirements, acceptance criteria, or feature descriptions that need to be tracked and validated against implementation.
agent-ops-testing
Test strategy, execution, and coverage analysis. Use when designing tests, running test suites, or analyzing test results beyond baseline checks.
agent-ops-testing
Test strategy, execution, and coverage analysis. Use when designing tests, running test suites, or analyzing test results beyond baseline checks.
agent-ops-state
Maintain .agent state files. Use at session start, after meaningful steps, and before concluding: read/update constitution/memory/focus/issues/baseline consistently.
Didn't find tool you were looking for?