name: repositories-and-data-access description: Implement repository interfaces for data persistence and abstraction over database operations using Bun ORM.
Repositories & Data Access
When to use this skill
- Create data access layer abstractions for entities
- Implement Bun-based repositories for database operations
- Define repository interfaces that services depend on
- Add transaction support (WithTx) to repositories
Key principles
- Interface segregation: One repository interface per domain model
- Bun ORM: Use Bun for type-safe database operations
- Transaction support: Every repository implements
WithTx(tx bun.IDB) - Context-aware: All operations accept
context.Context - No business logic: Repositories only handle CRUD; logic lives in services
Pattern
Repositories are data access only:
- Define interface with CRUD operations (Create, GetByID, Update, Delete)
- Implement with Bun in
bun_*.gofiles - Accept
bun.IDBat construction (not concrete*bun.DB) - Return interfaces from constructors
- Missing records return
nil, nilfor single queries; empty slice for batch queries
Example
See examples/bun_todo_repository.go for:
- ITodoRepository interface definition
- bunTodoRepository Bun implementation
- CRUD and batch query patterns
Common mistakes
- Returning errors for missing records (use nil instead)
- Forgetting
WithTx()method - Putting business logic in repositories
- Not passing context to Scan/Exec
- Repository-to-repository calls instead of service orchestration