Documentation ¶
Index ¶
- Constants
- type FindParams
- type Handler
- func (h *Handler[NewModel, Model, Filters]) Create(w http.ResponseWriter, r *http.Request)
- func (h *Handler[NewModel, Model, Filters]) Delete(w http.ResponseWriter, r *http.Request)
- func (h *Handler[NewModel, Model, Filters]) Find(w http.ResponseWriter, r *http.Request)
- func (h *Handler[NewModel, Model, Filters]) FindByID(w http.ResponseWriter, r *http.Request)
- func (h *Handler[NewModel, Model, Filters]) Update(w http.ResponseWriter, r *http.Request)
- type IDGeneratorFunc
- type Identifiable
- type MemoryStore
- func (m *MemoryStore[NewModel, Model, Filters]) Create(ctx context.Context, newModel *NewModel) (*Model, error)
- func (m *MemoryStore[NewModel, Model, Filters]) Delete(ctx context.Context, id string) error
- func (m *MemoryStore[NewModel, Model, Filters]) Find(ctx context.Context, params *FindParams[Filters]) ([]*Model, int, error)
- func (m *MemoryStore[NewModel, Model, Filters]) FindByID(ctx context.Context, id string) (*Model, error)
- func (m *MemoryStore[NewModel, Model, Filters]) Update(ctx context.Context, model *Model) (*Model, error)
- type NewModelFunc
- type SortOrder
- type SqlStore
- func (s *SqlStore[NewModel, Model, Filter]) Create(ctx context.Context, newModel *NewModel) (*Model, error)
- func (s *SqlStore[NewModel, Model, Filter]) Delete(ctx context.Context, id string) error
- func (s *SqlStore[NewModel, Model, Filter]) Find(ctx context.Context, params *FindParams[Filter]) ([]*Model, int, error)
- func (s *SqlStore[NewModel, Model, Filter]) FindByID(ctx context.Context, id string) (*Model, error)
- func (s *SqlStore[NewModel, Model, Filter]) Update(ctx context.Context, model *Model) (*Model, error)
- type SqlTable
- type Storer
Constants ¶
View Source
const ( OrderAsc = SortOrder("ASC") OrderDesc = SortOrder("DESC") )
Possible values for SortOrder.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type FindParams ¶
type FindParams[Filters any] struct { Offset int64 `json:"offset,omitempty"` Limit int64 `json:"limit,omitempty"` Sort string `json:"sort,omitempty"` Order SortOrder `json:"order,omitempty"` Filters *Filters `json:"filters,omitempty"` }
FindParams defines parameters used by Find methods.
type Handler ¶
type Handler[NewModel any, Model Identifiable, Filters any] struct { // contains filtered or unexported fields }
func NewHandler ¶
func NewHandler[NewModel any, Model Identifiable, Filters any](storer Storer[NewModel, Model, Filters]) *Handler[NewModel, Model, Filters]
func (*Handler[NewModel, Model, Filters]) Create ¶
func (h *Handler[NewModel, Model, Filters]) Create(w http.ResponseWriter, r *http.Request)
func (*Handler[NewModel, Model, Filters]) Delete ¶
func (h *Handler[NewModel, Model, Filters]) Delete(w http.ResponseWriter, r *http.Request)
func (*Handler[NewModel, Model, Filters]) Find ¶
func (h *Handler[NewModel, Model, Filters]) Find(w http.ResponseWriter, r *http.Request)
type IDGeneratorFunc ¶
IDGeneratorFunc takes a new model and returns generates an identifier for it.
type Identifiable ¶
type Identifiable interface { // Identifier returns the unique identifier of the object. Identifier() string }
Identifiable is an object with an identifier.
type MemoryStore ¶
type MemoryStore[NewModel any, Model Identifiable, Filters any] struct { // contains filtered or unexported fields }
MemoryStore implements a generic model store in memory.
func NewMemoryStore ¶
func NewMemoryStore[NewModel any, Model Identifiable, Filters any](modelFromNewModel NewModelFunc[NewModel, Model], idGenerator IDGeneratorFunc[NewModel]) *MemoryStore[NewModel, Model, Filters]
NewMemoryStore returns a new memory store.
func (*MemoryStore[NewModel, Model, Filters]) Create ¶
func (m *MemoryStore[NewModel, Model, Filters]) Create(ctx context.Context, newModel *NewModel) (*Model, error)
Create adds a new model to the store.
func (*MemoryStore[NewModel, Model, Filters]) Delete ¶
func (m *MemoryStore[NewModel, Model, Filters]) Delete(ctx context.Context, id string) error
Delete deletes an existing model. It returns an error if the model did not exist before.
func (*MemoryStore[NewModel, Model, Filters]) Find ¶
func (m *MemoryStore[NewModel, Model, Filters]) Find(ctx context.Context, params *FindParams[Filters]) ([]*Model, int, error)
Find returns a list of models.
type NewModelFunc ¶
type NewModelFunc[NewModel any, Model Identifiable] func(newModel *NewModel, id string) *Model
NewModelFunc take a new model and an id and returns a model.
type SqlStore ¶
type SqlStore[NewModel any, Model Identifiable, Filter any] struct { // contains filtered or unexported fields }
func NewSqlStore ¶
func NewSqlStore[NewModel any, Model Identifiable, Filters any](db *sqlx.DB, table SqlTable[Filters], modelFromNewModel NewModelFunc[NewModel, Model], idGenerator IDGeneratorFunc[NewModel]) *SqlStore[NewModel, Model, Filters]
NewSqlStore returns a new generic sql store.
type Storer ¶
type Storer[NewModel any, Model Identifiable, Filters any] interface { Create(ctx context.Context, newModel *NewModel) (*Model, error) Update(ctx context.Context, model *Model) (*Model, error) Delete(ctx context.Context, id string) error FindByID(ctx context.Context, id string) (*Model, error) Find(ctx context.Context, params *FindParams[Filters]) ([]*Model, int, error) }
Storer defines a generic crud store.
Click to show internal directories.
Click to hide internal directories.