crudman

package module
v0.0.0-...-9f4dcca Latest Latest
Warning

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

Go to latest
Published: Oct 10, 2020 License: MIT Imports: 2 Imported by: 1

README

crudman

crudman completes common operations of adding, deleting, modifying and querying, making data operation more convenient

Example

Because only gorm is implemented now, take gorm as an example

  • For a complete example, you can view the example directory

  • Build a data structure

    type user struct {
    	gorm.Model
    	Nickname string `json:"nickname" gorm:"column:nickname;type:varchar(16)" validate:"required"`
    	Age      uint8  `json:"age" gorm:"column:age;"`
    }
  • Implement crudman.Tabler
// TableName table name
func (user) TableName() string {
	return "members"
}
  • Initialize the DB object
db, err = gorm.Open(sqlite.Open("file:pager?mode=memory&cache=shared&_fk=1"), &gorm.Config{PrepareStmt: true, Logger: logger.New(
		log.New(os.Stdout, "\r\n", log.LstdFlags), // io writer
		logger.Config{
			SlowThreshold: time.Second,
			LogLevel:      logger.Info,
			Colorful:      false,
		},
	)})

You can initialize and migrate some data.

  • func main
	var crud = crudman.New() // get a crudman instance

    // register your custom struct validator
	driver.SetValidator(func(obj interface{}) interface{} {
		return validator.New().Struct(obj)
	})

    // register a data struct
	crud.Register(driver.NewGorm(db, "ID"), user{}, crudman.SetRoute("/crud"))

    // register http handler
	http.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
		var obj, err = crud.Handler(writer, request)
		var body = map[string]interface{}{
			"data": obj,
			"msg": func() string {
				if err != nil {
					return err.Error()
				}
				return ""
			}(),
		}

		data, _ := json.Marshal(body)
		writer.Header().Set("Content-Type", "application/json")
		_, _ = writer.Write(data)
	})
	log.Fatalln(http.ListenAndServe(":3359", nil))

Documentation

Overview

Package crudman curd api

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ManagerInterface

type ManagerInterface interface {
	List(r *http.Request) interface{}
	Post(r *http.Request) (interface{}, error)
	Put(r *http.Request) (interface{}, error)
	Delete(r *http.Request) error
	GetRoute() string
	SetRoute(route string)
	SetTableTyp(typ reflect.Type)
	GetTableTyp() reflect.Type
	GetTable() Tabler
	SetTable(table Tabler)
}

ManagerInterface manger interface

type Managers

type Managers struct {
	// contains filtered or unexported fields
}

Managers all managers

func New

func New() *Managers

New get a new Managers object

func (*Managers) Forbidden

func (managers *Managers) Forbidden(w http.ResponseWriter, r *http.Request)

Forbidden 403

func (*Managers) Get

func (managers *Managers) Get(route string) (ManagerInterface, bool)

Get get exist manager

func (*Managers) Handler

func (managers *Managers) Handler(w http.ResponseWriter, r *http.Request) (obj interface{}, err error)

Handler http handler, support GET->list data, POST->create data, PUT->update data, DELETE->delete data

func (*Managers) NotFound

func (managers *Managers) NotFound(w http.ResponseWriter, r *http.Request)

NotFound 404

func (*Managers) Register

func (managers *Managers) Register(manager ManagerInterface, entity Tabler, setups ...Setup) *Managers

Register register manager you can inherit GormManager or MongoManager and then override the method to implement custom operations

type Route

type Route struct {
	// contains filtered or unexported fields
}

Route router

func SetRoute

func SetRoute(r string) *Route

SetRoute setup custom route

type Setup

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

Setup setting

type Tabler

type Tabler interface {
	TableName() string
}

Tabler any structure that uses this library needs to implement this interface, for example, in mysql, this is identified as the table name

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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