ent 基本使用十一 sql.DB 集成

这个功能是github中大家提的比较多的一个,所以官方也暴露了相关的api

配置sql.DB

  • 一种方式
package main
import (
    "time"
    "<your_project>/ent"
    "github.com/facebookincubator/ent/dialect/sql"
)
func Open() (*ent.Client, error) {
    drv, err := sql.Open("mysql", "<mysql-dsn>")
    if err != nil {
        return nil, err
    }
    // Get the underlying sql.DB object of the driver.
    db := drv.DB()
    db.SetMaxIdleConns(10)
    db.SetMaxOpenConns(100)
    db.SetConnMaxLifetime(time.Hour)
    return ent.NewClient(ent.Driver(drv)), nil
}
 
 
  • 第二种方式
package main
import (
    "database/sql"
    "time"
    "<your_project>/ent"
    entsql "github.com/facebookincubator/ent/dialect/sql"
)
func Open() (*ent.Client, error) {
    db, err := sql.Open("mysql", "<mysql-dsn>")
    if err != nil {
        return nil, err
    }
    db.SetMaxIdleConns(10)
    db.SetMaxOpenConns(100)
    db.SetConnMaxLifetime(time.Hour)
    // Create an ent.Driver from `db`.
    drv := entsql.OpenDB("mysql", db)
    return ent.NewClient(ent.Driver(drv)), nil
}
 

集成监控

方便分析sql

package main
import (
    "context"
    "database/sql"
    "database/sql/driver"
    "<project>/ent"
    "contrib.go.opencensus.io/integrations/ocsql"
    "github.com/go-sql-driver/mysql"
    entsql "github.com/facebookincubator/ent/dialect/sql"
)
type connector struct {
    dsn string
}
func (c connector) Connect(context.Context) (driver.Conn, error) {
    return c.Driver().Open(c.dsn)
}
func (connector) Driver() driver.Driver {
    return ocsql.Wrap(
        mysql.MySQLDriver{},
        ocsql.WithAllTraceOptions(),
        ocsql.WithRowsClose(false),
        ocsql.WithRowsNext(false),
        ocsql.WithDisableErrSkip(true),
    )
}
// Open new connection and start stats recorder.
func Open(dsn string) *ent.Client {
    db := sql.OpenDB(connector{dsn})
    // Create an ent.Driver from `db`.
    drv := entsql.OpenDB("mysql", db)
    return ent.NewClient(ent.Driver(drv))
}

参考资料

https://entgo.io/docs/sql-integration/

posted on 2019-10-15 09:23  荣锋亮  阅读(976)  评论(0编辑  收藏  举报

导航