storage

package
v0.0.0-...-5fe9b4e Latest Latest
Warning

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

Go to latest
Published: Apr 28, 2026 License: MIT Imports: 10 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 DefaultSQLitePath = "data/gomodel.db"

DefaultSQLitePath is the default file path for the SQLite database.

Variables

This section is empty.

Functions

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.

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

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns a Config with sensible defaults

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 SQLiteConfig

type SQLiteConfig struct {
	// Path is the database file path (default: data/gomodel.db)
	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.

Jump to

Keyboard shortcuts

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