Documentation
¶
Overview ¶
Package storage provides shared database connections for all features. This abstraction allows multiple features (audit logging, IAM, guardrails) to share a single database connection.
Index ¶
- Constants
- func DefaultSQLitePath() string
- func MongoUnexpiredFilter(id string, now time.Time) bson.M
- func ResolveBackend[T any](store Storage, sqlite func(*sql.DB) (T, error), ...) (T, error)
- func RunCleanupLoop(stop <-chan struct{}, interval time.Duration, cleanupFn func())
- func UnixOrZero(t time.Time) int64
- func UnixTime(sec int64) time.Time
- type Config
- type HealthChecker
- type MongoDBConfig
- type MongoDBStorage
- type PostgreSQLConfig
- type PostgreSQLStorage
- type RowScanner
- type SQLiteConfig
- type SQLiteStorage
- type Storage
Constants ¶
const ( TypeSQLite = "sqlite" TypePostgreSQL = "postgresql" TypeMongoDB = "mongodb" )
Type constants for storage backends
const DefaultMongoDatabase = "gomodel"
DefaultMongoDatabase is the database used when neither the explicit Database config nor the connection string names one.
const LegacySQLitePath = "data/gomodel.db"
LegacySQLitePath is the historical default database location, relative to the working directory. It stays the default whenever a ./data directory exists (existing deployments, the Docker image), so upgrades never move anyone's database.
Variables ¶
This section is empty.
Functions ¶
func DefaultSQLitePath ¶
func DefaultSQLitePath() string
DefaultSQLitePath returns the database path used when none is configured: LegacySQLitePath when a ./data directory already exists, otherwise the OS-conventional per-user data directory (see platformdir.DataDir).
func MongoUnexpiredFilter ¶
MongoUnexpiredFilter matches the document with the given id whose expiry is unset or still in the future.
func ResolveBackend ¶
func ResolveBackend[T any]( store Storage, sqlite func(*sql.DB) (T, error), postgresql func(*pgxpool.Pool) (T, error), mongodb func(*mongo.Database) (T, error), ) (T, error)
ResolveBackend dispatches to the callback matching the concrete storage backend.
func RunCleanupLoop ¶
RunCleanupLoop runs a cleanup function immediately and then at the given interval until the stop channel is closed. Store backends use it for periodic retention sweeps.
func UnixOrZero ¶
UnixOrZero converts a time into a unix-seconds retention column value, mapping the zero time to the 0 "never expires" sentinel.
Types ¶
type Config ¶
type Config struct {
// Type specifies the storage backend: "sqlite", "postgresql", or "mongodb"
Type string
// SQLite configuration
SQLite SQLiteConfig
// PostgreSQL configuration
PostgreSQL PostgreSQLConfig
// MongoDB configuration
MongoDB MongoDBConfig
}
Config holds storage configuration
type HealthChecker ¶
HealthChecker is implemented by storage backends that can verify connectivity to the underlying database. All concrete backends satisfy it; readiness checks type-assert against this interface.
type MongoDBConfig ¶
type MongoDBConfig struct {
// URL is the connection string (e.g., mongodb://localhost:27017)
URL string
// Database is the database name (default: gomodel)
Database string
}
MongoDBConfig holds MongoDB-specific configuration
type MongoDBStorage ¶
MongoDBStorage exposes a MongoDB database handle.
func NewMongoDB ¶
func NewMongoDB(ctx context.Context, cfg MongoDBConfig) (MongoDBStorage, error)
NewMongoDB creates a new MongoDB storage connection.
type PostgreSQLConfig ¶
type PostgreSQLConfig struct {
// URL is the connection string (e.g., postgres://user:pass@localhost/dbname)
URL string
// MaxConns is the maximum connection pool size (default: 10)
MaxConns int
}
PostgreSQLConfig holds PostgreSQL-specific configuration
type PostgreSQLStorage ¶
PostgreSQLStorage exposes a PostgreSQL connection pool.
func NewPostgreSQL ¶
func NewPostgreSQL(ctx context.Context, cfg PostgreSQLConfig) (PostgreSQLStorage, error)
NewPostgreSQL creates a new PostgreSQL storage connection. It creates a connection pool for efficient connection reuse.
type RowScanner ¶
RowScanner is the single-row result shape shared by database/sql and pgx.
type SQLiteConfig ¶
type SQLiteConfig struct {
// Path is the database file path (default: DefaultSQLitePath())
Path string
}
SQLiteConfig holds SQLite-specific configuration
type SQLiteStorage ¶
SQLiteStorage exposes a SQLite database handle.
func NewSQLite ¶
func NewSQLite(cfg SQLiteConfig) (SQLiteStorage, error)
NewSQLite creates a new SQLite storage connection. It enables WAL mode for better concurrent read/write performance.