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 MongoSwapSubfields(field string, current, next map[string]string) (filter bson.M, update mongo.Pipeline)
- func MongoUnexpiredFilter(id string, now time.Time) bson.M
- func ResolveBackend[T any](store Storage, sqlite func(*sql.DB) (T, error), ...) (T, error)
- func ResolveSQLBackend[T any](ctx context.Context, store Storage, sqlStore func(sqlx.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.DataFile).
func MongoSwapSubfields ¶ added in v0.1.100
func MongoSwapSubfields(field string, current, next map[string]string) (filter bson.M, update mongo.Pipeline)
MongoSwapSubfields builds a conditional update of string values inside the subdocument at field: the filter expression matches only while every key in current still holds its value there, and the update pipeline sets each of those keys to its value in next. Keys are addressed with $getField and $setField rather than dotted paths, so names containing "." or starting with "$" work too (MongoDB 5.0+). Compare key by key rather than the whole subdocument: a stored subdocument's key order is not stable.
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 ResolveSQLBackend ¶ added in v0.1.60
func ResolveSQLBackend[T any]( ctx context.Context, store Storage, sqlStore func(sqlx.DB) (T, error), mongodb func(*mongo.Database) (T, error), ) (T, error)
ResolveSQLBackend dispatches a storage backend to one of two constructors: a single SQL one serving both SQLite and PostgreSQL through sqlx.DB, and a MongoDB one.
It replaces ResolveBackend for stores that have been unified. ResolveBackend remains for the readers and stores that still branch per SQL dialect.
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.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package mongotest runs a store's test suite against MongoDB.
|
Package mongotest runs a store's test suite against MongoDB. |
|
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. |
|
Package sqlx lets one store implementation serve both SQL backends.
|
Package sqlx lets one store implementation serve both SQL backends. |
|
sqlxtest
Package sqlxtest runs a store's test suite against every SQL dialect.
|
Package sqlxtest runs a store's test suite against every SQL dialect. |