model

package
v0.0.0-...-d6b644b Latest Latest
Warning

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

Go to latest
Published: Apr 10, 2020 License: MIT Imports: 8 Imported by: 3

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Hash

func Hash(v interface{}) (uint64, error)

Hash FNV hash64 of v

Types

type Addon

type Addon struct {
	// ID xid 20位小写字符串全局id
	ID string `json:"id" gorm:"type:varchar(20);primary_key"`
	// 创建时间
	CreatedAt time.Time `json:"created_at"`
	// 最后更新时间
	UpdatedAt time.Time `json:"updated_at"`
}

Addon 实体附属表共用字段,硬删除,请确认只依赖于某实体

func (*Addon) BeforeCreate

func (*Addon) BeforeCreate(scope *gorm.Scope) error

BeforeCreate GORM hook

type Entity

type Entity struct {
	// ID xid 20位小写字符串全局id
	ID string `json:"id" gorm:"type:varchar(20);primary_key"`
	// 创建时间
	CreatedAt time.Time `json:"created_at"`
	// 最后更新时间
	UpdatedAt time.Time `json:"updated_at"`
	// 软删除
	DeletedAt *time.Time `json:"-" gorm:"index"`
}

Entity 实体共用字段,软删除

func (*Entity) BeforeCreate

func (*Entity) BeforeCreate(scope *gorm.Scope) error

BeforeCreate GORM hook

type Log

type Log struct {
	// ID xid 20位小写字符串全局id
	ID string `json:"id" gorm:"type:varchar(20);primary_key"`
	// 创建时间
	CreatedAt time.Time `json:"created_at"`
}

Log 日志共用字段,不可更新和删除

func (*Log) BeforeCreate

func (*Log) BeforeCreate(scope *gorm.Scope) error

BeforeCreate GORM hook

type Paginator

type Paginator struct {
	Page    string `query:"page"`
	PerPage string `query:"per_page"`
}

Paginator 分页器

func (Paginator) AddHeader

func (p Paginator) AddHeader(c echo.Context)

AddHeader 增加分页相关的响应 header

func (Paginator) Apply

func (p Paginator) Apply(tx *gorm.DB) (*gorm.DB, error)

Apply 将翻页信息应用的数据库查询,并在 echo Context 中添加相应 Header

type Sorter

type Sorter struct {
	Sort      string `query:"sort"`      // 排序字段的 gorm 名称
	Direction string `query:"direction"` // 方向 asc/desc
}

Sorter 排序器

func (Sorter) Apply

func (s Sorter) Apply(tx *gorm.DB, d string, limits ...string) (*gorm.DB, error)

Apply 将排序信息应用的数据库查询 d 为缺省值 例如 "created_at desc" 不会被此函数检查 写错了会造成数据库查询出错 limits 是 sort 字段的范围,防止攻击

type Source

type Source struct {
	// ID xid 20位小写字符串全局id
	ID string `json:"id" hash:"-" gorm:"type:varchar(20);primary_key"`
	// 创建时间
	CreatedAt time.Time `json:"created_at" hash:"-"`
	// 校验和
	Hash uint64 `json:"-" hash:"-" gorm:"index"`
}

Source 用作抓取数据临时存储,靠哈希值判断是否已存在记录,不可更新和删除

func (*Source) BeforeCreate

func (*Source) BeforeCreate(scope *gorm.Scope) error

BeforeCreate GORM hook

type Tracker

type Tracker struct {
	// ID xid 20位小写字符串全局id
	ID string `json:"id" hash:"-" gorm:"type:varchar(20);primary_key"`
	// 创建时间
	CreatedAt time.Time `json:"created_at" hash:"-"`
	// 最后更新时间
	UpdatedAt time.Time `json:"updated_at" hash:"-"`
	// 校验和
	Hash uint64 `json:"-" hash:"-" gorm:"index"`
}

Tracker 模型自动记录创建时间和更改时间,可用哈希快速查找业务数据是否重复。可配合一个历史表在更新和删除记录时记录历史。

func (*Tracker) BeforeCreate

func (*Tracker) BeforeCreate(scope *gorm.Scope) error

BeforeCreate GORM hook

Jump to

Keyboard shortcuts

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