Skip to content

Commit

Permalink
chore: fix readme (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
bxcodec authored Dec 10, 2022
1 parent dd35dee commit 94a1937
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func ExampleWrapDBs() {
// configure the DBs for other setup eg, tracing, etc
// eg, tracing.Postgres(dbReadOnlyReplica)

connectionDB := dbresolver.NewResolver(
connectionDB := dbresolver.New(
dbresolver.WithPrimaryDBs(dbPrimary),
dbresolver.WithReplicaDBs(dbReadOnlyReplica),
dbresolver.WithLoadBalancer(dbresolver.RoundRobinLB))
Expand Down
70 changes: 70 additions & 0 deletions example_wrap_dbs_mutli_primary_multi_replicas_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package dbresolver_test

import (
"context"
"database/sql"
"fmt"
"log"

"github.com/bxcodec/dbresolver/v2"
_ "github.com/lib/pq"
)

func ExampleNew_multiPrimaryMultiReplicas() {
var (
host1 = "localhost"
port1 = 5432
user1 = "postgresrw"
password1 = "<password>"
host2 = "localhost"
port2 = 5433
user2 = "postgresro"
password2 = "<password>"
dbname = "<dbname>"
)
// connection string
rwPrimary := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable", host1, port1, user1, password1, dbname)
readOnlyReplica := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable", host2, port2, user2, password2, dbname)

// open database for primary
dbPrimary1, err := sql.Open("postgres", rwPrimary)
if err != nil {
log.Print("go error when connecting to the DB")
}
// open database for primary
dbPrimary2, err := sql.Open("postgres", rwPrimary)
if err != nil {
log.Print("go error when connecting to the DB")
}

// configure the DBs for other setup eg, tracing, etc
// eg, tracing.Postgres(dbPrimary)

// open database for replica
dbReadOnlyReplica1, err := sql.Open("postgres", readOnlyReplica)
if err != nil {
log.Print("go error when connecting to the DB")
}
// open database for replica
dbReadOnlyReplica2, err := sql.Open("postgres", readOnlyReplica)
if err != nil {
log.Print("go error when connecting to the DB")
}
// configure the DBs for other setup eg, tracing, etc
// eg, tracing.Postgres(dbReadOnlyReplica)

connectionDB := dbresolver.New(
dbresolver.WithPrimaryDBs(dbPrimary1, dbPrimary2),
dbresolver.WithReplicaDBs(dbReadOnlyReplica1, dbReadOnlyReplica2),
dbresolver.WithLoadBalancer(dbresolver.RoundRobinLB))

// now you can use the connection for all DB operation
_, err = connectionDB.ExecContext(context.Background(), "DELETE FROM book WHERE id=$1") // will use primaryDB
if err != nil {
log.Print("go error when executing the query to the DB", err)
}
_ = connectionDB.QueryRowContext(context.Background(), "SELECT * FROM book WHERE id=$1") // will use replicaReadOnlyDB

// Output:
//
}

0 comments on commit 94a1937

Please sign in to comment.