-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.go
91 lines (77 loc) · 1.8 KB
/
init.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package main
import (
"fmt"
"time"
"github.com/pcpratheesh/dbmux"
"github.com/pcpratheesh/dbmux/driver"
"github.com/pcpratheesh/dbmux/driver/options"
"github.com/pcpratheesh/dbmux/entity"
)
func main() {
mysql_1 := entity.Options{
Name: "mysql_1",
Driver: driver.MYSQL,
Host: "localhost",
User: "root",
Password: "root",
Database: "db",
Port: 3306,
ConnectionOptions: []entity.ConnectionOptions{
options.SetConnMaxLifetime(10 * time.Second),
options.SetConnMaxIdleTime(30 * time.Second),
options.SetMaxOpenConns(5),
options.SetMaxIdleConns(2),
},
}
psql_1 := entity.Options{
Name: "psql_1",
Driver: driver.POSTGRES,
Host: "localhost",
User: "postgres",
Password: "postgres",
Database: "db",
Port: 5433,
}
mongo_1 := entity.Options{
Name: "mongo_1",
Driver: driver.MONGO,
Host: "localhost",
User: "admin",
Password: "admin",
Database: "db",
Port: 27017,
}
var dbConnections = []entity.Options{
mysql_1, mongo_1, psql_1,
}
// this will initiate the connection
dbmuxConn := dbmux.New(dbConnections...)
// get the mongo connection pool
{
mongoConnection, err := dbmuxConn.GetConnection("mongo_1")
if err != nil {
panic(err)
}
mongoDB := dbmux.GetConnectionPool[entity.MongoClient](mongoConnection)
_ = mongoDB
}
// get the psql connection pool
{
psqlConnection, err := dbmuxConn.GetConnection("psql_1")
if err != nil {
panic(err)
}
psqlDB := dbmux.GetConnectionPool[entity.SqlDB](psqlConnection)
_ = psqlDB
}
// get the psql connection pool
{
mysqlConnection, err := dbmuxConn.GetConnection("mysql_1")
if err != nil {
panic(err)
}
mysqlDB := dbmux.GetConnectionPool[entity.SqlDB](mysqlConnection)
_ = mysqlDB
}
fmt.Println("dbmux connection initiated")
}