gormutils

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Jan 6, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const (
	EnvDBInit            = "DB_INIT"
	EnvDBDriver          = "DB_DRIVER"
	EnvDBConnection      = "DB_CONNECTION"
	EnvDBConnMaxLifetime = "DB_CONNMAXLIFETIME"
	EnvDBMaxOpenConns    = "DB_MAXOPENCONNS"
	EnvDBMaxIdleConns    = "DB_MAXIDLECONNS"
	EnvDBDebug           = "DB_DEBUG"
)

Variables

This section is empty.

Functions

func Create

func Create(v any) error

Create insert the value into database

func Delete

func Delete(value any, where ...any) (err error)

Delete delete value match given conditions, if the value has primary key, then will including the primary key as condition

func Find

func Find(out any, where ...any) (err error)

Find find records that match given conditions

func First

func First(out any, where ...any) (exist bool, err error)

First find first record that match given conditions, order by primary key

func FromJsonb

func FromJsonb(jsonb datatypes.JSON, v any) (err error)

FromJsonb convert jsonb object to entity

func GetDB

func GetDB() *gorm.DB

GetDB get gorm DB client

func MustToJsonb

func MustToJsonb(v any) (jsonb datatypes.JSON)

MustToJsonb convert object to jsonb and ignore errors

func NewLogrAdapter

func NewLogrAdapter(l logr.Logger) gormLogger.Writer

func Save

func Save(v any) error

Save update value in database, if the value doesn't have primary key, will insert it

func ToJsonb

func ToJsonb(v any) (jsonb datatypes.JSON, err error)

ToJsonb convert object to jsonb

func Tx

func Tx(tx func(dbCtx DBContext) error) error

Tx runs a transaction in a function

Types

type DBContext

type DBContext interface {
	SetContext(ctx context.Context)

	// db
	Create(v any) error
	Save(v any) error
	First(out any, where ...any) (exist bool, err error)
	Find(out any, where ...any) (err error)
	Delete(value any, where ...any) (err error)
	Exec(sql string, values ...any) error
	Tx(tx func(dbCtx DBContext) error) (err error)

	// transaction
	Begin() (err error)
	Commit() (err error)
	Rollback() error

	// query
	NewQuery() Query
}

DBContext indicates the db context interface

func DefaultDBContext

func DefaultDBContext(dbCtx DBContext) DBContext

DefaultDBContext returns a new DBContextImpl if the input dbCtx is nil

func NewDBContext

func NewDBContext() DBContext

NewDBContext return a new DBContextImpl

type DBContextImpl

type DBContextImpl struct {
	// contains filtered or unexported fields
}

DBContextImpl is a DB operation package

func (*DBContextImpl) Begin

func (dbCtx *DBContextImpl) Begin() (err error)

Begin a new transaction

func (*DBContextImpl) Commit

func (dbCtx *DBContextImpl) Commit() (err error)

Commit a transaction

func (*DBContextImpl) Create

func (dbCtx *DBContextImpl) Create(v any) error

Create insert the value into database

func (*DBContextImpl) Delete

func (dbCtx *DBContextImpl) Delete(value any, where ...any) (err error)

Delete delete value match given conditions, if the value has primary key, then will including the primary key as condition

func (*DBContextImpl) Exec

func (dbCtx *DBContextImpl) Exec(sql string, values ...any) error

Exec execute a raw SQL expression

func (*DBContextImpl) Find

func (dbCtx *DBContextImpl) Find(out any, where ...any) (err error)

Find find records that match given conditions

func (*DBContextImpl) First

func (dbCtx *DBContextImpl) First(out any, where ...any) (exist bool, err error)

First find first record that match given conditions, order by primary key

func (*DBContextImpl) NewQuery

func (dbCtx *DBContextImpl) NewQuery() Query

NewQuery return a new QueryImpl

func (*DBContextImpl) Rollback

func (dbCtx *DBContextImpl) Rollback() error

Rollback a transaction

func (*DBContextImpl) Save

func (dbCtx *DBContextImpl) Save(v any) error

Save update value in database, if the value doesn't have primary key, will insert it

func (*DBContextImpl) SetContext

func (dbCtx *DBContextImpl) SetContext(ctx context.Context)

SetContext the context of the DBContextImpl

func (*DBContextImpl) Tx

func (dbCtx *DBContextImpl) Tx(tx func(dbCtx DBContext) error) (err error)

Tx starts a transaction

type Model

type Model struct {
	ModelUnscoped

	DeletedAt gorm.DeletedAt `gorm:"index" json:"deleted_at"`
}

Model base DB model

type ModelUnscoped

type ModelUnscoped struct {
	ID        uint      `gorm:"primary_key" json:"id"`
	CreatedAt time.Time `json:"created_at"`
	UpdatedAt time.Time `json:"updated_at"`
}

type Query

type Query interface {
	DBContext() DBContext
	Model(model any) Query
	Where(query any, args ...any) Query
	OrderBy(exp any) Query
	Offset(offset int) Query
	Limit(limit int) Query
	Page(page, pageSize int) Query
	Raw(sql string, values ...any) Query
	Find(out any) error
	First(out any) (exist bool, err error)
	Count() (count int64, err error)
	Scan(out any) (err error)
}

Query indicates the db query interface

type QueryImpl

type QueryImpl struct {
	// contains filtered or unexported fields
}

QueryImpl DB query model

func (*QueryImpl) Count

func (q *QueryImpl) Count() (count int64, err error)

Count returns the record count

func (*QueryImpl) DBContext

func (q *QueryImpl) DBContext() DBContext

DBContext returns the DBContext query uses

func (*QueryImpl) Find

func (q *QueryImpl) Find(out any) error

Find fetches the query records

func (*QueryImpl) First

func (q *QueryImpl) First(out any) (exist bool, err error)

First return the first item

func (*QueryImpl) Limit

func (q *QueryImpl) Limit(limit int) Query

Limit sets the query limit

func (*QueryImpl) Model

func (q *QueryImpl) Model(model any) Query

Model sets the query model

func (*QueryImpl) Offset

func (q *QueryImpl) Offset(offset int) Query

Offset sets the query offset

func (*QueryImpl) OrderBy

func (q *QueryImpl) OrderBy(exp any) Query

OrderBy sets the query order by expr

func (*QueryImpl) Page

func (q *QueryImpl) Page(page, pageSize int) Query

Page sets the query offset and limit with pagination logical

func (*QueryImpl) Raw

func (q *QueryImpl) Raw(sql string, values ...any) Query

Raw sets the query raw SQL

func (*QueryImpl) Scan

func (q *QueryImpl) Scan(out any) (err error)

Scan the raw sql results

func (*QueryImpl) Where

func (q *QueryImpl) Where(query any, args ...any) Query

Where sets the query condition

Jump to

Keyboard shortcuts

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