Token导航 LogoToken导航TokenDH.com
研究检索权限需确认github未标认证来源可访问许可证需确认审计通过

go-repository去存储库

Agent Skill

用于围绕 GitHub 仓库、Issue、Pull Request、分支、提交和代码协作流程提供辅助能力。它适合让 Agent 查询项目状态、整理变更、辅助创建或检查协作事项,并把仓库中的信息转成可执行的下一步。使用时需要区分只读查询和写入操作;涉及创建 PR、修改 Issue、推送分支或访问私有仓库时,应确认 token 权限、目标仓库范围和用户授权。

总安装

399

周安装

16

GitHub Stars

公开资料未说明

下载量

129
CodexClaudeCursorGemini CLI

安装说明

本站只整理中文说明和来源信息,不托管安装包,也不代用户安装。

GitHub

来源数

2

许可证

unknown

最后核验

2026-05-01

来源状态

来源可访问

安装方式

通过对话安装

复制提示词发给支持本地命令或 Skills 的 AI 助手,先确认命令和权限,再让它执行。

请帮我安装这个 Agent Skill:go-repository(去存储库)
来源仓库:https://github.com/cristiano-pacheco/ai-tools
仓库路径:skills/go-repository
安装命令:
npx skills add https://github.com/cristiano-pacheco/ai-tools --skill go-repository
安装前请先检查当前环境是否支持对应 CLI,并向我确认将要执行的命令、安装目录、联网范围和文件读写权限;确认后再执行。

命令行安装

复制命令到本机终端执行。该命令会通过 npx skills 从第三方来源获取 Skill;本站只展示命令,不托管安装包,也不自动执行。

skills.shnpx skills
npx skills add https://github.com/cristiano-pacheco/ai-tools --skill go-repository

简介

用于围绕 GitHub 仓库、Issue 和 PR 提供协作辅助能力。

  • 适合查询项目状态、整理变更和创建协作事项等场景。
  • 可把仓库信息转成可执行的操作建议。go-repository 属于研究检索类 Skill,可作为该场景下的辅助能力补充。
  • 涉及写入操作时应确认 token 权限和仓库范围。
  • 安装前建议确认来源仓库维护状态和权限范围。

SKILL.md

Go Repository

Generate repository port interfaces and implementations for Go modular architecture conventions.

When to Use

  • Create data access layers for entities
  • CRUD operations (Create, FindAll, FindByID, Update, Delete)
  • Custom queries, pagination, transactions
  • Join queries and filtered lookups

Two-File Pattern

Every repository requires two files:

  1. Port interface: internal/modules/<module>/ports/<entity>_repository.go
  2. Repository implementation: internal/modules/<module>/repository/<entity>_repository.go

Port Interface Structure

Location: internal/modules/<module>/ports/<entity>_repository.go

package ports

import (
	"context"
	"github.com/cristiano-pacheco/pingo/internal/modules/<module>/model"
)

// EntityRepository defines entity persistence operations.
//
// Add a comprehensive comment here describing the purpose of the repository,
// what domain concept it represents, and any non-obvious behavior.
type EntityRepository interface {
	FindAll(ctx context.Context) ([]model.EntityModel, error)
	FindByID(ctx context.Context, id uint64) (model.EntityModel, error)
	Create(ctx context.Context, entity model.EntityModel) (model.EntityModel, error)
	Update(ctx context.Context, entity model.EntityModel) (model.EntityModel, error)
	Delete(ctx context.Context, id uint64) error
}

Pagination variant:

FindAll(ctx context.Context, page, pageSize int) ([]model.EntityModel, int64, error)

Custom methods: Add domain-specific queries as needed (e.g., FindByName, FindBySKU).

Repository Implementation Structure

Location: internal/modules/<module>/repository/<entity>_repository.go

package repository

