Database

Define Golang database interaction standards with raw SQL and repository patterns.

51|6|Updated Mar 28, 2019
One-click install
npx skills add https://github.com/Mte90/dotfiles --skill database-mte90
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: Database
Source: https://github.com/Mte90/dotfiles/tree/main/.config/opencode/skills/golang/database
Command: npx skills add https://github.com/Mte90/dotfiles --skill database-mte90

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

This Skill establishes best practices for interacting with databases in Golang, ensuring efficient, secure, and maintainable code.

Core Features & Use Cases

  • Type-Safe SQL: Promotes the use of sqlc for generating type-safe SQL queries, reducing runtime errors.
  • Repository Pattern: Enforces the abstraction of database access logic through interfaces, improving code organization and testability.
  • Connection Management: Guides on proper configuration of database connection pools to optimize performance and resource utilization.
  • Use Case: When developing a new Golang microservice that requires user data storage, follow these standards to implement a robust and scalable user repository using PostgreSQL and pgx.

Quick Start

Implement the repository pattern for user data access in Golang using PostgreSQL and pgx.

Frequently Asked Questions about Database

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I implement the repository pattern for PostgreSQL in Golang?

To implement the repository pattern in Golang for PostgreSQL, abstract database access logic using interfaces and the `pgx` driver, ensuring code organization and testability. This approach prevents global DB connections and improves maintainability.

What's the best way to write type-safe SQL queries in Golang?

The best way to generate type-safe SQL queries in Golang is using `sqlc`. It compiles SQL into native Go code, reducing runtime errors while adhering to the standard of preferring raw SQL or query builders over ORMs.

Why should I avoid ORMs for Golang database interaction?

You should avoid ORMs in Golang database interaction because raw SQL and builders provide better performance and control. Preferring raw SQL avoids ORM abstraction leaks and aligns with Go's explicit error handling paradigms.

How do I configure connection pooling with pgx in Golang?

Configure connection pooling in Golang with `pgx` by tuning pool parameters like maximum connections and idle timeouts. Proper connection tuning optimizes performance and prevents resource exhaustion in microservices.

What are common anti-patterns when managing database connections in Go?

Common database connection anti-patterns in Go include using global DB connections and ignoring context. These practices lead to unmanageable state and unresponsive requests, making connection pooling and context-aware interfaces essential.

Does the Golang repository pattern require specific PostgreSQL drivers?

Yes, implementing the repository pattern for PostgreSQL in Golang specifies the `jackc/pgx` driver. It provides robust connection pooling and context-aware execution, ensuring efficient database interaction without ORM overhead.