dbutils

package
v0.0.0-...-225f367 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 13, 2022 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Open

func Open(c Config) (*gorm.DB, error)

Open initialize db connection

func Transaction

func Transaction(ctx context.Context, conn *gorm.DB, handler TransactionHandler, afterCommitFns ...func()) (err error)

Transaction handles a task under transaction. It's NOT concurrency-safe.

Transaction creates a transaction automatically if there is no available transaction in the context, otherwise reuse the existing one. The existing transaction instance is passed to the context.

Example usage:

type Storage struct {
	conn *gorm.DB
}

func (s *Storage) CreateUser(ctx context.Context, user *User) error {
	return Transaction(ctx, s.conn, func(ctx context.Context, tx *gorm.DB) error {
		return tx.Create(user).Error
	})
}

Types

type Config

type Config struct {
	gorm.Config

	DSN          string
	MaxIdleConns int           // the maximum number of connections in the idle connection pool.
	MaxOpenConns int           // the maximum number of open connections to the database.
	MaxLifetime  time.Duration // the maximum amount of time a connection may be reused.
	MaxIdleTime  time.Duration // the maximum amount of time a connection may be idle.
}

Config connection config

type TransactionHandler

type TransactionHandler func(ctx context.Context, tx *gorm.DB) error

TransactionHandler transaction handler

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL