kv

package
v1.1.0-beta.2 Latest Latest
Warning

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

Go to latest
Published: Sep 23, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	SetDb = func(gdb *gorm.DB) {
		db = gdb
		if err := db.AutoMigrate(&ConfigItem{}); err != nil {
			panic("failed to migrate config item table: " + err.Error())
		}
	}
)

Functions

func GetAll

func GetAll() (map[string]any, error)

func GetAs

func GetAs[T any](key string, defaults ...any) (T, error)

GetAs 获取并转换为指定类型 (泛型),支持数值类型自动转换

func GetMany

func GetMany(keys map[string]any) (map[string]any, error)

GetMany 获取多个配置项,keys 为 map[key]defaultValue 如果 defaultValue 为 nil,则数据库不存在时不写入 如果 defaultValue 不为 nil,则数据库不存在时写入默认值

func GetManyAs

func GetManyAs[T any]() (*T, error)

GetManyAs 将多个配置项映射到一个结构体中,json tag 作为 Key 支持 default tag 作为默认值,如果数据库中不存在且有 default tag 则写入数据库 没有 default tag 的字段使用零值,不写入数据库

func IsChangedT

func IsChangedT[T any](e ConfigEvent, key string) (bool, T)

func Set

func Set(key string, value any) error

Set 设置单个配置,是 SetMany 只传一个 key 的简写。

func SetMany

func SetMany(cst map[string]any) error

func Subscribe

func Subscribe(subscriber ConfigSubscriber)

Subscribe registers a subscriber for all config events.

Types

type ConfigEvent

type ConfigEvent struct {
	Old map[string]any
	New map[string]any
}

func (ConfigEvent) IsChanged

func (e ConfigEvent) IsChanged(key string) bool

type ConfigItem

type ConfigItem struct {
	Key   string `gorm:"primaryKey;column:key;type:text"`
	Value string `gorm:"column:value;type:text"` // 存 JSON 字符串
}

func (ConfigItem) TableName

func (ConfigItem) TableName() string

type ConfigSubscriber

type ConfigSubscriber func(event ConfigEvent)

ConfigSubscriber handles config events

Jump to

Keyboard shortcuts

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