Documentation
¶
Index ¶
- Variables
- func CountWithContext[T any](ctx context.Context, db *gorm.DB, opts ...QueryOption) (int64, error)
- func Delete(db *gorm.DB, model any, opts ...QueryOption) error
- func DeleteWithContext(ctx context.Context, db *gorm.DB, model any, opts ...QueryOption) error
- func Find[T any](db *gorm.DB, out *[]*T, p Pager, opts ...QueryOption) (int64, error)deprecated
- func FindWithContext[T any](ctx context.Context, db *gorm.DB, out *[]*T, p Pager, opts ...QueryOption) (int64, error)deprecated
- func First(db *gorm.DB, out any, opts ...QueryOption) error
- func FirstWithContext(ctx context.Context, db *gorm.DB, out any, opts ...QueryOption) error
- func GenerateRandomString(length int) string
- func GetEnabledAutoMigrate() bool
- func IsDuplicatedKey(err error) bool
- func IsErrRecordNotFound(err error) bool
- func JSONUnmarshal(input, obj any) error
- func List[T any](db *gorm.DB, out *[]*T, p Pager, opts ...QueryOption) (int64, error)
- func ListWithContext[T any](ctx context.Context, db *gorm.DB, out *[]*T, p Pager, opts ...QueryOption) (int64, error)
- func New(dialector gorm.Dialector, cfg Config, opts ...GormOption) (*gorm.DB, error)
- func ParseTimeToLayout(value string) string
- func SetEnabledAutoMigrate(v bool)
- func Update[T any](db *gorm.DB, model *T, changeFn func(*T), opts ...QueryOption) error
- func UpdateWithContext[T any](ctx context.Context, db *gorm.DB, model *T, changeFn func(*T), ...) error
- func UpdateWithContext2[T any](ctx context.Context, db *gorm.DB, model *T, changeFn func(*T) error, ...) error
- func UpdateWithSession[T any](tx *gorm.DB, model *T, fn func(*T) error, opts ...QueryOption) error
- type Config
- type DeletedAt
- type DeletedModel
- type Engine
- func (e Engine) DeleteOne(model Tabler, opts ...Option) error
- func (e Engine) Find(model Tabler, bean any, opts ...Option) (total int64, err error)
- func (e Engine) FirstOrCreate(b any) (bool, error)
- func (e Engine) InsertOne(model Tabler) error
- func (e Engine) NextSeq(model Tabler) (nextID int, err error)
- func (e Engine) UpdateOne(model Tabler, id int, data map[string]any) error
- type GormOption
- type JSONValueScanner
- type Logger
- func (l *Logger) Error(ctx context.Context, msg string, args ...any)
- func (l *Logger) Info(ctx context.Context, msg string, args ...any)
- func (l *Logger) LogMode(level logger.LogLevel) logger.Interface
- func (l *Logger) SetLevel(level slog.Level)
- func (l *Logger) Trace(ctx context.Context, begin time.Time, ...)
- func (l *Logger) Warn(ctx context.Context, msg string, args ...any)
- type Map
- func (i Map) Delete(key string) Map
- func (i Map) Get(key string) any
- func (i Map) GetBool(key string) bool
- func (i Map) GetInt(key string) int
- func (i Map) GetString(key string) string
- func (i Map) Has(key string) bool
- func (i Map) Merge(other Map) Map
- func (i *Map) Scan(input any) error
- func (i Map) Set(key string, value any) Map
- func (i *Map) UnmarshalJSON(in []byte) error
- func (i Map) Value() (driver.Value, error)
- type Model
- type ModelWithStrID
- type Option
- type Pager
- type Query
- type QueryOption
- type Scanerdeprecated
- type StructMap
- type Tabler
- type Time
- type Type
- func (t Type[T]) Add(ctx context.Context, model *T) errordeprecated
- func (t Type[T]) Create(ctx context.Context, model *T) error
- func (t Type[T]) Del(ctx context.Context, model *T, opts ...QueryOption) errordeprecated
- func (t Type[T]) Delete(ctx context.Context, model *T, opts ...QueryOption) error
- func (t Type[T]) Edit(ctx context.Context, model *T, changeFn func(*T) error, opts ...QueryOption) errordeprecated
- func (t Type[T]) Find(ctx context.Context, out *[]*T, p Pager, opts ...QueryOption) (int64, error)deprecated
- func (t Type[T]) Get(ctx context.Context, out *T, opts ...QueryOption) error
- func (t Type[T]) List(ctx context.Context, out *[]*T, p Pager, opts ...QueryOption) (int64, error)
- func (t Type[T]) Update(ctx context.Context, model *T, changeFn func(*T) error, opts ...QueryOption) error
- type Universal
- type UniversalSession
Constants ¶
This section is empty.
Variables ¶
var ( ErrRecordNotFound = gorm.ErrRecordNotFound ErrDuplicatedKey = gorm.ErrDuplicatedKey )
var EnabledAutoMigrate bool
Deprecated: 请使用 GetEnabledAutoMigrate,SetEnabledAutoMigrate EnabledAutoMigrate 是否开启自动迁移 每次表迁移耗时,提供此全局变量,程序可根据需要是否迁移
Functions ¶
func CountWithContext ¶
func DeleteWithContext ¶
func FindWithContext
deprecated
func FirstWithContext ¶
func GenerateRandomString ¶
func GetEnabledAutoMigrate ¶
func GetEnabledAutoMigrate() bool
func IsDuplicatedKey ¶
func IsErrRecordNotFound ¶
func ListWithContext ¶
func ParseTimeToLayout ¶
ParseTimeToLayout 解析字符串对应的 layout 仅支持 年-月-日 或 年/月/日 等这种格式
func SetEnabledAutoMigrate ¶
func SetEnabledAutoMigrate(v bool)
func Update ¶
func Update[T any](db *gorm.DB, model *T, changeFn func(*T), opts ...QueryOption) error
func UpdateWithContext ¶
func UpdateWithContext2 ¶
func UpdateWithSession ¶
Types ¶
type DeletedModel ¶
DeletedModel 删除模型
func (*DeletedModel) BeforeCreate ¶
func (d *DeletedModel) BeforeCreate(*gorm.DB) error
func (*DeletedModel) BeforeUpdate ¶
func (d *DeletedModel) BeforeUpdate(*gorm.DB) error
type Engine ¶
type Engine struct {
// contains filtered or unexported fields
}
func (Engine) FirstOrCreate ¶
FirstOrCreate true:创建;false:查询
type GormOption ¶
func WithGormLogger ¶
func WithGormLogger(l *slog.Logger, slow time.Duration) GormOption
WithGormLogger 如果需要自定义 logger 的创建,仅供参考
type JSONValueScanner ¶
JSONValueScanner 数据库类型定义为 json 的结构体应当实现此接口
type Logger ¶
func (*Logger) Error ¶
Error implements logger.Interface. Subtle: this method shadows the method (*Logger).Error of Logger.Logger.
func (*Logger) Info ¶
Info implements logger.Interface. Subtle: this method shadows the method (*Logger).Info of Logger.Logger.
type Map ¶
Map 因值是 any 类型,封装一些快速提取的函数
func (*Map) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler.
type Model ¶
type Model struct {
ID int `gorm:"primaryKey;" json:"id"`
CreatedAt Time `gorm:"notNull;default:CURRENT_TIMESTAMP;index;comment:创建时间" json:"created_at"`
UpdatedAt Time `gorm:"notNull;default:CURRENT_TIMESTAMP;comment:更新时间" json:"updated_at"`
}
Model int id 模型 sqlite 不支持 default:now(),支持 CURRENT_TIMESTAMP
type ModelWithStrID ¶
type ModelWithStrID struct {
ID string `gorm:"primaryKey;" json:"id"`
CreatedAt Time `gorm:"notNull;default:CURRENT_TIMESTAMP;index;comment:创建时间" json:"created_at"`
UpdatedAt Time `gorm:"notNull;default:CURRENT_TIMESTAMP;comment:更新时间" json:"updated_at"`
}
ModelWithStrID string id 模型
func (*ModelWithStrID) BeforeCreate ¶
func (d *ModelWithStrID) BeforeCreate(*gorm.DB) error
func (*ModelWithStrID) BeforeUpdate ¶
func (d *ModelWithStrID) BeforeUpdate(*gorm.DB) error
type Option ¶
func WithCreatedAt ¶
type Query ¶
type Query struct {
// contains filtered or unexported fields
}
func (*Query) Encode ¶
func (q *Query) Encode() []QueryOption
type QueryOption ¶
QueryOption ..
type StructMap ¶
StructMap 其目的是将已知的参数作为结构体调用,未知的参数不动 例如 s.Data.Username
func (StructMap[T]) MarshalJSON ¶
MarshalJSON implements json.Marshaler.
func (*StructMap[T]) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler.
type Time ¶
func (Time) MarshalJSON ¶
func (*Time) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler.
type Type ¶
type Type[T any] struct { // contains filtered or unexported fields }
func (Type[T]) Delete ¶
func (t Type[T]) Delete(ctx context.Context, model *T, opts ...QueryOption) error
Delete 通用删除
func (Type[T]) Get ¶
func (t Type[T]) Get(ctx context.Context, out *T, opts ...QueryOption) error
First 通用查询
type Universal ¶
type Universal[T any] interface { Get(context.Context, *T, ...QueryOption) error // olw Edit(context.Context, *T, func(*T) error, ...QueryOption) error Del(context.Context, *T, ...QueryOption) error Add(context.Context, *T) error Find(context.Context, *[]*T, Pager, ...QueryOption) (int64, error) // new Delete(context.Context, *T, ...QueryOption) error Create(context.Context, *T) error List(context.Context, *[]*T, Pager, ...QueryOption) (int64, error) Update(context.Context, *T, func(*T) error, ...QueryOption) error }
Universal 通用增删改查 Deprecated: 建议使用 milady 生成代码,而非内嵌此接口