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) errordeprecated
- func DeleteWithContext(ctx context.Context, db *gorm.DB, model any, opts ...QueryOption) errordeprecated
- 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) errordeprecated
- func FirstWithContext(ctx context.Context, db *gorm.DB, out any, opts ...QueryOption) error
- func GenerateRandomString(length int) string
- func GetEnabledAutoMigrate() bool
- func GormDB(tx Tx) *gorm.DB
- 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)deprecated
- func ListWithContext[T any](ctx context.Context, db *gorm.DB, out *[]*T, p Pager, opts ...QueryOption) (int64, error)deprecated
- 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) errordeprecated
- func UpdateWithContext[T any](ctx context.Context, db *gorm.DB, model *T, changeFn func(*T), ...) errordeprecated
- 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) errordeprecated
- type Config
- type DeletedAt
- type DeletedModel
- type Enginedeprecated
- func NewEngine(db *gorm.DB) Enginedeprecated
- func (e Engine) DeleteOne(model Tabler, opts ...Option) errordeprecated
- func (e Engine) Find(model Tabler, bean any, opts ...Option) (total int64, err error)deprecated
- func (e Engine) FirstOrCreate(b any) (bool, error)deprecated
- func (e Engine) InsertOne(model Tabler) errordeprecated
- func (e Engine) NextSeq(model Tabler) (nextID int, err error)deprecated
- func (e Engine) UpdateOne(model Tabler, id int, data map[string]any) errordeprecated
- 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 Optiondeprecated
- func WithCreatedAt(startAt, endAt int64) Optiondeprecated
- func WithID(id int) Optiondeprecated
- func WithLimit(limit, offset int) Optiondeprecated
- type Pagerdeprecated
- type Query
- type QueryOption
- type Scanerdeprecated
- type StructMap
- type Tabler
- type Time
- type Tx
- type Typedeprecated
- func NewType[T any](db *gorm.DB) Type[T]deprecated
- func (t Type[T]) Add(ctx context.Context, model *T) errordeprecated
- func (t Type[T]) Create(ctx context.Context, model *T) errordeprecated
- 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) errordeprecated
- 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) errordeprecated
- func (t Type[T]) List(ctx context.Context, out *[]*T, p Pager, opts ...QueryOption) (int64, error)deprecated
- func (t Type[T]) Update(ctx context.Context, model *T, changeFn func(*T) error, opts ...QueryOption) errordeprecated
- type Universaldeprecated
- type UniversalSessiondeprecated
Constants ¶
This section is empty.
Variables ¶
var ( ErrRecordNotFound = gorm.ErrRecordNotFound ErrDuplicatedKey = gorm.ErrDuplicatedKey )
var EnabledAutoMigrate bool
Deprecated: 请使用 GetEnabledAutoMigrate / SetEnabledAutoMigrate
Functions ¶
func CountWithContext ¶ added in v1.3.16
CountWithContext 通用计数查询
func DeleteWithContext
deprecated
func FindWithContext
deprecated
func FirstWithContext ¶
FirstWithContext 按条件查询单条记录(内部使用 Take 避免额外排序)
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 ListWithContext
deprecated
added in
v1.7.0
func ParseTimeToLayout ¶
ParseTimeToLayout 解析字符串对应的 layout 仅支持 年-月-日 或 年/月/日 等这种格式
func SetEnabledAutoMigrate ¶ added in v1.3.1
func SetEnabledAutoMigrate(v bool)
func UpdateWithContext
deprecated
func UpdateWithContext2 ¶ added in v1.3.0
func UpdateWithContext2[T any](ctx context.Context, db *gorm.DB, model *T, changeFn func(*T) error, opts ...QueryOption) error
UpdateWithContext2 事务内悲观锁更新(SELECT FOR UPDATE + Save),changeFn 可返回 error 中止事务
func UpdateWithSession
deprecated
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
deprecated
type Engine struct {
// contains filtered or unexported fields
}
Deprecated: 请使用 goddd 生成的 Store 实现
func (Engine) FirstOrCreate
deprecated
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
Map 因值是 any 类型,封装一些快速提取的函数
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
deprecated
Deprecated: 请使用 QueryOption func(*gorm.DB) *gorm.DB(返回新 DB 实例的正确链式语义)
func WithCreatedAt
deprecated
type Query ¶
type Query struct {
// contains filtered or unexported fields
}
Query 链式查询条件构建器
func (*Query) Encode ¶
func (q *Query) Encode() []QueryOption
type QueryOption ¶
QueryOption 查询选项函数
func OrderBy
deprecated
func OrderBy(value any) QueryOption
Deprecated: 请使用 Query.OrderBy
func Select
deprecated
added in
v1.7.6
func Select(columns any) QueryOption
Deprecated: 请使用 Query.Select
func Unscoped
deprecated
added in
v1.5.4
func Unscoped() QueryOption
Deprecated: 请在 Store 直接调用 db.Unscoped()
type StructMap ¶ added in v1.7.5
StructMap 其目的是将已知的参数作为结构体调用,未知的参数不动 例如 s.Data.Username Example: type User = StructMap[UserInfo]
func (StructMap[T]) MarshalJSON ¶ added in v1.7.5
MarshalJSON implements json.Marshaler.
func (*StructMap[T]) UnmarshalJSON ¶ added in v1.7.5
UnmarshalJSON implements json.Unmarshaler.
type Time ¶
func (Time) MarshalJSON ¶
func (*Time) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler.
type Universal
deprecated
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) 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 }
Deprecated: 请使用 goddd 生成代码,而非内嵌此接口