import (
	"context"
	"errors"

	brickserrs "github.com/cristiano-pacheco/bricks/pkg/errs"
	"github.com/cristiano-pacheco/bricks/pkg/otel/trace"
	"github.com/cristiano-pacheco/pingo/internal/modules/<module>/model"
	"github.com/cristiano-pacheco/pingo/internal/modules/<module>/ports"
	"github.com/cristiano-pacheco/pingo/internal/shared/database"
	"gorm.io/gorm"
)

type EntityRepository struct {
	*database.PingoDB
}

var _ ports.EntityRepository = (*EntityRepository)(nil)

func NewEntityRepository(db *database.PingoDB) *EntityRepository {
	return &EntityRepository{PingoDB: db}
}
Note: The constructor MUST use named field initialization {PingoDB: db}, not positional {db}.

Method Implementations

FindAll (Simple)

func (r *EntityRepository) FindAll(ctx context.Context) ([]model.EntityModel, error) {
	ctx, span := trace.Span(ctx, "EntityRepository.FindAll")
	defer span.End()

	entities, err := gorm.G[model.EntityModel](r.DB).Find(ctx)
	if err != nil {
		return nil, err
	}
	return entities, nil
}

FindAll (Paginated with dynamic filters)

When you need optional WHERE filters or pagination, fall back to raw GORM — gorm.G does not support dynamic multi-condition builds. Use r.DB.WithContext(ctx).Model(...) for these cases:

func (r *EntityRepository) FindAll(
	ctx context.Context,
	filter dto.EntityFilter,
	paginationParams paginator.Params,
) ([]model.EntityModel, int64, error) {
	ctx, span := trace.Span(ctx, "EntityRepository.FindAll")
	defer span.End()

	baseQuery := r.DB.WithContext(ctx).Model(&model.EntityModel{})
	if filter.Status != "" {
		baseQuery = baseQuery.Where("status = ?", filter.Status)
	}
	if filter.Name != nil && strings.TrimSpace(*filter.Name) != "" {
		baseQuery = baseQuery.Where("name ILIKE ?", "%"+strings.TrimSpace(*filter.Name)+"%")
	}

	var totalCount int64
	if err := baseQuery.Count(&totalCount).Error; err != nil {
		return nil, 0, err
	}

	query := baseQuery.Order("id DESC")
	if paginationParams.Limit() > 0 {
		query = query.Limit(paginationParams.Limit())
	}
	if paginationParams.Offset() > 0 {
		query = query.Offset(paginationParams.Offset())
	}

	results := make([]model.EntityModel, 0)
	if err := query.Find(&results).Error; err != nil {
		return nil, 0, err
	}

	return results, totalCount, nil
}

FindAll (JOIN query)

For queries that require JOINs, also use raw GORM:

func (r *EntityRepository) FindByRelatedID(
	ctx context.Context,
	relatedID uint64,
) ([]model.EntityModel, error) {
	ctx, span := trace.Span(ctx, "EntityRepository.FindByRelatedID")
	defer span.End()

	var results []model.EntityModel
	err := r.DB.WithContext(ctx).
		Model(&model.EntityModel{}).
		Joins("JOIN related_table rt ON rt.entity_id = entities.id").
		Where("rt.related_id = ?", relatedID).
		Order("rt.id ASC").
		Find(&results).Error
	if err != nil {
		return nil, err
	}

	return results, nil
}

FindByID

func (r *EntityRepository) FindByID(ctx context.Context, id uint64) (model.EntityModel, error) {
	ctx, span := trace.Span(ctx, "EntityRepository.FindByID")
	defer span.End()

	entity, err := gorm.G[model.EntityModel](r.DB).
		Where("id = ?", id).
		Limit(1).
		First(ctx)
	if err != nil {
		if errors.Is(err, gorm.ErrRecordNotFound) {
			return model.EntityModel{}, brickserrs.ErrRecordNotFound
		}
		return model.EntityModel{}, err
	}
	return entity, nil
}

Create

