Categorygithub.com/hsldymq/go-entity
modulepackage
0.0.3
Repository: https://github.com/hsldymq/go-entity.git
Documentation: pkg.go.dev

# README

介绍

基于sqlx与goqu库,封装了实体对象的基本CRUD方法,除数据库读写外,还实现了自定义缓存机制,在数据库读写过程中,自动使用和更新缓存

样例代码见example.go

Struct Tag

type User struct {
	ID       int64 `db:"user_id" entity:"primaryKey,autoIncrement"`
	CreateAt int64 `db:"create_at" entity:"refuseUpdate,returningInsert"`
	UpdateAt int64 `db:"update_at"`
	Other    bool  `db:"other" entity:"deprecated"`
}

每个字段属性,必须声明db tag,否则不会作为字段对待

实体配置,写在entity tag内

可用tag:

  • primaryKey 主键字段, 每个实体对象必须至少要声明一个
  • refuseUpdate 不允许更新,UPDATE时会被忽略,当设置了primaryKeyautoIncrementreturningUpdate时,这个配置会自动生效
  • autoIncrement 自增长主键,构造INSERT时此字段会被忽略
  • returningInsert insert时,这个字段会被放到RETURNING子句内返回,无论使用的数据库是否支持RETURNING
  • returningUpdate update时,这个字段会被放到RETURNING子句内返回,无论使用的数据库是否支持RETURNING
  • returning 等于同时使用returningInsertreturningUpdate
  • deprecated 标记为废弃,构造CRUD查询时会被忽略掉

# Packages

No description provided by the author

# Functions

Delete 删除entity.
DeleteCache 删除entity缓存.
Insert 插入新entity.
Load 从数据库载入entity.
NewMetadata 构造实体对象元数据.
SaveCache 保存entity缓存.
Update 更新entity.

# Constants

EventAfterDelete after delete entity.
EventAfterInsert after insert entity.
EventAfterUpdate after update entity.
EventBeforeDelete before delete entity.
EventBeforeInsert before insert entity.
EventBeforeUpdate before update entity.
EventUnknown 未定义事件.

# Variables

DefaultCacher 默认缓存存储.
DefaultEnDecoder 默认的缓存数据编码解码器.
DefaultExpiration 默认缓存时间.
ErrConflict 发生了数据冲突.
ReadTimeout 读取entity数据的默认超时时间.
WriteTimeout 写入entity数据的默认超时时间.

# Structs

CacheOption 缓存参数.
Column 字段信息.
JSONEnDecoder 默认缓存数据编码解码器.
Metadata 元数据.

# Interfaces

Cacheable 可缓存实体对象接口.
Cacher 缓存数据存储接口.
DB 数据库接口 sqlx.DB 和 sqlx.Tx 公共方法.
Decoder 解码缓存数据,将缓存还原到entity中.
Encoder 编码对象,转换可缓存的字节数组.
Entity 实体对象接口.

# Type aliases

Event 存储事件.