Documentation
¶
Overview ¶
Package db provides a shared storage abstraction with support for multiple backends (memory, redis) and per-service isolated views.
Package db provides a shared storage abstraction with support for multiple backends (memory, redis, and external drivers) and per-service isolated views.
Index ¶
- func Drivers() []string
- func FlattenHeaders(h http.Header) []string
- func MaskHeaderValues(headers []string, maskHeaders []string)
- func ParseOptions[T any](options map[string]any) (*T, error)
- func Register(name string, factory StorageFactory)
- type DB
- type HistoryEntry
- type HistoryRequest
- type HistoryResponse
- type HistoryTable
- type Storage
- type StorageFactory
- type Table
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Drivers ¶ added in v2.1.91
func Drivers() []string
Drivers returns a sorted list of registered driver names.
func FlattenHeaders ¶
FlattenHeaders converts http.Header to a sorted slice of "Key: value" strings.
func MaskHeaderValues ¶ added in v2.1.71
MaskHeaderValues masks values of headers matching the patterns in maskHeaders, replacing all but the last 4 characters with asterisks. Patterns are matched case-insensitively. A trailing "*" matches any header with that prefix (e.g. "X-Internal-*" matches "X-Internal-Token"). The input slice is modified in place.
func ParseOptions ¶ added in v2.1.91
ParseOptions decodes a raw options map into a typed configuration struct. The target type T should have `yaml` struct tags matching the option keys.
Example:
type MyDriverConfig struct {
Host string `yaml:"host"`
Port int `yaml:"port"`
}
cfg, err := db.ParseOptions[MyDriverConfig](options)
func Register ¶ added in v2.1.91
func Register(name string, factory StorageFactory)
Register makes a storage backend available by the provided name. If Register is called twice with the same name or if factory is nil, it panics.
Built-in drivers register themselves via init(). External drivers can register in their own init() — users activate them with a blank import:
import _ "github.com/someone/connexions-db-dynamodb"
Types ¶
type DB ¶
type DB interface {
// History returns the history table with typed methods for request/response tracking.
History() HistoryTable
// Table returns a generic key-value table by name.
// Tables are created lazily on first access.
Table(name string) Table
// Close releases any resources held by the database.
Close()
}
DB is a per-service database that provides access to named tables. Each service gets its own isolated DB instance.
type HistoryEntry ¶
type HistoryEntry struct {
ID string `json:"id"`
Resource string `json:"resource"`
Response *HistoryResponse `json:"response,omitempty"`
Request *HistoryRequest `json:"request,omitempty"`
CreatedAt time.Time `json:"createdAt"`
}
HistoryEntry represents a recorded request in the history. ID is a unique identifier for this entry Resource is the openapi resource path, i.e. /pets, /pets/{id} Response is the response if present Request is the method, URL, body, headers, and remote address of the original request
type HistoryRequest ¶
type HistoryRequest struct {
Method string `json:"method"`
URL string `json:"url"`
Body []byte `json:"body,omitempty"`
Headers []string `json:"headers,omitempty"`
RemoteAddr string `json:"remoteAddr,omitempty"`
RequestID string `json:"requestId,omitempty"`
}
HistoryRequest represents the HTTP request stored in a history entry.
type HistoryResponse ¶
type HistoryResponse struct {
Body []byte `json:"body"`
StatusCode int `json:"statusCode"`
ContentType string `json:"contentType"`
IsFromUpstream bool `json:"isFromUpstream"`
UpstreamURL string `json:"upstreamURL"`
Headers []string `json:"headers,omitempty"`
Duration time.Duration `json:"duration,omitempty"`
UpstreamError string `json:"upstreamError,omitempty"`
}
HistoryResponse represents the response that was generated or received from the server. Body is the response body StatusCode is the HTTP status code returned ContentType is the Content-Type header of the response IsFromUpstream is true if the response was received from the upstream server UpstreamURL is the URL that was actually sent to the upstream service Duration is the time taken to produce the response
type HistoryTable ¶
type HistoryTable interface {
// Get retrieves the latest request record matching the HTTP request's method and URL.
Get(ctx context.Context, req *http.Request) (*HistoryEntry, bool)
// Set stores a request record with a unique ID.
Set(ctx context.Context, resource string, req *HistoryRequest, response *HistoryResponse) *HistoryEntry
// SetResponse updates the response for the latest request record matching the request's method and URL.
SetResponse(ctx context.Context, req *HistoryRequest, response *HistoryResponse)
// GetByID retrieves a single history entry by its ID.
GetByID(ctx context.Context, id string) (*HistoryEntry, bool)
// Data returns all request records as an ordered log.
Data(ctx context.Context) []*HistoryEntry
// Len returns the number of history entries.
Len(ctx context.Context) int
// Clear removes all history records.
Clear(ctx context.Context)
}
HistoryTable provides typed access to request/response history.
type Storage ¶
type Storage interface {
// NewDB returns a DB scoped to a specific service.
// The returned DB shares the underlying storage but isolates data via key prefixing.
NewDB(serviceName string, historyDuration time.Duration) DB
// Close releases any resources held by the storage backend.
Close()
}
Storage is the shared storage backend that can provide per-service DB instances. There should be only one Storage instance per application.
func NewStorage ¶
func NewStorage(storageCfg *config.StorageConfig) Storage
NewStorage creates a shared storage backend based on configuration. If storageCfg is nil or type is memory, returns an in-memory storage. For other types, the corresponding driver must be registered via Register.
type StorageFactory ¶ added in v2.1.91
StorageFactory creates a Storage backend from raw driver options. Options are typically parsed from the "options" key in storage config YAML. Use ParseOptions to decode them into a typed struct.
type Table ¶
type Table interface {
// Get retrieves a value by key.
Get(ctx context.Context, key string) (any, bool)
// Set stores a value with the given key.
// If ttl is 0, the value never expires.
// If ttl > 0, the value expires after the given duration.
Set(ctx context.Context, key string, value any, ttl time.Duration)
// Delete removes a value by key.
Delete(ctx context.Context, key string)
// Data returns a copy of all data in the table.
Data(ctx context.Context) map[string]any
// Clear removes all data from the table.
Clear(ctx context.Context)
}
Table is a generic key-value store with optional TTL support.