skills/go-repository-pattern/SKILL.md
Cloud-agnostic persistence layer with sqlc for type-safe SQL, repository interfaces, in-memory implementation, and migration conventions. Trigger: When implementing data persistence, creating repositories, writing migrations, or setting up database connections.
npx skillsauth add 333-333-333/agents go-repository-patternInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
| Pattern | Rule |
|---------|------|
| Interface in domain | Repository interface lives in domain/port.go |
| sqlc for PostgreSQL | Write SQL, generate type-safe Go code — no ORMs, no manual scanning |
| In-memory for local/tests | Pure Go map implementation for zero-dep development |
| Migrations versioned | Use golang-migrate with sequential numbered files |
| Each service owns its DB | No shared databases between microservices |
| Context everywhere | All repository methods accept context.Context as first parameter |
| Domain ≠ DB model | sqlc generates DB structs; map them to domain entities in the adapter |
internal/{domain}/
domain/
port.go # Repository interface (what the domain needs)
infrastructure/
repository/
query.sql # SQL queries with sqlc annotations
sqlc.yaml # sqlc configuration
db/ # Generated by sqlc — DO NOT EDIT
models.go # DB structs
query.sql.go # Type-safe query functions
db.go # DBTX interface
postgres.go # Adapter: implements domain port using sqlc
memory.go # In-memory implementation
See assets/port.go
See assets/sqlc.yaml
See assets/query.sql
sqlc generates:
See assets/models_generated.go
See assets/query_generated.go
See assets/postgres.go
See assets/memory.go
| Context | Implementation | Why | |---------|---------------|-----| | Unit tests | In-memory | Zero deps, instant, tests domain logic | | Local dev without Docker | In-memory | No Postgres needed, fast startup | | Local dev with Docker | PostgreSQL (sqlc) | Validate real SQL | | Integration tests | PostgreSQL via testcontainers | Validate real SQL in CI | | dev / staging / production | PostgreSQL (sqlc) | Real persistence |
See assets/wiring.go
See assets/database.go
Use golang-migrate:
migrations/
000001_create_users.up.sql
000001_create_users.down.sql
See assets/migration_up.sql
See assets/migration_down.sql
See assets/transactions.go
# Install sqlc CLI
brew install sqlc
# Generate code from SQL
cd internal/{domain}/infrastructure/repository && sqlc generate
# Verify generated code
go build ./...
# Install pgx driver
go get github.com/jackc/pgx/v5
go get github.com/jackc/pgx/v5/pgxpool
# Install migrate CLI
brew install golang-migrate
# Create migration
migrate create -ext sql -dir migrations -seq create_users
# Run migrations
migrate -path migrations -database "postgres://user:pass@localhost:5432/dbname?sslmode=disable" up
# Rollback
migrate -path migrations -database "..." down 1
See assets/Makefile
| Don't | Do |
|----------|-------|
| Edit generated db/ files | Edit query.sql and run sqlc generate |
| Import sqlc/pgx in domain | Domain defines interface, infra implements with sqlc |
| Use GORM or other ORMs | Use sqlc — you write SQL, it generates type-safe Go |
| Use sqlx for new code | Use sqlc + pgx — better type safety, less boilerplate |
| Shared database between services | Each service owns its schema |
| Return pgx.ErrNoRows to application | Map to domain error like ErrUserNotFound |
| Use sqlc structs as domain entities | Map sqlc structs to domain entities in the adapter |
| Skip in-memory implementation | Always provide in-memory for fast local dev + unit tests |
testing
Review Flutter components and screens for UX/UI compliance. Trigger: When user invokes /ux-review command or requests UX audit.
development
TypeScript strict patterns and best practices. Trigger: When implementing or refactoring TypeScript in .ts/.tsx (types, interfaces, generics, const maps, type guards, removing any, tightening unknown).
testing
Testing philosophy and strategy for every feature: test pyramid, mandatory levels per change type, completion checklist, and skill delegation. Trigger: When planning tests for a feature, reviewing test coverage, defining acceptance criteria, or asking what tests a change needs.
development
Terraform security practices: sensitive variables, secret management, state protection, .gitignore patterns, and CI/CD credential handling. Trigger: When handling secrets in Terraform, configuring state backends, reviewing .gitignore for Terraform, or setting up CI/CD pipelines for infrastructure.