curd

package module
v0.0.0-...-f989c2c Latest Latest
Warning

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

Go to latest
Published: May 16, 2021 License: MIT Imports: 3 Imported by: 0

README

Gin Curd

Provide CURD auxiliary library for gin

Github Actions Coveralls github GitHub go.mod Go version Go Report Card Release GitHub license

Setup

Install the curd auxiliary library

go get github.com/kainonly/gin-curd

Quick Start

First you need to define gorm

var db *gorm.DB
var err error

if db, err = gorm.Open(mysql.Open(dsn), &gorm.Config{}); err != nil {
    c.Error(err)
}

curd = curd.Initialize(db)

You can also refer to the lab-api project to use dependency injection initialization

Operates(operates ...Operator) *execute

Used to plan curd operations, Executable as Originlists, Lists, Get, Add, Edit, Delete results, the following are some examples of lab-api

Originlists() interface{}

Execute origin lists

type originListsBody struct {
	curd.OriginLists
}

func (c *Controller) OriginLists(ctx *gin.Context) interface{} {
	var body originListsBody
	var err error
	if err = ctx.ShouldBindJSON(&body); err != nil {
		return err
	}
	return c.Curd.Operates(
		curd.Plan(model.Acl{}, body),
	).Originlists()
}
Lists() interface{}

Execute lists

type listsBody struct {
	curd.Lists
}

func (c *Controller) Lists(ctx *gin.Context) interface{} {
	var body listsBody
	var err error
	if err = ctx.ShouldBindJSON(&body); err != nil {
		return err
	}
	return c.Curd.Operates(
		curd.Plan(model.Acl{}, body),
	).Lists()
}
Get() interface{}

Execute get

type getBody struct {
	curd.Get
}

func (c *Controller) Get(ctx *gin.Context) interface{} {
	var body getBody
	var err error
	if err = ctx.ShouldBindJSON(&body); err != nil {
		return err
	}
	return c.Curd.Operates(
		curd.Plan(model.Acl{}, body),
	).Get()
}
Add(value interface{}) interface{}

Execute add

  • value interface{} insert data
type addBody struct {
	Key    string              `binding:"required"`
	Name   datatype.JSONObject `binding:"required"`
	Read   datatype.JSONArray
	Write  datatype.JSONArray
	Status bool
}

func (c *Controller) Add(ctx *gin.Context) interface{} {
	var body addBody
	var err error
	if err = ctx.ShouldBindJSON(&body); err != nil {
		return err
	}
	data := model.Acl{
		Key:    body.Key,
		Name:   body.Name,
		Read:   body.Read,
		Write:  body.Write,
		Status: body.Status,
	}
	return c.Curd.Operates(
		curd.After(func(tx *gorm.DB) error {
			c.clearcache()
			return nil
		}),
	).Add(&data)
}
Edit(value interface{}) interface{}

Execute edit

  • value interface{} update data
type editBody struct {
	curd.Edit
	Key    string
	Name   map[string]interface{} `json:"name"`
	Read   datatype.JSONArray     `json:"read"`
	Write  datatype.JSONArray     `json:"write"`
	Status bool
}

func (c *Controller) Edit(ctx *gin.Context) interface{} {
	var body editBody
	var err error
	if err = ctx.ShouldBindJSON(&body); err != nil {
		return err
	}
	data := model.Acl{
		Key:    body.Key,
		Name:   body.Name,
		Read:   body.Read,
		Write:  body.Write,
		Status: body.Status,
	}
	return c.Curd.Operates(
		curd.Plan(model.Acl{}, body),
		curd.After(func(tx *gorm.DB) error {
			c.clearcache()
			return nil
		}),
	).Edit(data)
}
Delete() interface{}

Execute delete

type deleteBody struct {
	curd.Delete
}

func (c *Controller) Delete(ctx *gin.Context) interface{} {
	var body deleteBody
	var err error
	if err = ctx.ShouldBindJSON(&body); err != nil {
		return err
	}
	return c.Curd.Operates(
		curd.Plan(model.Acl{}, body),
		curd.After(func(tx *gorm.DB) error {
			c.clearcache()
			return nil
		}),
	).Delete()
}
Option

Global default configuration

  • Orders Orders Default order by, Orders{"id": "desc"}
  • UpdateStatus string Default updated status field, "status"
  • UpdateOmit []string Default updated exclude fields, []string{"id", "create_time"}
curd.Set(curd.Option{
    ...
})
Operator
Plan(model interface{}, body interface{}) Operator

Plan a model expression

  • model interface{} GORM defined model
  • body interface{} Request body
Where(value Conditions) Operator

Set condition array

  • value Conditions Conditions Condition array, Conditions [][]interface{}
SubQuery(fn func(tx *gorm.DB) *gorm.DB) Operator

Set sub query

  • fn func(tx *gorm.DB) *gorm.DB
OrderBy(value Orders) Operator

Set order by

  • value Orders Orders, Orders map[string]string
Field(value []string, exclude bool) Operator

Set selecting specific fields

  • value []string fields
  • exclude bool
Update(status string) Operator

Set update

  • status string When switch is true, update the status field
After(fn func(tx *gorm.DB) error) Operator

After hook, when the return is error, the transaction will be rolled back

  • fn fn func(tx *gorm.DB) error
Prep(fn func(tx *gorm.DB) error) Operator

Preparation hook, the transaction will be terminated early when the return is error

  • fn fn func(tx *gorm.DB) error
Body Definition

