sql.DB Integration

The following examples show how to pass a custom sql.DB object to ent.Client.

Configure sql.DB

First option:

  1. package main
  2. import (
  3. "time"
  4. "<your_project>/ent"
  5. "entgo.io/ent/dialect/sql"
  6. )
  7. func Open() (*ent.Client, error) {
  8. drv, err := sql.Open("mysql", "<mysql-dsn>")
  9. if err != nil {
  10. return nil, err
  11. }
  12. // Get the underlying sql.DB object of the driver.
  13. db := drv.DB()
  14. db.SetMaxIdleConns(10)
  15. db.SetMaxOpenConns(100)
  16. db.SetConnMaxLifetime(time.Hour)
  17. return ent.NewClient(ent.Driver(drv)), nil
  18. }

Second option:

  1. package main
  2. import (
  3. "database/sql"
  4. "time"
  5. "<your_project>/ent"
  6. entsql "entgo.io/ent/dialect/sql"
  7. )
  8. func Open() (*ent.Client, error) {
  9. db, err := sql.Open("mysql", "<mysql-dsn>")
  10. if err != nil {
  11. return nil, err
  12. }
  13. db.SetMaxIdleConns(10)
  14. db.SetMaxOpenConns(100)
  15. db.SetConnMaxLifetime(time.Hour)
  16. // Create an ent.Driver from `db`.
  17. drv := entsql.OpenDB("mysql", db)
  18. return ent.NewClient(ent.Driver(drv)), nil
  19. }

Use Opencensus With MySQL

  1. package main
  2. import (
  3. "context"
  4. "database/sql"
  5. "database/sql/driver"
  6. "<project>/ent"
  7. "contrib.go.opencensus.io/integrations/ocsql"
  8. "entgo.io/ent/dialect"
  9. entsql "entgo.io/ent/dialect/sql"
  10. "github.com/go-sql-driver/mysql"
  11. )
  12. type connector struct {
  13. dsn string
  14. }
  15. func (c connector) Connect(context.Context) (driver.Conn, error) {
  16. return c.Driver().Open(c.dsn)
  17. }
  18. func (connector) Driver() driver.Driver {
  19. return ocsql.Wrap(
  20. mysql.MySQLDriver{},
  21. ocsql.WithAllTraceOptions(),
  22. ocsql.WithRowsClose(false),
  23. ocsql.WithRowsNext(false),
  24. ocsql.WithDisableErrSkip(true),
  25. )
  26. }
  27. // Open new connection and start stats recorder.
  28. func Open(dsn string) *ent.Client {
  29. db := sql.OpenDB(connector{dsn})
  30. // Create an ent.Driver from `db`.
  31. drv := entsql.OpenDB(dialect.MySQL, db)
  32. return ent.NewClient(ent.Driver(drv))
  33. }

Use pgx with PostgreSQL

  1. package main
  2. import (
  3. "context"
  4. "database/sql"
  5. "log"
  6. "<project>/ent"
  7. "entgo.io/ent/dialect"
  8. entsql "entgo.io/ent/dialect/sql"
  9. _ "github.com/jackc/pgx/v5/stdlib"
  10. )
  11. // Open new connection
  12. func Open(databaseUrl string) *ent.Client {
  13. db, err := sql.Open("pgx", databaseUrl)
  14. if err != nil {
  15. log.Fatal(err)
  16. }
  17. // Create an ent.Driver from `db`.
  18. drv := entsql.OpenDB(dialect.Postgres, db)
  19. return ent.NewClient(ent.Driver(drv))
  20. }
  21. func main() {
  22. client := Open("postgresql://user:password@127.0.0.1/database")
  23. // Your code. For example:
  24. ctx := context.Background()
  25. if err := client.Schema.Create(ctx); err != nil {
  26. log.Fatal(err)
  27. }
  28. users, err := client.User.Query().All(ctx)
  29. if err != nil {
  30. log.Fatal(err)
  31. }
  32. log.Println(users)
  33. }