func (r *EntityRepository) Create(ctx context.Context, entity model.EntityModel) (model.EntityModel, error) {
	ctx, span := trace.Span(ctx, "EntityRepository.Create")
	defer span.End()

	err := gorm.G[model.EntityModel](r.DB).Create(ctx, &entity)
	return entity, err
}

When the module defines a conflict error, map gorm.ErrDuplicatedKey:

func (r *EntityRepository) Create(ctx context.Context, entity model.EntityModel) (model.EntityModel, error) {
	ctx, span := trace.Span(ctx, "EntityRepository.Create")
	defer span.End()

	err := gorm.G[model.EntityModel](r.DB).Create(ctx, &entity)
	if err != nil {
		if errors.Is(err, gorm.ErrDuplicatedKey) {
			return model.EntityModel{}, errs.ErrEntityNameConflict
		}
		return model.EntityModel{}, err
	}
	return entity, nil
}

Update

For updates where all fields are non-zero, use the gorm.G Updates pattern:

func (r *EntityRepository) Update(ctx context.Context, entity model.EntityModel) (model.EntityModel, error) {
	ctx, span := trace.Span(ctx, "EntityRepository.Update")
	defer span.End()

	rowsAffected, err := gorm.G[model.EntityModel](r.DB).
		Where("id = ?", entity.ID).
		Updates(ctx, entity)
	if err != nil {
		return model.EntityModel{}, err
	}
	if rowsAffected == 0 {
		return model.EntityModel{}, brickserrs.ErrRecordNotFound
	}

	updated, err := gorm.G[model.EntityModel](r.DB).Where("id = ?", entity.ID).Limit(1).First(ctx)
	if err != nil {
		if errors.Is(err, gorm.ErrRecordNotFound) {
			return model.EntityModel{}, brickserrs.ErrRecordNotFound
		}
		return model.EntityModel{}, err
	}
	return updated, nil
}

Update (Zero-Value Fields)

GORM's Updates() skips zero values (false, 0, ""). When any updated field may be zero, use one of two patterns:

Option A — map[string]any (when fields are heterogeneous or sparse):

func (r *EntityRepository) Update(ctx context.Context, entity model.EntityModel) (model.EntityModel, error) {
	ctx, span := trace.Span(ctx, "EntityRepository.Update")
	defer span.End()

	updates := map[string]any{
		"name":      entity.Name,
		"is_active": entity.IsActive, // bool: would be skipped by plain Updates()
		"count":     entity.Count,    // int: would be skipped when 0
	}

	result := r.DB.WithContext(ctx).
		Model(&model.EntityModel{}).
		Where("id = ?", entity.ID).
		Updates(updates)
	if result.Error != nil {
		return model.EntityModel{}, result.Error
	}
	if result.RowsAffected == 0 {
		return model.EntityModel{}, brickserrs.ErrRecordNotFound
	}

	updated, err := gorm.G[model.EntityModel](r.DB).Where("id = ?", entity.ID).Limit(1).First(ctx)
	if err != nil {
		if errors.Is(err, gorm.ErrRecordNotFound) {
			return model.EntityModel{}, brickserrs.ErrRecordNotFound
		}
		return model.EntityModel{}, err
	}
	return updated, nil
}

Option B — Select(fields).Updates(&entity) (when updating a fixed set of columns):

result := r.DB.WithContext(ctx).
	Model(&model.EntityModel{}).
	Where("id = ?", entity.ID).
	Select("name", "slug", "is_active").
	Updates(&entity)

Single-Field Targeted Update

For methods that set one field by ID and return no model (e.g., MarkEmailConfirmed, SetTOTPEnabled), raw GORM is correct — this is intentional, not a deviation:

func (r *EntityRepository) MarkConfirmed(ctx context.Context, id uint64) error {
	ctx, span := trace.Span(ctx, "EntityRepository.MarkConfirmed")
	defer span.End()

	return r.DB.WithContext(ctx).Model(&model.EntityModel{}).
		Where("id = ?", id).
		Update("confirmed", true).Error
}

