agentsclimarketplace

Go database

Skill kabradshaw1/go-standards/skills/go-database

Implement or review the database layer for a Go service following architecture standardsFrom its SKILL.md

Install
npx -y skills add kabradshaw1/go-standards --skill go-database

Assembled from the repository path, not quoted from the project. Check it against their README if it does not work.

2 things to look at

  • no licenseNo license file was found in the repository. Code published without one is not open source by default, so using it at work is a question for whoever answers licensing questions where you are.
  • 0 stars0 stars. Stars are a popularity signal and not a quality one, but at this level it is likely that nobody has read this closely except its author, and you would be relying on your own review.

SKILL.md

6.6 KB, as published. Nobody here has run it

Implement or review the database layer for $ARGUMENTS following the specs below exactly.

Service Architecture — Overview

This document describes the top-level structure for a service in this project. Before building any layer, read this file. Then read the spec file for the specific layer you are implementing.


Directory Structure

Every service follows this layout:

services/<name>/
├── main.go         # Env validation, dependency wiring, server start
├── handler/
│   └── handler.go  # Transport layer
├── service/
│   └── service.go  # Business logic
├── database/
│   └── database.go # Data access
├── client/
│   └── client.go   # Outbound clients to other services or APIs
├── types/
│   └── types.go    # Local interfaces for every dependency
└── Dockerfile

Each directory is its own Go package named after the directory.


Build Order

Implement layers in this order. Each layer depends on the one before it.

  1. types/ — define all interfaces first so every other layer has a contract to depend on
  2. database/ — implement the data access layer against the Database interface
  3. client/ — implement any outbound clients against their interfaces
  4. service/ — implement business logic using only the interfaces from types/
  5. handler/ — implement the transport layer using only the Service interface from types/
  6. main.go — wire all concrete implementations together and start the server

Core Principles

Depend on interfaces, not implementations. Every layer holds its dependencies as interfaces from types/. Nothing is imported directly from sibling packages except in main.go.

Inject all dependencies through constructors. All dependencies are passed into New* functions. Nothing is instantiated inside the service or handler.

One responsibility per layer. Transport logic does not touch the database. Business logic does not build queries. Queries do not make outbound calls.

Crash on bad config, log on recoverable errors. Missing environment variables are fatal at startup. Cache failures and best-effort side effects are logged and swallowed. Anything that prevents a correct response is returned as an error.

Shared infrastructure lives in services/common. Reusable clients, interfaces, and domain models are defined there and imported. Never copy infrastructure code into a service.


Layer Spec — types/types.go

Responsibility

Define the interfaces that every other layer in the service depends on. This file is the contract layer — it describes what each dependency must provide without specifying how.


Rules

  • One interface per dependency: Service, Database, SomeClient, etc.
  • The Service interface must exactly match the public methods implemented on the service struct — the handler depends on it
  • No concrete types, no structs, no implementation details — interfaces only
  • Shared domain models used across multiple services (e.g. common database models, queue message types) live in services/common and are imported here as needed
  • Do not import from sibling packages (handler, service, database, client) — only from services/common and standard libraries

Pattern

package types

import (
    "context"

    commontypes "github.com/kabradshaw1/story/services/common"
    "github.com/kabradshaw1/story/services/common/genproto/<name>"
)

type Service interface {
    MethodOne(ctx context.Context, req *proto.Request) (*proto.Response, error)
    MethodTwo(ctx context.Context, req *proto.Request) (*proto.Response, error)
}

type Database interface {
    FindRecord(ctx context.Context, id int) (*commontypes.SomeModel, error)
    SaveRecord(ctx context.Context, record commontypes.SomeModel) error
}

type SomeClient interface {
    Notify(ctx context.Context, payload SomePayload) error
}

Checklist

  • One interface defined for each dependency the service layer requires
  • Service interface matches the service struct's public methods exactly
  • No concrete types or structs defined here
  • All shared domain types imported from services/common, not redefined

Layer Spec — database/database.go

Responsibility

Execute all data access operations. This is the only layer that communicates with the database.


Rules

  • No business logic — only queries and result mapping
  • A Connect function (or equivalent) accepts a connection string, establishes the connection, and returns a typed *DB struct; it calls log.Fatal on failure
  • Every method accepts context.Context as its first argument and passes it through to the underlying query
  • The service layer never constructs a query directly — all data access goes through this layer
  • Does not call outbound clients or touch the cache

Pattern

package database

type DB struct {
    client *SomeClient
}

func Connect(connectionString string) *DB {
    // connect, fatal on error
    return &DB{client: client}
}

func (d *DB) FindRecord(ctx context.Context, id int) (*commontypes.SomeModel, error) {
    // query using ctx
}

func (d *DB) SaveRecord(ctx context.Context, record commontypes.SomeModel) error {
    // insert using ctx
}

Mutation Authorization

For any mutation scoped to a user, always verify ownership before modifying data:

  1. Fetch the record by ID
  2. Confirm record.User == requestingUser — return a descriptive error if not
  3. Perform the update

Avoiding Extra Round-Trips

After an update query, do not re-fetch the record to return it. Instead, update the already-fetched struct in memory:

// fetch → verify → update → return modified struct
record.Field = newValue
return &record, nil

Error Handling

  • Distinguish "record not found" from other errors and return a clear, specific message for each
  • Wrap all errors with context before returning: fmt.Errorf("failed to find record: %w", err)

Checklist

  • Connect accepts a connection string and calls log.Fatal on failure
  • Every method takes context.Context as its first parameter
  • No business logic, no cache access, no outbound calls
  • User-scoped mutations verify ownership before updating
  • Updates modify the in-memory struct and return it — no redundant re-fetch
  • "Not found" errors are distinguished from other errors
  • All returned errors are wrapped with context

Keep looking

Skills are one crate of 607,533. Ordering is by how many stacks a row turns up in, so the top of any crate is what has actually been picked rather than what has the most stars.