/
entclient.go
executable file
·40 lines (34 loc) · 1.04 KB
/
entclient.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
package database
import (
"database/sql"
"time"
"ariga.io/sqlcomment"
"entgo.io/ent/dialect"
entsql "entgo.io/ent/dialect/sql"
_ "github.com/jackc/pgx/v4/stdlib"
_ "github.com/mattn/go-sqlite3"
"github.com/stablecog/sc-go/database/ent"
)
func NewEntClient(connInfo SqlDBConn) (*ent.Client, error) {
db, err := sql.Open(connInfo.Dialect(), connInfo.DSN())
if err != nil {
return nil, err
}
// Set up connection pool and configure it
db.SetMaxOpenConns(25) // Adjust the max open connections according to your needs
db.SetMaxIdleConns(25) // Adjust the max idle connections
db.SetConnMaxLifetime(5 * time.Minute)
// For some reason, ent doesn't recognize pgx as a valid dialect
entDialect := connInfo.Dialect()
if entDialect == "pgx" {
entDialect = dialect.Postgres
}
driver := entsql.OpenDB(entDialect, db)
sqlcommentDrv := sqlcomment.NewDriver(driver,
sqlcomment.WithDriverVerTag(),
sqlcomment.WithTags(sqlcomment.Tags{
sqlcomment.KeyApplication: "stablecog",
}),
)
return ent.NewClient(ent.Driver(sqlcommentDrv)), nil
}