implement-repository

Implement Go repository persistence ports with PostgreSQL, pgx/v5, and sqlc.

Updated Sep 14, 2026
One-click install
npx skills add https://github.com/nakamori-naoya/go-convention-plugins --skill implement-repository-nakamori-naoya
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: implement-repository
Source: https://github.com/nakamori-naoya/go-convention-plugins/tree/main/plugins/go-convention/skills/implement-repository
Command: npx skills add https://github.com/nakamori-naoya/go-convention-plugins --skill implement-repository-nakamori-naoya

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve? It standardizes how Go developers implement the persistence layer for domain aggregates, eliminating ad-hoc decisions about transaction handling, error translation, and domain-to-row mapping when writing repositories against PostgreSQL. ## Core Features & Use Cases - Two persistence patterns: Implements either a standard type (FindByID / Create / Update overwriting state) or an event-sourced type (FindByID plus Apply{Event} appending domain events and reflecting them into a current row with optimistic locking), chosen from the data model document. - Marshaller conventions: Defines pure functions named {source}To{target} that convert between domain values and sqlc rows/Params, restoring aggregates only through New* and Restore* constructors. - Error translation: Maps DB constraint violations to domain sentinels, ErrNoRows to rdb.ErrNotFound, and optimistic-lock conflicts to rdb.ErrConflict, with context added exactly once per method. - Use Case: Given a domain aggregate with a Repository interface and a logical data model document, generate the full repository implementation including sqlc queries, marshallers, and constraint translation, then compile and report. ## Quick Start Implement the repository for this aggregate from the domain-model and data-model documents using PostgreSQL with pgx and sqlc.

Frequently Asked Questions about implement-repository

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

FAQPage Schema
How do I implement a Go repository with sqlc and pgx?▼

Write SQL in a query file per aggregate, run sqlc generate with sql_package pgx/v5, and call sqlcgen.New(tx) directly in each method. The repository takes the pgx.Tx from context and never opens its own transaction.

How to handle optimistic locking in a PostgreSQL repository?▼

Use an UPDATE with WHERE current_version = expected_version via sqlc :execrows, where expected_version is the event version minus one. Zero affected rows translate to a conflict sentinel, and a unique constraint on (aggregate_id, version) is the last line of defense.

Should the repository or usecase manage transactions in Go?▼

The usecase owns the transaction through a tx.Manager that places pgx.Tx on the context. The repository only reads it via tx.From(ctx) and returns an error if none exists, never calling Begin, Commit, or Rollback itself.

How do I map PostgreSQL errors to domain errors in Go?▼

Inspect pgconn.PgError by SQLSTATE and named constraint, then return the domain sentinel matching that constraint from a single translation table. ErrNoRows becomes a NotFound sentinel, and unlisted violations pass through untranslated with one layer of context.

When should I use event-sourced persistence versus state overwrite?▼

Choose event-sourced persistence when the data model document defines base and detail event tables, giving the aggregate a version and Apply{Event} methods. Use plain Create/Update overwrites when only resource tables exist and the aggregate has no version.

What are the limitations of this repository convention?▼

It excludes repository interface design, transaction initiation, read models like lists and search, DDL and migration tooling, and test strategy, each delegated to its own convention. It also refuses to enforce invariants via SELECT checks, requiring DB constraints instead.