Delete

func (r *EntityRepository) Delete(ctx context.Context, id uint64) error {
	ctx, span := trace.Span(ctx, "EntityRepository.Delete")
	defer span.End()

	rowsAffected, err := gorm.G[model.EntityModel](r.DB).
		Where("id = ?", id).
		Delete(ctx)
	if err != nil {
		return err
	}
	if rowsAffected == 0 {
		return brickserrs.ErrRecordNotFound
	}
	return nil
}

Bulk Cleanup Delete

For DeleteExpired-style operations, zero rows deleted is not an error — discard rowsAffected:

func (r *EntityRepository) DeleteExpired(ctx context.Context) error {
	ctx, span := trace.Span(ctx, "EntityRepository.DeleteExpired")
	defer span.End()

	_, err := gorm.G[model.EntityModel](r.DB).
		Where("expires_at < ?", time.Now().UTC()).
		Delete(ctx)
	return err
}

Custom Query (by field)

func (r *EntityRepository) FindByName(ctx context.Context, name string) (model.EntityModel, error) {
	ctx, span := trace.Span(ctx, "EntityRepository.FindByName")
	defer span.End()

	entity, err := gorm.G[model.EntityModel](r.DB).
		Where("name = ?", name).
		Limit(1).
		First(ctx)
	if err != nil {
		if errors.Is(err, gorm.ErrRecordNotFound) {
			return model.EntityModel{}, brickserrs.ErrRecordNotFound
		}
		return model.EntityModel{}, err
	}
	return entity, nil
}

Transaction (relationship operations)

func (r *EntityRepository) AssignRelated(ctx context.Context, entityID uint64, relatedIDs []uint64) error {
	ctx, span := trace.Span(ctx, "EntityRepository.AssignRelated")
	defer span.End()

	tx := r.DB.Begin()

	_, err := gorm.G[model.EntityRelationModel](tx).
		Where("entity_id = ?", entityID).
		Delete(ctx)
	if err != nil {
		tx.Rollback()
		return err
	}

	var relations []model.EntityRelationModel
	for _, relatedID := range relatedIDs {
		relations = append(relations, model.EntityRelationModel{
			EntityID:  entityID,
			RelatedID: relatedID,
		})
	}

	err = gorm.G[model.EntityRelationModel](tx).CreateInBatches(ctx, &relations, len(relations))
	if err != nil {
		tx.Rollback()
		return err
	}

	if commitErr := tx.Commit().Error; commitErr != nil {
		return commitErr
	}

	return nil
}

Fx Wiring

Add to internal/modules/<module>/fx.go:

fx.Provide(
	fx.Annotate(
		repository.NewEntityRepository,
		fx.As(new(ports.EntityRepository)),
	),
),

Anti-Patterns (Do NOT Do These)

Missing .Limit(1) before .First() — BAD

// BAD: missing Limit(1) — always add it before First()
entity, err := gorm.G[model.EntityModel](r.DB).
    Where("id = ?", id).
    First(ctx)  // ← wrong
// GOOD
entity, err := gorm.G[model.EntityModel](r.DB).
    Where("id = ?", id).
    Limit(1).   // ← required
    First(ctx)

Wrong span variable name — BAD

// BAD: using 'span' instead of 'span'
ctx, span := trace.Span(ctx, "EntityRepository.FindByID")
defer span.End()
// GOOD
ctx, span := trace.Span(ctx, "EntityRepository.FindByID")
defer span.End()

Redundant method comments — BAD

