Database Module
A database abstraction interface for GORM, providing a common interface for different database connectors.
Features
- Common interface for database operations
- GORM integration
- Swappable database backends (PostgreSQL, SQLite, etc.)
Installation
go get github.com/weedbox/common-modules/database
Interface
type DatabaseConnector interface {
GetDB() *gorm.DB
}
Available Implementations
Usage
Injecting Database Connector
package repository
import (
"github.com/weedbox/common-modules/database"
"go.uber.org/fx"
)
type Params struct {
fx.In
Database database.DatabaseConnector
}
func (r *Repository) GetUsers() ([]User, error) {
var users []User
err := r.params.Database.GetDB().Find(&users).Error
return users, err
}
Switching Database Backends
The DatabaseConnector interface allows you to switch between different database implementations without changing your application code:
package main
import (
"github.com/weedbox/common-modules/postgres_connector"
// or
// "github.com/weedbox/common-modules/sqlite_connector"
"go.uber.org/fx"
)
func main() {
fx.New(
// Use PostgreSQL
postgres_connector.Module("database"),
// Or use SQLite
// sqlite_connector.Module("database"),
// Your application modules that depend on database.DatabaseConnector
// will work with either implementation
).Run()
}
License
Apache License 2.0