基于 Gorm API,根据 Restful API 设计对资源进行维护并检索
简单说就是:自动 curd
先来看个栗子
package main
import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/go-eyas/toolkit/db"
"github.com/go-eyas/toolkit/db/resource"
)
type Article struct {
ID int64 `resource:"pk;search:none"`
Title string `resource:"create;update;search:like"`
Content string `resource:"create;update;search:like"`
Status byte `resource:"search:="`
}
func main() {
DB, err := db.Gorm(&db.Config{URI: "root:123456@(127.0.0.1:3306)/test"})
r := resource.NewGormResource(DB, &Article{})
/******* create resource ********/
err = r.Create(&Article{Title: "the title", Content: "the content"}) // 使用原本类型结构体
// 使用临时结构体
err = r.Create(&struct{
Title string
Content string
}{Title: "the title", Content: "the content"})
// 使用 map, 会自动匹配map 为 数据库列名,或者struct Key,或者 json key
err = r.Create(map[string]interface{}{"title": "the title", "content": "the content"})
/******* update resource ********/
err = r.Update(1, &Article{Title: "the title", Content: "the content"})
err = r.Update(1, &struct{
Title string
Content string
}{Title: "the title", Content: "the content"})
err = r.Update(1, map[string]interface{}{"title": "the title", "content": "the content"})
/******* delete resource ********/
err = r.Delete(1)
/******* list resource ********/
list := []*Article{}
// 查询所有记录
total, err := r.List(&list)
// 使用原始结构体设置查询参数,会忽略 0 值
total, err := r.List(&list, &Article{Title: "the title", Content: "the content"})
// 临时结构体,会自动匹配原始结构体的 struct key
total, err := r.List(&list, &struct{
Title string
Content string
Status byte
}{Status: 1})
// map 定义查询条件,key 会自动匹配struct key 和 数据库列名
total, err := r.List(&list, map[string]interface{}{
"status": 1,
})
// 查询,并指定列名排序,会使用数组顺序作为排序权重
total, err := r.List(&list, nil, []string{"id DESC", "status ASC"})
// 查询,并使用 map 指定排序,map无序,无法指定多个排序权重
total, err := r.List(&list, nil, map[string]string{
"id": "DESC",
"status": "ASC",
})
}
资源定义,继承gorm的所有struct tag,可以与gorm的模型结构体共用同一个
type Article struct {
ID int64 `resource:"pk;search:=;order:desc" json:"id"`
Title string `resource:"create;update;search:like" json:"title"`
Content string `resource:"create;update;search:like" json:"text"`
Status byte `resource:"search:=" json:"-"`
}
tag 的 key 为 resource
,每队键值使用 :
定义,以 ;
分隔
key | 默认值 | 说明 |
---|---|---|
pk | false | 是否为主键 |
search | = | 该字段的查询类型,sql语句的 where 匹配关系,为 - 时该字段不可作为查询条件 |
order | - | 查询时默认的排序规则 |
create | - | 该字段是否在调用 Create 时是否有效,即新增记录的时候是否给该字段赋值 |
update | - | 该字段是否在调用 Update 时是否有效,即修改记录的时候是否给该字段赋值 |
使用数据库配置创建资源实例
- conf 数据库配置
github.com/go-eyas/toolkit/db.Config
- model 资源模型
返回
- *Resource 资源实例
- *gorm.DB gorm 实例
- error 错误对象
基于gorm 实例,创建一个资源,返回资源实例
r := resource.NewGormResource(db, Article{})
创建资源,data 可以是结构体、map
- 为结构体时,匹配 struct Key,为结构体可以获取到创建完成后的其他字段,如id,时间等
- 为 map 时,顺序匹配struct Key、column key、json key
返回错误对象
更新资源, pk 是主键的值,data 是要更改的之后的数据
- 为结构体时,匹配 struct Key,为结构体可以获取到创建完成后的其他字段,如id,时间等
- 为 map 时,顺序匹配struct Key、column key、json key
返回错误对象
查询指定资源,pk是主键的值,查询到资源后赋值给 dest,dest必须为指针类型
删除指定主键值的记录
查询资源
- list 是数组或切片的指针对象,查询到数据后赋值到该变量
- query 可选,查询条件,查询类型会按照 tag 定义的search值,可为 struct 和 map, 为 map 时,顺序匹配struct Key、column key、json key
- query 有两个内部字段会用上, offset 和 limit,用于定义分页,返回值的总数会忽略这两个值的查询条件
- order 可选,定义排序规则,如果不传,默认会按照 tag 定义的 order 值,可为 []string 和 map,为 []string 可定义排序字段权重,为map时无序
返回值
- int64 为在query查询条件能查到的总数,查询条件不会包含 offset 和 limit
- error 错误对象
返回 gorm 绑定了主键查询条件的实例