crud

package module
v1.0.6 Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2020 License: Apache-2.0 Imports: 4 Imported by: 0

README

A little opinionated CRUD interface

Documentation Go Report Card Maintainability Coverage Status CircleCI

Use case

Let's take a simple use case, you have an entity A, and you want to easily enable a CRUD API.

Simply create a "manager" fulfilling the induzo/crud MgrI interface for the entity A you want to enable CRUD for. This interface (described in mgr.go) is as follows:

type MgrI interface {
    NewEmptyEntity() interface{}
    Create(context.Context, interface{}, io.Reader) (interface{}, error)
    Delete(context.Context, xid.ID) error
    Get(context.Context, xid.ID) (interface{}, error)
    GetList(context.Context, ListModifiers) (interface{}, error)
    Update(context.Context, xid.ID, interface{}, io.Reader) (interface{}, error)
    PartialUpdate(context.Context, xid.ID, PartialUpdateData, io.Reader) error
    MapErrorToHTTPError(error) *gohttperror.ErrResponse
}

You can see an example of implementation in the mock folder.

Once this is done, you can just use this newly created manager and wrap it to enable the API.

You want to spawn a REST API, following the std library http handler? Easy, just wrap you manager with the REST implementation in the rest folder.

And there you go, you have a complete REST API in 5 lines

Example

A very short example with the rest wrapper in the example folder.

Coming soon

If this interface is successful, we are planning to simply add more wrappers on top of the REST one:

  • GRPCWeb
  • CQRS

Opinions

  • Your entity ids should be using github.com/rs/xid
  • Your crud errors should be handlable by an httpresponse

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ListModifiers

type ListModifiers map[string][]string

ListModifiers will modify the query, it can be url.Values for example Mostly used for Get and GetList

type MgrI

type MgrI interface {
	NewEmptyEntity() interface{}
	Create(context.Context, interface{}, io.Reader) (interface{}, error)
	Delete(context.Context, xid.ID) error
	Get(context.Context, xid.ID) (interface{}, error)
	GetList(context.Context, ListModifiers) (interface{}, error)
	Update(context.Context, xid.ID, interface{}, io.Reader) (interface{}, error)
	PartialUpdate(context.Context, xid.ID, PartialUpdateData, io.Reader) error
	MapErrorToHTTPError(error) *gohttperror.ErrResponse
}

MgrI is the interface to initialize the new entity mgr

type PartialUpdateData

type PartialUpdateData map[string]interface{}

PartialUpdateData represent the partial update data Used for PartialUpdate or PATCH in rest

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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