gocruddy

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Mar 6, 2024 License: BSD-3-Clause Imports: 6 Imported by: 0

README

Becklyn gocruddy

CI codecov

Made with ❤ by Becklyn

A framework that supports you in creating CRUD APIs using go (golang).

This framework is useful for applications that use fiber as router and gorm as ORM.

Installation

go get -u github.com/Becklyn/gocruddy

Usage

You can find a working example in the example directory of this repository. The folder also contains a postman template that demonstrates the API usage.

Development

Set up your local development environment:

make setup

Add new modules / dependencies:

make install MOD=your.dependency/name

Tidy up modules:

make tidy

Test your implementation:

make test

Calculate the code coverage: (This currently requires a local go installation)

make cover

References

This project makes use of some really great packages. Please make sure to check them out!

Package Usage
github.com/ao-concepts/logging Logging
github.com/ao-concepts/storage DB abstraction
github.com/gofiber/fiber HTTP router
github.com/stretchr/testify Testing
gorm.io/gorm ORM

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Delete

func Delete(c Container, config CrudConfig) fiber.Handler

func Get

func Get(c Container, config CrudConfig) fiber.Handler

func Post

func Post(c Container, config CrudConfig) fiber.Handler

func Put

func Put(c Container, config CrudConfig) fiber.Handler

func RegisterCrudRoutes

func RegisterCrudRoutes(router fiber.Router, c Container)

RegisterCrudRoutes register all crud routes based on their configuration

Types

type Container

type Container interface {
	GetCrudConfigs() []CrudConfig
	GetLogger() logging.Logger
	GetDatabase() Database
	GetCrudRepo() CrudRepository
}

Container service container interface

type CrudConfig

type CrudConfig interface {
	UseContainer(c Container)
	GetEntityEntry() interface{}
	GetBasePath() string
	CreateGetFilter(ctx *fiber.Ctx) DatabaseFilter
	CreateUpdateFilter(ctx *fiber.Ctx) DatabaseFilter
	CreateDeleteFilter(ctx *fiber.Ctx) DatabaseFilter
	SerializeList(entries []interface{}, ctx *fiber.Ctx, tx *storage.Transaction) (serialized interface{}, err error)
	MapPostEntry(entry interface{}, ctx *fiber.Ctx, tx *storage.Transaction) (mapped Entry, err error)
	MapPutEntry(entry interface{}, ctx *fiber.Ctx, tx *storage.Transaction) (mapped Entry, err error)
}

CrudConfig configuration of a crud api for an entity

type CrudContainer

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

CrudContainer is a basic service container that can be used with gocruddy

func (*CrudContainer) GetCrudConfigs

func (c *CrudContainer) GetCrudConfigs() []CrudConfig

GetCrudConfigs return all registered crud configurations

func (*CrudContainer) UseCrudConfig

func (c *CrudContainer) UseCrudConfig(crud CrudConfig)

UseCrudConfig register a crud configuration

type CrudRepository

type CrudRepository interface {
	Repo
	GetAllEntries(tx *storage.Transaction, filter DatabaseFilter, t interface{}) (entries []interface{}, err error)
	GetByID(tx *storage.Transaction, id uint, filter DatabaseFilter, t interface{}) (entry interface{}, err error)
}

type Database

type Database interface {
	UseTransaction(fn storage.HandlerFunc) error
	Gorm() *gorm.DB
	Begin() (tx *storage.Transaction, err error)
}

Database interface

type DatabaseFilter

type DatabaseFilter func(db *gorm.DB) *gorm.DB

DatabaseFilter a function that filters a database query

type Entry

type Entry interface {
	GetID() uint
}

Entry a single instance of an entity

type Error

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

func NewError

func NewError(responseCode int, err error) Error

func (Error) Error

func (e Error) Error() string

func (Error) Respond

func (e Error) Respond() Error

func (Error) Unwrap

func (e Error) Unwrap() error

type Repo

type Repo interface {
	Insert(tx *storage.Transaction, entry interface{}) (err error)
	Update(tx *storage.Transaction, entry interface{}) (err error)
	Delete(tx *storage.Transaction, entry interface{}) (err error)
	Remove(tx *storage.Transaction, entry interface{}) (err error)
}

type Repository

type Repository struct {
	storage.Repository
}

Repository crud data repository

func (*Repository) GetAllEntries

func (r *Repository) GetAllEntries(tx *storage.Transaction, filter DatabaseFilter, t interface{}) (entries []interface{}, err error)

GetAllEntries returns all entities matching a filter

func (*Repository) GetByID

func (r *Repository) GetByID(tx *storage.Transaction, id uint, filter DatabaseFilter, t interface{}) (entry interface{}, err error)

GetByID fetches an entity by its unique id

Directories

Path Synopsis
app

Jump to

Keyboard shortcuts

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