Go database
Implement or review the database layer for a Go service following architecture standardsFrom its SKILL.md
npx -y skills add kabradshaw1/go-standards --skill go-databaseAssembled 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.
types/— define all interfaces first so every other layer has a contract to depend ondatabase/— implement the data access layer against theDatabaseinterfaceclient/— implement any outbound clients against their interfacesservice/— implement business logic using only the interfaces fromtypes/handler/— implement the transport layer using only theServiceinterface fromtypes/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
Serviceinterface 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/commonand are imported here as needed - Do not import from sibling packages (
handler,service,database,client) — only fromservices/commonand 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
-
Serviceinterface 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
Connectfunction (or equivalent) accepts a connection string, establishes the connection, and returns a typed*DBstruct; it callslog.Fatalon failure - Every method accepts
context.Contextas 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:
- Fetch the record by ID
- Confirm
record.User == requestingUser— return a descriptive error if not - 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
-
Connectaccepts a connection string and callslog.Fatalon failure - Every method takes
context.Contextas 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