mirror of
https://github.com/SamyRai/tercul-backend.git
synced 2025-12-27 04:01:34 +00:00
Key changes include:
- **Architectural Refactoring (CQRS/DTOs):** Refactored the `work` and `translation` application services to use Data Transfer Objects (DTOs) for query responses. This separates the domain layer from the API layer, improving maintainability and performance.
- **Implemented Core Business Logic:** Implemented the `AnalyzeWork` command, which was previously a stub. This command now performs linguistic analysis on works and translations by calling the analytics service.
- **Dependency Injection Improvements:**
- Refactored the configuration loading in `internal/platform/config/config.go` to use a local `viper` instance, removing the reliance on a global singleton.
- Injected the `analytics.Service` into the `work.Service` to support the `AnalyzeWork` command.
- **Comprehensive Documentation:**
- Created a new root `README.md` with a project overview, setup instructions, and architectural principles.
- Added detailed `README.md` files to key packages (`api`, `analytics`, `auth`, `work`, `db`) to document their purpose and usage.
- **Improved Test Coverage:**
- Added new unit tests for the refactored `work` and `translation` query handlers.
- Added a new test suite for the `translation` queries, which were previously untested.
- Added tests for the new `AnalyzeWork` command.
- Fixed numerous compilation errors in the test suites caused by the refactoring.
2.2 KiB
2.2 KiB
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.