dbx

package
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Sep 14, 2026 License: Apache-2.0 Imports: 18 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrDriverRequired      = errx.New("database driver is required")
	ErrDSNRequired         = errx.New("database dsn is required")
	ErrDriverNotRegistered = errx.New("database driver is not registered")

	// ErrRecordNotFound 表示查询没有找到记录。
	ErrRecordNotFound = gorm.ErrRecordNotFound
	// ErrDuplicatedKey 表示唯一约束或主键冲突。
	ErrDuplicatedKey = gorm.ErrDuplicatedKey
	// ErrForeignKeyViolated 表示外键约束冲突。
	ErrForeignKeyViolated = gorm.ErrForeignKeyViolated
)

Functions

func IsDuplicatedKey

func IsDuplicatedKey(err error) bool

IsDuplicatedKey 判断错误链是否包含唯一键冲突错误。

func IsForeignKeyViolated

func IsForeignKeyViolated(err error) bool

IsForeignKeyViolated 判断错误链是否包含外键约束错误。

func IsRecordNotFound

func IsRecordNotFound(err error) bool

IsRecordNotFound 判断错误链是否包含记录不存在错误。

func NewLogger

func NewLogger(config LoggerConfig) logger.Interface

NewLogger 创建从 GORM context 读取 core/logx Logger 的日志适配器。

func Register

func Register(name string, driver Driver)

Register 注册数据库驱动。重复注册属于程序配置错误,会直接 panic。

Types

type DB

type DB = gorm.DB

DB 是 GORM 数据库会话的类型别名,保留全部原生方法和类型兼容性。

func Open

func Open(driverName, dsn string, options ...Option) (*DB, error)

Open 使用已注册驱动和原生 DSN 打开数据库,不执行数据库迁移。

type DialectorFunc

type DialectorFunc func(dsn string) (gorm.Dialector, error)

DialectorFunc 根据原生 DSN 创建 GORM Dialector。

type Driver

type Driver struct {
	Dialector DialectorFunc
	Pool      PoolConfig
}

Driver 描述按需注册的数据库驱动及其默认连接池参数。

type LoggerConfig

type LoggerConfig struct {
	SlowThreshold             time.Duration
	LogLevel                  logger.LogLevel
	IgnoreRecordNotFoundError bool
}

LoggerConfig 定义 GORM 日志级别和慢查询阈值。

func DefaultLoggerConfig

func DefaultLoggerConfig() LoggerConfig

DefaultLoggerConfig 返回与 core/logx 默认级别一致的 GORM 日志配置。

type Metadata

type Metadata struct {
	// Id 主键
	Id string `json:"id" gorm:"primaryKey;comment:主键"`

	// 创建人
	CreatedBy string `json:"created_by" gorm:"type:varchar(36);comment:创建人"`
	// 创建时间
	CreatedAt time.Time `json:"created_at" gorm:"autoCreateTime:milli;comment:创建时间"`
	// 更新人
	UpdatedBy string `json:"updated_by" gorm:"type:varchar(36);comment:更新人"`
	// 更新时间
	UpdatedAt time.Time `json:"updated_at" gorm:"autoUpdateTime:milli;comment:更新时间"`
	// 删除时间,0 表示有效记录,非 0 值为 Unix 毫秒时间戳
	DeletedAt datatype.DeletedAt `json:"deleted_at" gorm:"not null;default:0;index;comment:删除时间"`
}

Metadata 元数据

type OnConflict

type OnConflict = clause.OnConflict

OnConflict 是 GORM 写入冲突策略的类型别名。

type Option

type Option func(*config)

Option 调整 Open 的公共配置。

func WithGORMConfig

func WithGORMConfig(value *gorm.Config) Option

WithGORMConfig 使用指定的 GORM 配置覆盖默认配置;TranslateError 始终保持开启。

func WithGORMOptions

func WithGORMOptions(options ...gorm.Option) Option

WithGORMOptions 追加传递给 gorm.Open 的选项。

func WithPoolConfig

func WithPoolConfig(value PoolConfig) Option

WithPoolConfig 覆盖驱动提供的默认连接池配置。

type PoolConfig

type PoolConfig struct {
	MaxIdleConns    int
	MaxOpenConns    int
	ConnMaxLifetime time.Duration
	ConnMaxIdleTime time.Duration
}

PoolConfig 定义 database/sql 连接池参数。

Directories

Path Synopsis
Package migrate 提供带跨副本锁的有序 GORM 数据库迁移。
Package migrate 提供带跨副本锁的有序 GORM 数据库迁移。
Package mysql 按需注册 MySQL GORM 驱动。
Package mysql 按需注册 MySQL GORM 驱动。
Package postgres 按需注册 PostgreSQL GORM 驱动。
Package postgres 按需注册 PostgreSQL GORM 驱动。
Package sqlite 按需注册无 CGO 的 SQLite GORM 驱动。
Package sqlite 按需注册无 CGO 的 SQLite GORM 驱动。

Jump to

Keyboard shortcuts

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