orm

package
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2024 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrRevordNotFound = gorm.ErrRecordNotFound
	ErrDuplicatedKey  = gorm.ErrDuplicatedKey
)

Functions

func Delete

func Delete(db *gorm.DB, model any, opts ...QueryOption) error

func DeleteWithContext added in v1.0.2

func DeleteWithContext(ctx context.Context, db *gorm.DB, model any, opts ...QueryOption) error

func Find

func Find[T any](db *gorm.DB, out *[]*T, p Pager, opts ...QueryOption) (int64, error)

func FindWithContext added in v1.0.2

func FindWithContext[T any](ctx context.Context, db *gorm.DB, out *[]*T, p Pager, opts ...QueryOption) (int64, error)

func First

func First(db *gorm.DB, out any, opts ...QueryOption) error

func FirstWithContext added in v1.0.2

func FirstWithContext(ctx context.Context, db *gorm.DB, out any, opts ...QueryOption) error

func GenerateRandomString

func GenerateRandomString(length int) string

func IsDuplicatedKey

func IsDuplicatedKey(err error) bool

func IsErrRecordNotFound

func IsErrRecordNotFound(err error) bool

func JsonUnmarshal

func JsonUnmarshal(input, obj any) error

JsonUnmarshal 将 input 反序列化到 obj 上

func New

func New(debug bool, dialector gorm.Dialector, cfg Config, w logger.Writer) (*gorm.DB, error)

New ...

func Update

func Update[T any](db *gorm.DB, model *T, changeFn func(*T), opts ...QueryOption) error

func UpdateWithContext added in v1.0.2

func UpdateWithContext[T any](ctx context.Context, db *gorm.DB, model *T, changeFn func(*T), opts ...QueryOption) error

Types

type Config

type Config struct {
	MaxIdleConns    int
	MaxOpenConns    int
	ConnMaxLifetime time.Duration
	SlowThreshold   time.Duration
}

type DeletedAt

type DeletedAt = gorm.DeletedAt

type DeletedModel

type DeletedModel struct {
	Model
	DeletedAt gorm.DeletedAt `gorm:"index;comment:删除时间" json:"-"`
}

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 NewEngine

func NewEngine(db *gorm.DB) Engine

func (Engine) DeleteOne

func (e Engine) DeleteOne(model Tabler, opts ...Option) error

func (Engine) Find

func (e Engine) Find(model Tabler, bean any, opts ...Option) (total int64, err error)

func (Engine) FirstOrCreate

func (e Engine) FirstOrCreate(b any) (bool, error)

FirstOrCreate true:创建;false:查询

func (Engine) InsertOne

func (e Engine) InsertOne(model Tabler) error

func (Engine) NextSeq

func (e Engine) NextSeq(model Tabler) (nextID int, err error)

NextSeq 获取序列下一个值

func (Engine) UpdateOne

func (e Engine) UpdateOne(model Tabler, id int, data map[string]any) error

type Logger

type Logger struct {
	*slog.Logger
	// contains filtered or unexported fields
}

func NewLogger

func NewLogger(l *slog.Logger, debug bool, slow time.Duration) *Logger

NewLogger 封装日志

func (*Logger) Printf

func (l *Logger) Printf(format string, v ...interface{})

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

func (*Model) BeforeCreate

func (d *Model) BeforeCreate(*gorm.DB) error

func (*Model) BeforeUpdate

func (d *Model) BeforeUpdate(*gorm.DB) error

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 NewModelWithStrID

func NewModelWithStrID(id string) ModelWithStrID

NewModelWithStrID 新建模型

func (*ModelWithStrID) BeforeCreate

func (d *ModelWithStrID) BeforeCreate(*gorm.DB) error

func (*ModelWithStrID) BeforeUpdate

func (d *ModelWithStrID) BeforeUpdate(*gorm.DB) error

type Option

type Option func(*gorm.DB)

func WithCreatedAt

func WithCreatedAt(startAt, endAt int64) Option

func WithID

func WithID(id int) Option

WithID ...

func WithLimit

func WithLimit(limit, offset int) Option

type Pager

type Pager interface {
	Limit() int
	Offset() int
}

type QueryOption

type QueryOption func(*gorm.DB) *gorm.DB

QueryOption ..

func OrderBy

func OrderBy(value interface{}) QueryOption

OrderBy 排序条件

func Where

func Where(query interface{}, args ...interface{}) QueryOption

Where 查询条件

type Scaner

type Scaner interface {
	Scan(input interface{}) error
}

Scaner 所有模型内组合的结构体,必须满足该接口

type Tabler

type Tabler interface {
	TableName() string
}

Tabler 模型需要用指针接收器实现接口

type Time

type Time struct {
	time.Time
}

func Now

func Now() Time

func (Time) MarshalJSON

func (t Time) MarshalJSON() ([]byte, error)

func (*Time) Scan

func (t *Time) Scan(input interface{}) error

Scan implements scaner

func (*Time) UnmarshalJSON

func (t *Time) UnmarshalJSON(b []byte) error

UnmarshalJSON implements json.Unmarshaler.

func (Time) Value

func (t Time) Value() (driver.Value, error)

type Type

type Type[T any] struct {
	// contains filtered or unexported fields
}

func (Type[T]) Create

func (t Type[T]) Create(model *T) error

func (Type[T]) Delete

func (t Type[T]) Delete(model *T, opts ...QueryOption) error

Delete 通用删除

func (Type[T]) Find

func (t Type[T]) Find(out *[]*T, p Pager, opts ...QueryOption) (int64, error)

func (*Type[T]) First

func (t *Type[T]) First(out *T, opts ...QueryOption) error

First 通用查询

func (Type[T]) Update

func (t Type[T]) Update(model *T, changeFn func(*T), opts ...QueryOption) error

Update 通用更新

type Universal

type Universal[T any] interface {
	First(*T, ...QueryOption) error
	Update(*T, func(*T), ...QueryOption) error
	Delete(*T, ...QueryOption) error
	Create(*T) error
	Find(out *[]*T, p Pager, opts ...QueryOption) (int64, error)
}

Universal 通用增删改查

func NewUniversal

func NewUniversal[T any](db *gorm.DB) Universal[T]

Jump to

Keyboard shortcuts

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