Documentation ¶
Index ¶
- Constants
- Variables
- func DefaultDeserializer[T any](op CrudOperation, ctx Context) (T, error)
- func DefaultDeserializerMany[T any](op CrudOperation, ctx Context) ([]T, error)
- func DefaultOperatorMap() map[string]string
- func GetResourceName[T any]() (string, string)
- func GetResourceTitle[T any]() (string, string)
- func SetOperatorMap(om map[string]string)
- type APIListResponse
- type APIResponse
- type Context
- type Controller
- func (c *Controller[T]) Create(ctx Context) error
- func (c *Controller[T]) CreateBatch(ctx Context) error
- func (c *Controller[T]) Delete(ctx Context) error
- func (c *Controller[T]) DeleteBatch(ctx Context) error
- func (c *Controller[T]) GetMetadata() router.ResourceMetadata
- func (c *Controller[T]) Index(ctx Context) error
- func (c *Controller[T]) RegisterRoutes(r Router)
- func (c *Controller[T]) Show(ctx Context) error
- func (c *Controller[T]) Update(ctx Context) error
- func (c *Controller[T]) UpdateBatch(ctx Context) error
- type CrudOperation
- type DefaultResponseHandler
- func (h DefaultResponseHandler[T]) OnData(c Context, data T, op CrudOperation, filters ...*Filters) error
- func (h DefaultResponseHandler[T]) OnEmpty(c Context, op CrudOperation) error
- func (h DefaultResponseHandler[T]) OnError(c Context, err error, op CrudOperation) error
- func (h DefaultResponseHandler[T]) OnList(c Context, data []T, op CrudOperation, filters *Filters) error
- type Filters
- type NotFoundError
- type Option
- type Order
- type RelationFilter
- type RelationInfo
- type Request
- type ResourceController
- type ResourceHandler
- type Response
- type ResponseHandler
- type Router
- type RouterRouteInfo
- type ValidationError
Constants ¶
const ( TAG_CRUD = "crud" TAG_BUN = "bun" TAG_JSON = "json" TAG_KEY_RESOURCE = "resource" )
Variables ¶
var DefaultLimit = 25
var DefaultOffset = 0
Functions ¶
func DefaultDeserializer ¶
func DefaultDeserializer[T any](op CrudOperation, ctx Context) (T, error)
DefaultDeserializer provides a generic deserializer.
func DefaultDeserializerMany ¶ added in v0.1.0
func DefaultDeserializerMany[T any](op CrudOperation, ctx Context) ([]T, error)
DefaultDeserializerMany provides a generic deserializer.
func DefaultOperatorMap ¶
func GetResourceName ¶
GetResourceName returns the singular and plural resource names for type T. It first checks for a 'crud:"resource:..."' tag on any embedded fields. If found, it uses the specified resource name. Otherwise, it derives the name from the type's name.
func GetResourceTitle ¶ added in v0.1.1
func SetOperatorMap ¶
Types ¶
type APIListResponse ¶ added in v0.0.2
type APIResponse ¶ added in v0.0.2
type Controller ¶
type Controller[T any] struct { Repo repository.Repository[T] // contains filtered or unexported fields }
Controller handles CRUD operations for a given model.
func NewController ¶
func NewController[T any](repo repository.Repository[T], opts ...Option[T]) *Controller[T]
NewController creates a new Controller with functional options.
func (*Controller[T]) Create ¶
func (c *Controller[T]) Create(ctx Context) error
func (*Controller[T]) CreateBatch ¶ added in v0.1.0
func (c *Controller[T]) CreateBatch(ctx Context) error
func (*Controller[T]) Delete ¶
func (c *Controller[T]) Delete(ctx Context) error
func (*Controller[T]) DeleteBatch ¶ added in v0.1.0
func (c *Controller[T]) DeleteBatch(ctx Context) error
func (*Controller[T]) GetMetadata ¶ added in v0.1.1
func (c *Controller[T]) GetMetadata() router.ResourceMetadata
GetMetadata implements router.MetadataProvider, we use it to generate the required info that will be used to create a OpenAPI spec or something similar
func (*Controller[T]) Index ¶ added in v0.1.0
func (c *Controller[T]) Index(ctx Context) error
Index supports different query string parameters: GET /users?limit=10&offset=20 GET /users?order=name asc,created_at desc GET /users?select=id,name,email GET /users?include=company,profile GET /users?name__ilike=John&age__gte=30 GET /users?name__and=John,Jack GET /users?name__or=John,Jack
func (*Controller[T]) RegisterRoutes ¶
func (c *Controller[T]) RegisterRoutes(r Router)
func (*Controller[T]) Show ¶ added in v0.1.0
func (c *Controller[T]) Show(ctx Context) error
Show supports different query string parameters: GET /user?include=Company,Profile GET /user?select=id,age,email
func (*Controller[T]) Update ¶
func (c *Controller[T]) Update(ctx Context) error
func (*Controller[T]) UpdateBatch ¶ added in v0.1.0
func (c *Controller[T]) UpdateBatch(ctx Context) error
type CrudOperation ¶
type CrudOperation string
CrudOperation defines the type for CRUD operations.
const ( OpCreate CrudOperation = "create" OpCreateBatch CrudOperation = "create:batch" OpRead CrudOperation = "read" OpList CrudOperation = "list" OpUpdate CrudOperation = "update" OpUpdateBatch CrudOperation = "update:batch" OpDelete CrudOperation = "delete" OpDeleteBatch CrudOperation = "delete:batch" )
type DefaultResponseHandler ¶ added in v0.0.2
type DefaultResponseHandler[T any] struct{}
func (DefaultResponseHandler[T]) OnData ¶ added in v0.0.2
func (h DefaultResponseHandler[T]) OnData(c Context, data T, op CrudOperation, filters ...*Filters) error
func (DefaultResponseHandler[T]) OnEmpty ¶ added in v0.0.2
func (h DefaultResponseHandler[T]) OnEmpty(c Context, op CrudOperation) error
func (DefaultResponseHandler[T]) OnError ¶ added in v0.0.2
func (h DefaultResponseHandler[T]) OnError(c Context, err error, op CrudOperation) error
func (DefaultResponseHandler[T]) OnList ¶ added in v0.0.2
func (h DefaultResponseHandler[T]) OnList(c Context, data []T, op CrudOperation, filters *Filters) error
type Filters ¶ added in v0.1.0
type Filters struct { Operation string `json:"operation,omitempty"` Limit int `json:"limit,omitempty"` Offset int `json:"offset,omitempty"` Count int `json:"count,omitempty"` Order []Order `json:"order,omitempty"` Fields []string `json:"fields,omitempty"` Include []string `json:"include,omitempty"` Relations []RelationInfo `json:"relations,omitempty"` }
func BuildQueryCriteria ¶ added in v0.1.0
func BuildQueryCriteria[T any](ctx Context, op CrudOperation) ([]repository.SelectCriteria, *Filters, error)
Index supports different query string parameters: GET /users?limit=10&offset=20 GET /users?order=name asc,created_at desc GET /users?select=id,name,email GET /users?name__ilike=John&age__gte=30 GET /users?name__and=John,Jack GET /users?name__or=John,Jack GET /users?include=Company,Profile GET /users?include=Profile.status=outdated TODO: Support /projects?include=Message&include=Company
type NotFoundError ¶ added in v0.0.2
type NotFoundError struct {
// contains filtered or unexported fields
}
type Option ¶
type Option[T any] func(*Controller[T])
func WithDeserializer ¶
func WithDeserializer[T any](d func(CrudOperation, Context) (T, error)) Option[T]
WithDeserializer sets a custom deserializer for the Controller.
func WithResponseHandler ¶ added in v0.0.2
func WithResponseHandler[T any](handler ResponseHandler[T]) Option[T]
type RelationFilter ¶ added in v0.1.0
type RelationInfo ¶ added in v0.1.0
type RelationInfo struct { Name string `json:"name"` Filters []RelationFilter `json:"filters,omitempty"` }
type ResourceController ¶ added in v0.1.0
ResourceController defines an interface for registering CRUD routes
type ResourceHandler ¶ added in v0.1.0
type ResponseHandler ¶ added in v0.0.2
type ResponseHandler[T any] interface { OnError(ctx Context, err error, op CrudOperation) error OnData(ctx Context, data T, op CrudOperation, filters ...*Filters) error OnEmpty(ctx Context, op CrudOperation) error OnList(ctx Context, data []T, op CrudOperation, filters *Filters) error }
ResponseHandler defines how controller responses are handled
func NewDefaultResponseHandler ¶ added in v0.0.2
func NewDefaultResponseHandler[T any]() ResponseHandler[T]
type Router ¶ added in v0.1.0
type Router interface { Get(path string, handler func(Context) error) RouterRouteInfo Post(path string, handler func(Context) error) RouterRouteInfo Put(path string, handler func(Context) error) RouterRouteInfo Delete(path string, handler func(Context) error) RouterRouteInfo }
Router is a simplified interface from the crud package perspective, referencing the generic router
func NewFiberAdapter ¶ added in v0.1.0
func NewFiberAdapter(r fiber.Router) Router
NewFiberAdapter creates a new crud.Router that uses a router.Router[any]
func NewGoRouterAdapter ¶ added in v0.1.1
NewGoRouterAdapter creates a new crud.Router that uses a router.Router[T] This follows the same pattern as the existing NewFiberAdapter
type RouterRouteInfo ¶ added in v0.1.0
type RouterRouteInfo interface {
Name(string) RouterRouteInfo
}
RouterRouteInfo is a simplified interface for route info
type ValidationError ¶ added in v0.0.2
type ValidationError struct {
// contains filtered or unexported fields
}