db

package
v0.0.0-...-daa9717 Latest Latest
Warning

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

Go to latest
Published: Apr 14, 2026 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNilContext = errors.New("nil context")
View Source
var ErrNotFound = fmt.Errorf("not found")

Functions

func Ctx

func Ctx(ctx context.Context, db *gorm.DB) context.Context

func CtxNew

func CtxNew(ctx context.Context) context.Context

func DBAll

func DBAll[T DBModelID](ctx context.Context, cb func(context.Context, T) error, ops ...DBAllOption) error

DBAll 按主键递增分页读取数据,并将结果分发给固定数量的工作协程并发执行回调。 通过透传 context 可以让外部取消信号和内部错误更快地停止后续分页与回调处理。

func ExecuteSQLFilesFromEmbed

func ExecuteSQLFilesFromEmbed(ctx context.Context, fs embed.FS, dir string) error

ExecuteSQLFilesFromEmbed reads SQL files from an embedded directory and executes them if not already executed. Uses a transaction to ensure atomicity of SQL execution and history recording.

func For

func For(ctx context.Context) *gorm.DB

func GenID

func GenID(ctx context.Context, name string) (int64, error)

Gen generates a new ID for the given name Uses Upsert to atomically increment the sequence number

func Loader

func Loader[T any](column string, key func(T) string, options ...dataloadgen.Option) *dataloadgen.Loader[string, T]

func LoaderCtx

func LoaderCtx[T any](column string, key func(context.Context, T) string, options ...dataloadgen.Option) *dataloadgen.Loader[string, T]

func NewLoggerOp

func NewLoggerOp(logger logger.Interface) func(*gorm.Config)

func Tx

func Tx(ctx context.Context, f func(context.Context) error, opts ...*sql.TxOptions) error

Types

type Array

type Array[T any] []T

type Config

type Config struct {
	Driver string `json:"driver" yaml:"driver"`
	URL    string `json:"url"    yaml:"url"`
	// contains filtered or unexported fields
}

func (*Config) Ctx

func (c *Config) Ctx(ctx context.Context) context.Context

func (*Config) DB

func (c *Config) DB() *gorm.DB

func (*Config) Open

func (d *Config) Open(ops ...Option) (*gorm.DB, error)

type DBAllConfig

type DBAllConfig struct {
	PageSize    int
	Concurrency int
}

type DBAllOption

type DBAllOption func(*DBAllConfig)

func WithDBAllConcurrency

func WithDBAllConcurrency(concurrency int) DBAllOption

WithDBAllConcurrency 允许按需调整回调并发数,兼顾吞吐和下游处理能力。 channel 缓冲区会与并发数保持一致,避免生产速度远高于消费速度时积压过多内存。

func WithDBAllPageSize

func WithDBAllPageSize(size int) DBAllOption

WithDBAllPageSize 允许按需调整分页大小,避免不同表规模下固定分页带来额外压力。

type DBModelID

type DBModelID interface {
	GetID() uint
}

type IDGen

type IDGen struct {
	Model
	Name string `gorm:"uniqueIndex"` // Unique name for ID generator
	Num  int64  // Current sequence number
}

type Map

type Map[K comparable, T any] map[K]T

type Model

type Model struct {
	CreatedAt time.Time
	UpdatedAt time.Time
	DeletedAt gorm.DeletedAt `gorm:"index"`

	ID uint `gorm:"primarykey"`
}

func (Model) GetID

func (d Model) GetID() uint

type Option

type Option func(*gorm.Config)

type SQLExecutionHistory

type SQLExecutionHistory struct {
	Model
	FileName string `gorm:"uniqueIndex"`
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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