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)
- func FindWithContext[T any](ctx context.Context, db *gorm.DB, out *[]*T, p Pager, opts ...QueryOption) (int64, error)
- 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 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 Tabler
- type Time
- type Type
- func (t Type[T]) Add(ctx context.Context, model *T) error
- func (t Type[T]) Del(ctx context.Context, model *T, opts ...QueryOption) error
- func (t Type[T]) Edit(ctx context.Context, model *T, changeFn func(*T) error, opts ...QueryOption) error
- func (t Type[T]) Find(ctx context.Context, out *[]*T, p Pager, opts ...QueryOption) (int64, error)
- func (t Type[T]) Get(ctx context.Context, out *T, 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 ¶ added in v1.3.16
func DeleteWithContext ¶
func FindWithContext ¶
func FirstWithContext ¶
func GenerateRandomString ¶
func GetEnabledAutoMigrate ¶ added in v1.3.1
func GetEnabledAutoMigrate() bool
func IsDuplicatedKey ¶
func IsErrRecordNotFound ¶
func JSONUnmarshal ¶ added in v1.2.12
JSONUnmarshal 将 input 反序列化到 obj 上
func ParseTimeToLayout ¶
ParseTimeToLayout 解析字符串对应的 layout 仅支持 年-月-日 或 年/月/日 等这种格式
func SetEnabledAutoMigrate ¶ added in v1.3.1
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 ¶ added in v1.3.0
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 ¶ added in v1.5.0
func WithGormLogger ¶ added in v1.5.0
func WithGormLogger(l *slog.Logger, slow time.Duration) GormOption
WithGormLogger 如果需要自定义 logger 的创建,仅供参考
type JSONValueScanner ¶ added in v1.4.0
JSONValueScanner 数据库类型定义为 json 的结构体应当实现此接口
type Logger ¶
func (*Logger) Error ¶ added in v1.5.0
Error implements logger.Interface. Subtle: this method shadows the method (*Logger).Error of Logger.Logger.
func (*Logger) Info ¶ added in v1.5.0
Info implements logger.Interface. Subtle: this method shadows the method (*Logger).Info of Logger.Logger.
type Map ¶ added in v1.5.2
func (*Map) UnmarshalJSON ¶ added in v1.5.2
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 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]) Del ¶
func (t Type[T]) Del(ctx context.Context, model *T, opts ...QueryOption) error
Delete 通用删除
func (Type[T]) Edit ¶
func (t Type[T]) Edit(ctx context.Context, model *T, changeFn func(*T) error, opts ...QueryOption) error
Update 通用更新
type Universal ¶
type Universal[T any] interface { Get(context.Context, *T, ...QueryOption) error 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) }
Universal 通用增删改查