Skip to content

Commit 41896d9

Browse files
authored
chore: update readme
1 parent 9cf259a commit 41896d9

1 file changed

Lines changed: 56 additions & 43 deletions

File tree

README.md

Lines changed: 56 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Excalidraw live [diagram](https://excalidraw.com/#json=DTs8yxHOGF6uLkjnZny4z,RVo
2626

2727
</details>
2828

29-
### Usecases 2: Cross Region Database
29+
### Usecase 2: Cross Region Database
3030

3131
<details open>
3232

@@ -38,7 +38,7 @@ Excalidraw live [diagram](https://excalidraw.com/#json=DTs8yxHOGF6uLkjnZny4z,RVo
3838

3939
</details>
4040

41-
### Usecases 3: Multi-Master (Multi-Primary) Database
41+
### Usecase 3: Multi-Master (Multi-Primary) Database
4242

4343
<details open>
4444

@@ -71,50 +71,63 @@ go get -u github.com/bxcodec/dbresolver/v2
7171
<summary>Click to Expand</summary>
7272

7373
```go
74-
var (
75-
host1 = "localhost"
76-
port1 = 5432
77-
user1 = "postgresrw"
78-
password1 = "<password>"
79-
host2 = "localhost"
80-
port2 = 5433
81-
user2 = "postgresro"
82-
password2 = "<password>"
83-
dbname = "<dbname>"
74+
package main
75+
76+
import (
77+
"context"
78+
"database/sql"
79+
"fmt"
80+
"log"
81+
82+
"github.com/bxcodec/dbresolver/v2"
83+
_ "github.com/lib/pq"
8484
)
85-
// connection string
86-
rwPrimary := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable", host1, port1, user1, password1, dbname)
87-
readOnlyReplica := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable", host2, port2, user2, password2, dbname)
88-
89-
// open database for primary
90-
dbPrimary, err := sql.Open("postgres", rwPrimary)
91-
if err != nil {
92-
log.Print("go error when connecting to the DB")
93-
}
94-
// configure the DBs for other setup eg, tracing, etc
95-
// eg, tracing.Postgres(dbPrimary)
9685

97-
// open database for replica
98-
dbReadOnlyReplica, err := sql.Open("postgres", readOnlyReplica)
99-
if err != nil {
100-
log.Print("go error when connecting to the DB")
86+
func main() {
87+
var (
88+
host1 = "localhost"
89+
port1 = 5432
90+
user1 = "postgresrw"
91+
password1 = "<password>"
92+
host2 = "localhost"
93+
port2 = 5433
94+
user2 = "postgresro"
95+
password2 = "<password>"
96+
dbname = "<dbname>"
97+
)
98+
// connection string
99+
rwPrimary := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable", host1, port1, user1, password1, dbname)
100+
readOnlyReplica := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable", host2, port2, user2, password2, dbname)
101+
102+
// open database for primary
103+
dbPrimary, err := sql.Open("postgres", rwPrimary)
104+
if err != nil {
105+
log.Print("go error when connecting to the DB")
106+
}
107+
// configure the DBs for other setup eg, tracing, etc
108+
// eg, tracing.Postgres(dbPrimary)
109+
110+
// open database for replica
111+
dbReadOnlyReplica, err := sql.Open("postgres", readOnlyReplica)
112+
if err != nil {
113+
log.Print("go error when connecting to the DB")
114+
}
115+
// configure the DBs for other setup eg, tracing, etc
116+
// eg, tracing.Postgres(dbReadOnlyReplica)
117+
118+
connectionDB := dbresolver.New(
119+
dbresolver.WithPrimaryDBs(dbPrimary),
120+
dbresolver.WithReplicaDBs(dbReadOnlyReplica),
121+
dbresolver.WithLoadBalancer(dbresolver.RoundRobinLB))
122+
123+
defer connectionDB.Close()
124+
// now you can use the connection for all DB operation
125+
_, err = connectionDB.ExecContext(context.Background(), "DELETE FROM book WHERE id=$1") // will use primaryDB
126+
if err != nil {
127+
log.Print("go error when executing the query to the DB", err)
128+
}
129+
connectionDB.QueryRowContext(context.Background(), "SELECT * FROM book WHERE id=$1") // will use replicaReadOnlyDB
101130
}
102-
// configure the DBs for other setup eg, tracing, etc
103-
// eg, tracing.Postgres(dbReadOnlyReplica)
104-
105-
connectionDB := dbresolver.New(
106-
dbresolver.WithPrimaryDBs(dbPrimary),
107-
dbresolver.WithReplicaDBs(dbReadOnlyReplica),
108-
dbresolver.WithLoadBalancer(dbresolver.RoundRobinLB))
109-
110-
defer connectionDB.Close()
111-
// now you can use the connection for all DB operation
112-
_, err = connectionDB.ExecContext(context.Background(), "DELETE FROM book WHERE id=$1") // will use primaryDB
113-
if err != nil {
114-
log.Print("go error when executing the query to the DB", err)
115-
}
116-
connectionDB.QueryRowContext(context.Background(), "SELECT * FROM book WHERE id=$1")
117-
118131
```
119132

120133
</details>

0 commit comments

Comments
 (0)