storage

package
v0.1.56 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 21, 2026 License: MIT Imports: 15 Imported by: 0

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

View Source
const (
	TypeSQLite     = "sqlite"
	TypePostgreSQL = "postgresql"
	TypeMongoDB    = "mongodb"
)

Type constants for storage backends

View Source
const DefaultMongoDatabase = "gomodel"

DefaultMongoDatabase is the database used when neither the explicit Database config nor the connection string names one.

View Source
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

func MongoUnexpiredFilter(id string, now time.Time) bson.M

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

func RunCleanupLoop(stop <-chan struct{}, interval time.Duration, cleanupFn func())

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

func UnixOrZero(t time.Time) int64

UnixOrZero converts a time into a unix-seconds retention column value, mapping the zero time to the 0 "never expires" sentinel.

func UnixTime

func UnixTime(sec int64) time.Time

UnixTime converts a unix-seconds retention column into a time, mapping the 0 "never expires" sentinel to the zero time.

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

type HealthChecker interface {
	Ping(ctx context.Context) error
}

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

type MongoDBStorage interface {
	Storage
	Database() *mongo.Database
}

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

type PostgreSQLStorage interface {
	Storage
	Pool() *pgxpool.Pool
}

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

type RowScanner interface {
	Scan(dest ...any) error
}

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

type SQLiteStorage interface {
	Storage
	DB() *sql.DB
}

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.

type Storage

type Storage interface {
	Close() error
}

Storage manages the lifecycle of a shared storage backend.

func New

func New(ctx context.Context, cfg Config) (Storage, error)

New creates a new Storage based on the configuration. It validates the configuration and establishes the database connection.

Directories

Path Synopsis
Package sqlutil provides small SQL query-building and value-conversion helpers shared by the SQL-backed stores and readers.
Package sqlutil provides small SQL query-building and value-conversion helpers shared by the SQL-backed stores and readers.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL