Help us shape the future of Go! Take the 2026 Go Developer Survey

dbutil

package
v2.16.0 Latest Latest
Warning

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

Go to latest
Published: Oct 13, 2026 License: BSD-3-Clause Imports: 5 Imported by: 0

Documentation

Overview

Package dbutil provides small generic helpers around GORM that consolidate repetitive single-row lookup and transaction boilerplate found across services.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FirstWhere

func FirstWhere[T any](ctx context.Context, db *gorm.DB, notFound error, where string, args ...any) (*T, error)

FirstWhere fetches a single row of T matching the provided WHERE clause. It maps gorm.ErrRecordNotFound to notFound (when non-nil) and otherwise wraps the error with a generic "failed to query <T>" prefix.

Example:

user, err := dbutil.FirstWhere[user.User](ctx, s.db.DB, ErrUserNotFound, "username = ?", username)

func IsRetryableWriteError added in v2.8.0

func IsRetryableWriteError(err error) bool

IsRetryableWriteError reports whether a database write failed because the database was temporarily busy or timed out.

func WithTx

func WithTx(ctx context.Context, db *gorm.DB, fn func(tx *gorm.DB) error) error

WithTx runs fn inside a GORM transaction bound to ctx. Equivalent to db.WithContext(ctx).Transaction(fn) but tightens the surface area at call sites.

Types

This section is empty.

Jump to

Keyboard shortcuts

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