Skip to content

Commit ee40a9c

Browse files
committed
upadte documentation
1 parent d37a371 commit ee40a9c

File tree

2 files changed

+24
-12
lines changed

2 files changed

+24
-12
lines changed

docs/quick-start/connecting-mysql/page.md

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ go get gofr.dev/pkg/gofr/datasource/dbresolver@latest
259259

260260
**1. Environment Variables**
261261

262-
Add replica connection details to your .env file:
262+
Configure the primary database in your .env file:
263263

264264
```editorconfig
265265
# Primary database
@@ -269,12 +269,6 @@ DB_USER=root
269269
DB_PASSWORD=root123
270270
DB_NAME=test_db
271271
DB_DIALECT=mysql
272-
273-
# Replica configuration
274-
DB_REPLICA_HOSTS=localhost,replica1,replica2
275-
DB_REPLICA_PORTS=3307,3308,3309
276-
DB_REPLICA_USERS=readonly1,readonly2,readonly3
277-
DB_REPLICA_PASSWORDS=pass1,pass2,pass3
278272
```
279273

280274
**2. Initialize DBResolver**
@@ -299,12 +293,30 @@ func main() {
299293
a := gofr.New()
300294

301295
// Initialize DB resolver with default settings
302-
err := dbresolver.InitDBResolver(a, dbresolver.Config{
296+
err := dbresolver.InitDBResolver(a, &dbresolver.Config{
303297
Strategy: dbresolver.StrategyRoundRobin, // use round-robin strategy or random strategy
304298
ReadFallback: true, // allow reads on primary if all replicas are down
305299
MaxFailures: 3, // number of allowed failures before marking a replica as down
306300
TimeoutSec: 30, // timeout for marking a replica as down
307-
PrimaryRoutes: []string{"/admin", "/api/payments/*"}, // routes that should go to primary
301+
PrimaryRoutes: []string{"/admin", "/api/payments/*"},
302+
303+
Replicas: []dbresolver.ReplicaCredential{
304+
{
305+
Host: "localhost:3307",
306+
User: "replica_user1",
307+
Password: "pass1",
308+
},
309+
{
310+
Host: "replica2.example.com:3308",
311+
User: "replica_user2",
312+
Password: "pass2",
313+
},
314+
{
315+
Host: "replica3.example.com:3309",
316+
User: "replica_user3",
317+
Password: "pass3",
318+
},
319+
},// routes that should go to primary
308320
})
309321
if err != nil {
310322
a.Logger().Errorf("failed to initialize db resolver: %v", err)

pkg/gofr/datasource/dbresolver/factory.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ type Config struct {
4444

4545
// ReplicaCredential stores credentials for a single replica.
4646
type ReplicaCredential struct {
47-
Host string `json:"host"` // Format: "hostname:port".
48-
User string `json:"user"`
49-
Password string `json:"password"` // Supports commas and special chars.
47+
Host string `json:"host"` // Format: "hostname:port" (e.g., "localhost:3307").
48+
User string `json:"user"` // Database username.
49+
Password string `json:"password"` // Database password (supports special characters).
5050
}
5151

5252
// ResolverProvider implements container.DBResolverProvider interface.

0 commit comments

Comments
 (0)