home / skills / hoangnguyen0403 / agent-skills-standard / database
This skill enforces Golang database best practices, promoting repository patterns, safe transactions, and proper connection pooling for reliable,
npx playbooks add skill hoangnguyen0403/agent-skills-standard --skill databaseReview the files below or copy the command above to add this skill to your agents.
---
name: Database
description: Standards for database interaction, connection pooling, and repository patterns in Golang.
metadata:
labels: [golang, database, sql, postgres, repository]
triggers:
files: ['internal/adapter/repository/**']
keywords: [database, sql, postgres, gorm, sqlc, pgx]
---
# Golang Database Standards
## **Priority: P0 (CRITICAL)**
## Principles
- **Prefer Raw SQL/Builders over ORMs**: Go structs map well to SQL. ORMs (GORM) can obscure performance. Recommended: `sqlc` (type-safe SQL generation).
- **Repository Pattern**: Abstract DB access behind interfaces in `internal/port/` (e.g., `UserRepository`).
- **Connection Pooling**: Always configure `SetMaxOpenConns`, `SetMaxIdleConns`, `SetConnMaxLifetime`.
- **Transactions**: Logic requiring ACID MUST use transactions. Pass `context.Context` everywhere.
## Drivers
- **PostgreSQL**: `jackc/pgx` (Prefer `pgx/v5` for performance and features).
- **MySQL**: `go-sql-driver/mysql`.
- **SQLite**: `mattn/go-sqlite3` or `modernc.org/sqlite` (pure Go).
## Anti-Patterns
- **Global DB Connection**: Do not use global `var db *sql.DB`. Inject it.
- **Ignoring Context**: Always use `db.QueryContext` or `db.ExecContext` to handle timeouts.
- **Leaking Rows**: ALWAYS `defer rows.Close()` and check `rows.Err()`.
## References
- [Repository Pattern Implementation](references/repository-pattern.md)
- [Connection Tuning](references/connection-tuning.md)
This skill documents standards for database interaction, connection pooling, and repository patterns in Golang. It focuses on practical rules that improve performance, reliability, and testability for services that use SQL databases. The guidance prioritizes raw SQL or builders, clear abstractions, and safe connection management.
The skill inspects and enforces patterns for database access: using type-safe SQL generation or builders instead of heavy ORMs, abstracting data access behind repository interfaces, and configuring database connection pools. It emphasizes passing context.Context through DB calls, using transaction boundaries for ACID operations, and avoiding global mutable DB state. Drivers and anti-patterns are highlighted for PostgreSQL, MySQL, and SQLite.
Why prefer raw SQL over an ORM in Go?
Go structs map cleanly to SQL and raw SQL or generators like sqlc keep queries explicit, predictable, and easier to profile than opaque ORM behavior.
How should I handle transactions across multiple repository calls?
Start the transaction in a higher-level service, pass a transactional context or tx-scoped repository implementation, and ensure commit/rollback on completion or error.