// BAD: comment that just restates the method name
// FindByID finds an entity by ID.
func (r *EntityRepository) FindByID(ctx context.Context, id uint64) (model.EntityModel, error) {

// BAD: comment that just restates the constructor
// NewEntityRepository creates a new entity repository.
func NewEntityRepository(db *database.PingoDB) *EntityRepository {
// GOOD: no comment on self-evident methods
func (r *EntityRepository) FindByID(ctx context.Context, id uint64) (model.EntityModel, error) {

// GOOD: comment only when behavior needs explanation
// FindByPriority resolves a template using collection+category, then category, then global fallback.
func (r *AIPromptTemplateRepository) FindByPriority(...)

Positional constructor initialization — BAD

// BAD: positional — fragile if struct fields change
return &EntityRepository{db}
// GOOD: named field
return &EntityRepository{PingoDB: db}

Critical Rules

  1. No standalone functions: When a file contains a struct with methods, do not add standalone functions. Use private methods on the struct instead.
  2. Struct: Embed *database.PingoDB only.
  3. Constructor: MUST return pointer *EntityRepository and use named field init: {PingoDB: db}.
  4. Interface assertion: Add var _ ports.EntityRepository = (*EntityRepository)(nil) below the struct.
  5. Tracing: Every method MUST start with ctx, span:= trace.Span(ctx, "Repo.Method") and defer span.End(). Always name the variable span, never span.
  6. .Limit(1) before .First(): Every single-record lookup MUST have .Limit(1) immediately before .First(ctx). No exceptions.
  7. Not found: Return brickserrs.ErrRecordNotFound when errors.Is(err, gorm.ErrRecordNotFound).
  8. Delete rowsAffected: Check rowsAffected == 0 and return brickserrs.ErrRecordNotFound for targeted deletes. For bulk cleanup (DeleteExpired, etc.), discard rowsAffected — zero rows is not an error.
  9. Zero-value updates: Use map[string]any or Select(fields).Updates(&model) when any field may be a zero value (false, 0, ""). Plain Updates(entity) silently skips zero values.
  10. Complex queries: Use gorm.G[Model](r.DB) for simple queries. Fall back to r.DB.WithContext(ctx).Model(...) only when gorm.G is insufficient: dynamic multi-condition WHERE, JOINs, subqueries, or .Select() with raw SQL fragments.
  11. Module-specific errors: Prefer module-defined errors (e.g., errs.ErrEntityNotFound) over the generic brickserrs.ErrRecordNotFound when the module's errs/ package defines them. Map gorm.ErrDuplicatedKey to a module conflict error when one exists.
  12. No redundant method comments: Do not add comments above methods that merely restate the method name (e.g., // FindByID finds an entity by ID.). Only add comments where the logic or behavior is non-obvious.
  13. Comments on interfaces: Port interfaces MUST have a comprehensive doc comment on the type explaining its purpose and any non-obvious behavior.
  14. Validation: Run make lint and make nilaway after generation.

Workflow

  1. Create port interface in ports/<entity>_repository.go
  2. Create repository implementation in repository/<entity>_repository.go
  3. Add Fx wiring to module's fx.go
  4. Run make lint to verify
  5. Run make nilaway for static analysis

适合场景

01

用户想查找某类 Agent Skill 时

02

需要根据任务场景推荐可安装能力包时

03

需要对比不同来源的安装命令和来源信息时

能力概览

能力 1

按任务关键词查找相关 Skills

能力 2

展示可复制的安装命令

能力 3

保留来源站点、仓库和原始说明,方便继续核验

能力 4

展示第三方安全扫描或审计结果

安装后应在对应宿主中按原始 README 的触发条件使用;具体调用方式请以来源页面和 README 为准。

平台分布

Codex

38.12%
按下载量换算49

Claude

27.14%
按下载量换算35

Cursor

19.12%
按下载量换算25

Gemini CLI

10.42%
按下载量换算13

安全审计

Gen Agent Trust Hub

通过

Socket

通过

Snyk

通过

权限和风险

权限需确认

当前来源未能明确判断权限范围,默认进入异常复核队列。

安装前确认

本站仅展示第三方公开信息,不托管安装包,不提供自动安装或运行环境。安装前应自行审查源码、依赖和命令行为。当前只有一个来源,正式发布前建议补源仓库或其他目录站核验。

来源信息

继续浏览同类 Skills