Used to request structure body embedding, include go-playground/validator verification of json

// General definition of origin list request body
type OriginLists struct {
	Conditions `json:"where" binding:"omitempty,gte=0,dive,len=3,dive,required"`
	Orders     `json:"order" binding:"omitempty,gte=0,dive,keys,endkeys,oneof=asc desc,required"`
}

// General definition of list request body
type Lists struct {

	// Condition array
	Conditions `json:"where" binding:"omitempty,gte=0,dive,len=3,dive,required"`

	// Order by
	Orders `json:"order" binding:"omitempty,gte=0,dive,keys,endkeys,oneof=asc desc,required"`

	// Page definition
	Pagination `json:"page" binding:"required"`
}

// General definition of get request body
type Get struct {

	// Primary key
	Id interface{} `json:"id" binding:"required_without=Conditions"`

	// Condition array
	Conditions `json:"where" binding:"required_without=Id,gte=0,dive,len=3,dive,required"`

	// Order by
	Orders `json:"order" binding:"omitempty,gte=0,dive,keys,endkeys,oneof=asc desc,required"`
}

// General definition of edit request body, choose one of primary key or condition array
type Edit struct {

	// Primary key
	Id interface{} `json:"id" binding:"required_without=Conditions"`

	// Only the status field is updated
	Switch bool `json:"switch"`

	// Condition array
	Conditions `json:"where" binding:"required_without=Id,gte=0,dive,len=3,dive,required"`
}

// General definition of delete request body, choose one of primary key or condition array
type Delete struct {

	// Primary key
	Id interface{} `json:"id" binding:"required_without=Conditions"`

	// Condition array
	Conditions `json:"where" binding:"required_without=Id,gte=0,dive,len=3,dive,required"`
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Conditions

type Conditions [][]interface{}

Conditions array condition definition

type Curd

type Curd struct {
	*Option
	// contains filtered or unexported fields
}

func Initialize

func Initialize(tx *gorm.DB) *Curd

Initialize the curd auxiliary library

@param `tx` *gorm.DB
@return Curd

func (*Curd) Operates

func (c *Curd) Operates(operates ...Operator) *execute

Start Curd operation process

@param `operates` ...Operator
@return execute

func (*Curd) Set

func (c *Curd) Set(option Option)

Set curd auxiliary configuration

@param `option` Option

type Delete

type Delete struct {

	// Primary key
	Id interface{} `json:"id" binding:"required_without=Conditions"`

	// Condition array
	Conditions `json:"where" binding:"required_without=Id,gte=0,dive,len=3,dive,required"`
}

Delete general definition of delete request body, choose one of primary key or condition array

type Edit

type Edit struct {

	// Primary key
	Id interface{} `json:"id" binding:"required_without=Conditions"`

	// Only the status field is updated
	Switch bool `json:"switch"`

	// Condition array
	Conditions `json:"where" binding:"required_without=Id,gte=0,dive,len=3,dive,required"`
}

Edit general definition of edit request body, choose one of primary key or condition array

type Get

type Get struct {

	// Primary key
	Id interface{} `json:"id" binding:"required_without=Conditions"`

	// Condition array
	Conditions `json:"where" binding:"required_without=Id,gte=0,dive,len=3,dive,required"`

	// Order by
	Orders `json:"order" binding:"omitempty,gte=0,dive,keys,endkeys,oneof=asc desc,required"`
}

Get general definition of get request body

type Lists

type Lists struct {

	// Condition array
	Conditions `json:"where" binding:"omitempty,gte=0,dive,len=3,dive,required"`

	// Order by
	Orders `json:"order" binding:"omitempty,gte=0,dive,keys,endkeys,oneof=asc desc,required"`

	// Page definition
	Pagination `json:"page" binding:"required"`
}

Lists general definition of list request body

type Operator

type Operator interface {
	// contains filtered or unexported methods
}

func After

func After(fn func(tx *gorm.DB) error) Operator

After hook, when the return is error, the transaction will be rolled back

func Field

func Field(value []string, exclude bool) Operator

Field set selecting specific fields

func OrderBy

func OrderBy(value Orders) Operator

OrderBy set order by

func Plan

func Plan(model interface{}, body interface{}) Operator

Plan a model expression

func Prep

func Prep(fn func(tx *gorm.DB) error) Operator

Prep Preparation hook, the transaction will be terminated early when the return is error

func SubQuery

func SubQuery(fn func(tx *gorm.DB) *gorm.DB) Operator

SubQuery set sub query

func Update

func Update(status string) Operator

Update set update

func Where

func Where(value Conditions) Operator

Where set condition array

type Option

type Option struct {

	// Default order by, Orders{"id": "desc"}
	Orders Orders

	// Default updated status field, "status"
	UpdateStatus string

	// Default updated exclude fields, []string{"id", "create_time"}
	UpdateOmit []string
}

type Orders

type Orders map[string]string

Orders definition

type OriginLists

type OriginLists struct {
	Conditions `json:"where" binding:"omitempty,gte=0,dive,len=3,dive,required"`
	Orders     `json:"order" binding:"omitempty,gte=0,dive,keys,endkeys,oneof=asc desc,required"`
}

OriginLists general definition of origin list request body

type Pagination

type Pagination struct {

	// the paging index
	Index int64 `json:"index" binding:"gt=0,number,required"`

	// the page size
	Limit int64 `json:"limit" binding:"gt=0,number,required"`
}

Pagination paging request field definition.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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