crudify

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Aug 30, 2020 License: MIT Imports: 10 Imported by: 0

README

go-crudify

Easily create CRUD endpoints with a given MUX and Database. The perfect package to prototype and build applications with.

Example

package main

import (
	"log"

	"github.com/jinzhu/gorm"
	"github.com/josebalius/go-crudify"
	databaseAdapter "github.com/josebalius/go-crudify/adapters/database/gorm"
	routerAdapter "github.com/josebalius/go-crudify/adapters/router/echo"
	"github.com/labstack/echo"
	"github.com/pkg/errors"

	_ "github.com/jinzhu/gorm/dialects/sqlite"
)

type User struct {
	gorm.Model
	Name string
}

func main() {
	e := echo.New()

	db, err := gorm.Open("sqlite3", "test.db")
	if err != nil {
		log.Fatal(errors.Wrap(err, "open database"))
	}
	defer db.Close()

	db.AutoMigrate(&User{})

	if err := crudify.NewEndpoint(
		crudify.WithRouter(routerAdapter.NewEchoRouter(e)),
		crudify.WithDatabase(databaseAdapter.NewGormAdapter(db)),
		crudify.WithModel(&User{}),
	); err != nil {
		log.Fatal(err)
	}

	e.Logger.Fatal(e.Start(":8000"))
}

This creates the following endpoints:

GET /users
POST /users
GET /users/:id
PUT /users/:id
DELETE /users/:id

TODOs

  • Setup go.mod
  • Cleanup TODOs in the code
  • Support non-integer IDs from gorm / database
  • Introduce adapters for router & support standard lib mux
  • Introduce adapters for database & support standard lib dbo
  • Tests
  • Permission behavior
  • Instrumentation support
  • Logging support
  • Support for middlewares
  • Improve documentation in code and docs
  • Your feature! Submit an issue or PR

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewEndpoint

func NewEndpoint(opts ...Option) error

Types

type Endpoint

type Endpoint struct {
	Options        *Options
	ControllerName string
}

type Option

type Option func(opts *Options) error

func WithControllerName

func WithControllerName(controllerName string) Option

func WithDatabase

func WithDatabase(db database.Database) Option

func WithModel

func WithModel(model interface{}) Option

func WithRouter

func WithRouter(router router.Router) Option

type Options

type Options struct {
	Router         router.Router
	DB             database.Database
	Model          interface{}
	ControllerName string
}

Directories

Path Synopsis
adapters
examples

Jump to

Keyboard shortcuts

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