Go to file
Vasiliy Tolstov 927218921e
Some checks failed
autoapprove / autoapprove (pull_request) Failing after 1m26s
automerge / automerge (pull_request) Failing after 4s
codeql / analyze (go) (pull_request) Failing after 3m16s
dependabot-automerge / automerge (pull_request) Has been skipped
prbuild / test (pull_request) Failing after 1m29s
prbuild / lint (pull_request) Failing after 2m36s
fixup metrics and tracing
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2023-09-01 00:21:08 +03:00
.github Bump dependabot/fetch-metadata from 1.3.5 to 1.3.6 (#62) 2023-01-30 21:21:26 +03:00
.gitignore Initial commit 2021-01-29 22:21:37 +03:00
common.go fixup metrics and tracing 2023-09-01 00:21:08 +03:00
conn.go fixup metrics and tracing 2023-09-01 00:21:08 +03:00
doc.go use generated interface 2023-06-10 13:09:25 +03:00
driver.go fixup metrics and tracing 2023-09-01 00:21:08 +03:00
gen.go fixup metrics and tracing 2023-09-01 00:21:08 +03:00
go.mod fixup metrics and tracing 2023-09-01 00:21:08 +03:00
go.sum fixup v3 2023-08-29 01:30:27 +03:00
LICENSE Initial commit 2021-01-29 22:21:37 +03:00
options.go fixup metrics and tracing 2023-09-01 00:21:08 +03:00
README.md remove ErrUnsupported and fixes 2023-02-17 23:54:50 +03:00
stats.go fixup v3 2023-08-29 01:30:27 +03:00
stmt.go fixup metrics and tracing 2023-09-01 00:21:08 +03:00
tx.go fixup metrics and tracing 2023-09-01 00:21:08 +03:00
wrap_gen.go fixup metrics and tracing 2023-09-01 00:21:08 +03:00
wrap.go fixup 2023-08-29 01:25:45 +03:00

micro-wrapper-sql

Example for For postgres

package storage

import (
	"fmt"
	"net/url"
	"time"

	"github.com/jackc/pgx/v4"
	"github.com/jackc/pgx/v4/stdlib"
	"github.com/jmoiron/sqlx"
	wrapper "go.unistack.org/micro-wrapper-sql/v3"
)

func Connect(cfg *PostgresConfig) (*sqlx.DB, error) {
	// format connection string
	cstr := fmt.Sprintf(
		"postgres://%s:%s@%s/%s?sslmode=disable&statement_cache_mode=describe",
		cfg.Login,
		url.QueryEscape(cfg.Passw),
		cfg.Addr,
		cfg.DBName,
	)

	// parse connection string
	dbConf, err := pgx.ParseConfig(cstr)
	if err != nil {
		return nil, err
	}

	// needed for pgbouncer
	dbConf.RuntimeParams = map[string]string{
		"standard_conforming_strings": "on",
		"application_name":            cfg.AppName,
	}
	// may be needed for pbbouncer, needs to check
	// dbConf.PreferSimpleProtocol = true
	// register pgx conn
	dsn := stdlib.RegisterConnConfig(dbConf)


  wrapper.DefaultMeterStatsInterval = 1 * time.Second
	logger.DefaultLogger = logger.NewLogger(logger.WithLevel(logger.DebugLevel))

	if err := logger.DefaultLogger.Init(); err != nil {
		t.Fatal(err)
	}

  // for postgres user stdlib.GetDefaultDriver() or it fails
	sql.Register("micro-wrapper-sql", wrapper.NewWrapper(&sqlite.Driver{},
		wrapper.DatabaseHost("localhost"),
		wrapper.DatabaseName("mydb"),
		wrapper.LoggerLevel(logger.DebugLevel),
		wrapper.LoggerEnabled(true),
	))

  wdb, err := sql.Open("micro-wrapper-sql", dsn)
	if err != nil {
		return nil, err
	}

	db := sqlx.NewDb(wdb, "pgx")
	db.SetMaxOpenConns(int(cfg.ConnMax))
	db.SetMaxIdleConns(int(cfg.ConnMaxIdle))
	db.SetConnMaxLifetime(time.Duration(cfg.ConnLifetime) * time.Second)
	db.SetConnMaxIdleTime(time.Duration(cfg.ConnMaxIdleTime) * time.Second)
	
	return db, nil
}