dbutil

package
v1.19.2 Latest Latest
Warning

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

Go to latest
Published: May 17, 2026 License: BSD-3-Clause Imports: 4 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[models.User](ctx, s.db.DB, ErrUserNotFound, "username = ?", username)

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