database

package
v0.10.14 Latest Latest
Warning

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

Go to latest
Published: May 18, 2026 License: Apache-2.0 Imports: 26 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidDB           = errors.New("invalid database, maybe not initialized")
	ErrUnknowDBType        = errors.New("unknow database type, only support mysql or sqlite")
	ErrNotPtrStruct        = errors.New("model is not pointer to structure")
	ErrNotPtrSlice         = errors.New("not pointer to slice")
	ErrNotPtrInt64         = errors.New("not pointer to int64")
	ErrNilCount            = errors.New("count parameter cannot be nil")
	ErrNilDest             = errors.New("dest parameter cannot be nil")
	ErrEmptyFieldName      = errors.New("field name cannot be empty")
	ErrNilValue            = errors.New("value cannot be nil")
	ErrNotAddressableModel = errors.New("model is not addressable")
	ErrNotAddressableSlice = errors.New("slice is not addressable")
	ErrNotSetSlice         = errors.New("slice cannot set")
	ErrIDRequired          = errors.New("id is required")
)
View Source
var (
	DB *gorm.DB
)

Functions

func Database

func Database[M types.Model](ctx *types.DatabaseContext) types.Database[M]

Database creates and returns a generic database manipulator implementing types.Database interface. Provides comprehensive CRUD capabilities with advanced features like caching, hooks, and query building. Automatically enables debug mode when log level is set to debug.

Type Parameters:

  • M: Model type that implements types.Model interface

Parameters:

  • ctx: Required database context for request tracing and metadata. In service layer operations, pass a valid DatabaseContext to track requests. For non-service layer operations, pass nil.

Returns a database manipulator with full CRUD and query capabilities.

Features:

  • Generic type safety for model operations
  • Automatic debug mode based on configuration
  • Context-aware operations for tracing
  • Default query limit protection
  • Panic protection for uninitialized database

Required usage:

You must call Database[M](ctx) again for each separate operation chain. Assigning the return
value to a variable and running another independent operation on it afterward (e.g.
WithQuery(...).List(...) then Get(...) or Update(...) on the same variable) is incorrect:
after each method, reset() clears this wrapper's options but the underlying GORM session
keeps prior clauses, so later calls can combine wrong WHERE conditions, return empty models,
or corrupt data.

Example:

var users []*User
// Service layer: one Database() call per operation chain (required; anything else is wrong).
_ = Database[*User](ctx.DatabaseContext()).WithQuery(&User{Name: "John"}).List(&users)
u := new(User)
_ = Database[*User](ctx.DatabaseContext()).Get(u, id)

// Non-service layer
_ = Database[*User](nil).WithQuery(&User{Name: "John"}).List(&users)

func Inited added in v0.10.1

func Inited() bool

func InstallGormTracingPlugin

func InstallGormTracingPlugin(db *gorm.DB) error

InstallGormTracingPlugin installs the GORM tracing plugin to the given database instance

Types

type GormTracingPlugin

type GormTracingPlugin struct{}

GormTracingPlugin is a GORM plugin that adds distributed tracing to database operations

func (*GormTracingPlugin) Initialize

func (p *GormTracingPlugin) Initialize(db *gorm.DB) error

Initialize initializes the plugin

func (*GormTracingPlugin) Name

func (p *GormTracingPlugin) Name() string

Name returns the plugin name

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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