This library is a complementary library for Gorm (v2) which resolves the first available pool passed to it.
go get github.com/partizaans/gormreplicated
Before reading the example it is better to check the gorm official docs.
package main
import (
"gorm.io/driver/mysql"
"gorm.io/gorm"
"gorm.io/plugin/dbresolver"
"github.com/partizaans/gormreplicated"
)
func main() {
db, err := gorm.Open(mysql.Open("db1_dsn"), &gorm.Config{})
db.Use(dbresolver.Register(dbresolver.Config{
// Use `db2`, `db1` as replica options
// It can be as many as replicas you want
// Note that the ordering is important
Replicas: []gorm.Dialector{mysql.Open("db2_dsn"), mysql.Open("db1_dsn")},
Policy: gormreplicated.ReplicaPolicy{},
}, "replicas"))
}