Packages

contract

The contract package defines the ports — pure Go interfaces that your business logic depends on. No concrete implementations live here, which is what makes the architecture hexagonal.


ORM

default

Use contract.ORM in GORM-based repositories. The adapter/gorm adapter satisfies this interface.

type ORM interface {
    // Session returns *gorm.DB scoped to the context
    Session(ctx context.Context) *gorm.DB
    // Transaction runs fn inside a DB transaction, rolling back on error
    Transaction(ctx context.Context, fn func(tx *gorm.DB) error) error
    Close() error
}

Database

Use contract.Database in your repository structs instead of *pgxpool.Pool. This lets you pass a mock in tests.

type Database interface {
    Query(ctx context.Context, sql string, args ...any) (Rows, error)
    QueryRow(ctx context.Context, sql string, args ...any) Row
    Exec(ctx context.Context, sql string, args ...any) error
    Ping(ctx context.Context) error
    Close()
}

Transactional

Embeds Database and adds BeginTx. Use it for repositories that need transaction support.

type Transactional interface {
    Database
    BeginTx(ctx context.Context) (Tx, error)
}

type Tx interface {
    Query(ctx context.Context, sql string, args ...any) (Rows, error)
    QueryRow(ctx context.Context, sql string, args ...any) Row
    Exec(ctx context.Context, sql string, args ...any) error
    Commit(ctx context.Context) error
    Rollback(ctx context.Context) error
}

Cache

type Cache interface {
    Set(ctx context.Context, key string, value any, ttl time.Duration) error
    Get(ctx context.Context, key string, dest any) error
    Delete(ctx context.Context, key string) error
    Exists(ctx context.Context, key string) (bool, error)
    Close() error
}

Storage

type Storage interface {
    Upload(ctx context.Context, bucket, name string, r io.Reader, size int64, contentType string) error
    Download(ctx context.Context, bucket, name string) (io.ReadCloser, error)
    Delete(ctx context.Context, bucket, name string) error
    URL(ctx context.Context, bucket, name string, expiry time.Duration) (string, error)
}

Token

type Claims map[string]any

type Token interface {
    Sign(claims Claims, expiry time.Duration) (string, error)
    Verify(token string) (Claims, error)
}