resource

package
v1.4.5 Latest Latest
Warning

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

Go to latest
Published: Dec 30, 2020 License: MIT Imports: 7 Imported by: 0

README

Resource 资源自动维护、检索

基于 Gorm API,根据 Restful API 设计对资源进行维护并检索

简单说就是:自动 curd

Usage

先来看个栗子

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",
  })

}

API

资源定义

资源定义,继承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 时是否有效,即修改记录的时候是否给该字段赋值
API 函数
resource.New(conf *db.Config, model interface{}) (*Resource, *gorm.DB, error)

使用数据库配置创建资源实例

  • conf 数据库配置 github.com/go-eyas/toolkit/db.Config
  • model 资源模型

返回

  • *Resource 资源实例
  • *gorm.DB gorm 实例
  • error 错误对象
resource.NewGormResource(db *gorm.DB, model interface{}) error

基于gorm 实例,创建一个资源,返回资源实例

r := resource.NewGormResource(db, Article{})
r.Create(data interface{}) error

创建资源,data 可以是结构体、map

  • 为结构体时,匹配 struct Key,为结构体可以获取到创建完成后的其他字段,如id,时间等
  • 为 map 时,顺序匹配struct Key、column key、json key

返回错误对象

r.Update(pk interface{}, data interface{}) error

更新资源, pk 是主键的值,data 是要更改的之后的数据

  • 为结构体时,匹配 struct Key,为结构体可以获取到创建完成后的其他字段,如id,时间等
  • 为 map 时,顺序匹配struct Key、column key、json key

返回错误对象

r.Detail(pk interface{}, dest interface{}) error

查询指定资源,pk是主键的值,查询到资源后赋值给 dest,dest必须为指针类型

r.Delete(pk interface{}) error

删除指定主键值的记录

r.List(list interface{}, query interface{}, order interface{}) (int64, error)

查询资源

  • 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 错误对象
r.Row(pk interface{}) *gorm.DB

返回 gorm 绑定了主键查询条件的实例

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Field

type Field struct {
	ColumnName string // 数据库列名
	StructKey  string // 结构体的名字

	JsonKey string // json key
	Search  string // 查询方式
	Order   string // 排序方式
	Update  bool   // 是否可更新,默认不可以
	Create  bool   // 是否可在创建时指定
	// contains filtered or unexported fields
}

type Pagination

type Pagination struct {
	Offset int
	Limit  int
}

type Resource

type Resource struct {
	Fields []*Field
	// contains filtered or unexported fields
}

func New

func New(conf *db.Config, model interface{}) (*Resource, *gorm.DB, error)

func NewGormResource

func NewGormResource(db *gorm.DB, v interface{}) *Resource

NewGormResource 实例化资源

example:

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:"-"`
}

r := NewGormResource(db, &Article{})

func (*Resource) Create

func (r *Resource) Create(v interface{}) error

Create 创建资源,支持传入 struct、map,创建前会重置 resource tag 未设置 create 的字段为0值,使得创建记录时忽略值

err := r.Create(&Article{Title: "Hello", Status: 2}) 这里 status 会被重置为 0 ,因为 status 的 resource tag 未设置 create

func (*Resource) CreateX

func (r *Resource) CreateX(v interface{}) error

CreateX 创建资源,支持传入 struct、map,传入的值均有效

err := r.CreateX(&Article{Title: "Hello", Status: 2}) 这里 status 成功设置值

func (*Resource) Delete

func (r *Resource) Delete(pk interface{}) error

Delete 删除指定主键的资源

err := r.Delete(1)

func (*Resource) Detail

func (r *Resource) Detail(pk interface{}, v interface{}) error

Detail 查询指定主键的记录

article := &Article{}
err := r.Detail(1, article)

func (*Resource) List

func (r *Resource) List(slice interface{}, args ...interface{}) (int64, error)

List 查询资源列表,提供查询条件,排序规则,查询列表,查询规则会以resource tag 的search 值为准

list := []*Article{}
total, err := r.List(&list, map[string]byte{"status": 1})

func (*Resource) ListPage

func (r *Resource) ListPage(slice interface{}, page *Pagination, args ...interface{}) (int64, error)

func (*Resource) Model

func (r *Resource) Model() *gorm.DB

Model 返回绑定了数据表的 gorm 实例

r.Model().Where("status = ?", status).Count()

func (*Resource) Row

func (r *Resource) Row(pk interface{}) *gorm.DB

Row 返回绑定了主键值的 gorm 实例

r.Row().Update("status", 1)

func (*Resource) Update

func (r *Resource) Update(pk interface{}, v interface{}) error

Update 更新资源,支持传入 struct、map,只会更新 resource tag 设置了 update 的字段

err := r.Update(1, map[string]string{"title": "after title"})

func (*Resource) UpdateX

func (r *Resource) UpdateX(pk interface{}, v interface{}) error

UpdateX 更新资源,支持传入 struct、map,更新传入的所有字段,如果传入的是 struct ,则会忽略 0 值

err := r.UpdateX(1, map[string]byte{"status": 1})

Jump to

Keyboard shortcuts

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