Go to file
dependabot[bot] eeac644146
Bump go.unistack.org/micro/v3 from 3.10.10 to 3.10.11
Bumps [go.unistack.org/micro/v3](https://github.com/unistack-org/micro) from 3.10.10 to 3.10.11.
- [Release notes](https://github.com/unistack-org/micro/releases)
- [Commits](https://github.com/unistack-org/micro/compare/v3.10.10...v3.10.11)

---
updated-dependencies:
- dependency-name: go.unistack.org/micro/v3
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
2023-02-14 18:58:19 +00: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 fix 2023-01-16 23:26:58 +03:00
conn.go fix ping 2023-01-30 20:59:04 +03:00
doc.go rewrite wrapper 2022-12-25 15:35:05 +03:00
driver.go fix context 2023-01-16 23:38:29 +03:00
go.mod Bump go.unistack.org/micro/v3 from 3.10.10 to 3.10.11 2023-02-14 18:58:19 +00:00
go.sum Bump go.unistack.org/micro/v3 from 3.10.10 to 3.10.11 2023-02-14 18:58:19 +00:00
LICENSE Initial commit 2021-01-29 22:21:37 +03:00
options.go add logger 2023-01-06 23:24:39 +03:00
README.md update README.md 2023-01-06 23:42:34 +03:00
stats.go rewrite wrapper 2022-12-25 15:35:05 +03:00
stmt.go fix context 2023-01-16 23:38:29 +03:00
tx.go fix transactions tracing (#58) 2023-01-17 15:20:36 +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)
	}

	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
}