mirror of
https://github.com/SamyRai/tercul-backend.git
synced 2025-12-27 05:11:34 +00:00
|
Some checks failed
- Updated database models and repositories to replace uint IDs with UUIDs. - Modified test fixtures to generate and use UUIDs for authors, translations, users, and works. - Adjusted mock implementations to align with the new UUID structure. - Ensured all relevant functions and methods are updated to handle UUIDs correctly. - Added necessary imports for UUID handling in various files. |
||
|---|---|---|
| .. | ||
| db.go | ||
| prometheus.go | ||
| README.md | ||
Database Platform Package
This package is responsible for initializing and managing the application's database connection. It provides a centralized and consistent way to configure and access the database.
Architecture Overview
The db package abstracts the underlying database technology (GORM) and provides a simple function, InitDB, to create a new database connection based on the application's configuration.
Key Components
db.go: Contains theInitDBfunction, which is the sole entry point for this package. It takes aconfig.Configobject and a*observability.Metricsinstance, and returns a*gorm.DBconnection pool.
Features
- Centralized Configuration: All database connection settings (host, port, user, password, etc.) are read from the application's central
configobject. - Prometheus Integration: The package integrates with the
gorm-prometheusplugin to expose GORM metrics (query latency, error rates, etc.) to the application's Prometheus instance. - Connection Management: The
InitDBfunction returns a fully configured*gorm.DBobject, which manages the connection pool. TheClosefunction is provided to gracefully close the database connection on application shutdown.
Usage
The InitDB function is called once at the application's startup in cmd/api/main.go. The resulting *gorm.DB object is then passed down to the repository layer.
Example: Initializing the Database
// In cmd/api/main.go
import "tercul/internal/platform/db"
// ...
// Initialize database connection
database, err := db.InitDB(cfg, metrics)
if err != nil {
// handle error
}
defer db.Close(database)
// Pass the 'database' object to the repositories
repos := sql.NewRepositories(database, cfg)
// ...
Dependencies
internal/platform/config: Relies on theConfigstruct for all database connection parameters.internal/observability: Uses theMetricsobject to register GORM's Prometheus metrics.gorm.io/driver/postgres: The underlying database driver for PostgreSQL.gorm.io/gorm: The GORM library.gorm.io/plugin/prometheus: The GORM plugin for Prometheus integration.