tables

package
v0.0.7 Latest Latest
Warning

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

Go to latest
Published: May 26, 2024 License: CC0-1.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AbstractResource

type AbstractResource struct {
	DB              *gorm.DB
	Request         *http.Request
	Fields          []*Field
	Filters         []*Filter
	Searches        []*Search
	Preloads        []*Preload
	TableRequest    *TableRequest
	HasGlobalSearch bool
	DefaultPerPage  int
}

func (*AbstractResource) ApplySearch

func (r *AbstractResource) ApplySearch(db *gorm.DB, field, value string)

ApplySearch applies searchc criteria to query

func (*AbstractResource) FlagVisibility

func (r *AbstractResource) FlagVisibility()

FlagVisibility applies visibility flag to field the attributes

func (*AbstractResource) Paginate

func (r *AbstractResource) Paginate(resource ITable, model any) (map[string]interface{}, error)

Paginate this is the main function for our resource It applies filters and search criteria and paginates Pagination uses a "Length aware" approach

func (*AbstractResource) ToResponse

func (r *AbstractResource) ToResponse(paged *Pagination) map[string]interface{}

type ActionItems added in v0.0.5

type ActionItems struct {
	Label  string   `json:"label"`
	Link   string   `json:"link"`
	Params []string `json:"params"`
}

type Client added in v0.0.5

type Client struct {
	ID          uint   `gorm:"primaryKey" json:"id"`
	Title       string `json:"title"`
	Description string `json:"description"`
}

func (Client) TableName added in v0.0.5

func (Client) TableName() string

type Field

type Field struct {
	Component  string                 `json:"component"`
	Attribute  string                 `json:"attribute"`
	Name       string                 `json:"name"`
	Sortable   bool                   `json:"sortable"`
	Searchable bool                   `json:"searchable"`
	Visibility bool                   `json:"visibility"`
	Visible    bool                   `json:"visible"`
	Actions    []*ActionItems         `json:"actions,omitempty"`
	Meta       map[string]interface{} `json:"meta,omitempty"`
}

A Field represents a table field

func NewActionField added in v0.0.5

func NewActionField(name string, actions []*ActionItems) *Field

func NewField

func NewField(name string, opts ...FieldOption) *Field

NewField creates a new table field

func (*Field) SetVisibility

func (f *Field) SetVisibility(flag bool)

SetVisibility sets the fields visibility flag to show or hide a column

type FieldOption

type FieldOption func(*Field)

func WithAttribute

func WithAttribute(name string) FieldOption

WithAttribute is a Field option to set the database field

func WithFieldComponent added in v0.0.6

func WithFieldComponent(name string) FieldOption

WithFieldComponent allows you to override the default component type (text)

func WithMeta added in v0.0.5

func WithMeta(data map[string]interface{}) FieldOption

WithMeta add extra meta information for special field types

func WithSearchable

func WithSearchable() FieldOption

WithSearchable is a Field option to allow the field to be searched

func WithSortable

func WithSortable() FieldOption

WithSortable is a Field option to allow the field to be sorted

func WithVisibility

func WithVisibility() FieldOption

WithVisibility is a Field option to allow the field's visibility to be toggled

type Filter

type Filter struct {
	Component string          `json:"component"`
	Label     string          `json:"label"`
	Field     string          `json:"field"`
	Options   []FilterOptions `json:"options"`
	Value     string          `json:"value"`
}

Filter defines filters for adding query clauses to our query

func NewFilter

func NewFilter(name string, opts ...FilterOpt) *Filter

NewFilter creates a new filter

func (*Filter) ApplyQuery

func (f *Filter) ApplyQuery(db *gorm.DB)

ApplyQuery adds search criteria to the database query

type FilterOpt

type FilterOpt func(*Filter)

FilterOpt is an optional function type to set filter attributes

func WithComponent

func WithComponent(name string) FilterOpt

WithComponent allows you to override the default component type (text)

func WithField

func WithField(name string) FilterOpt

WithField allows you to override the default generated field name

func WithOptions

func WithOptions(options ...FilterOptions) FilterOpt

WithOptions allows you to set options for required filter types (select)

type FilterOptions

type FilterOptions struct {
	Label string `json:"label"`
	Value any    `json:"value"`
}

FilterOptions defines filter options

type ITable

type ITable interface {
	GetFields() []*Field
	GetFilters() []*Filter
	WithGlobalSearch(db *gorm.DB, val string)
	ApplyFilter(db *gorm.DB)
}

type Pagination

type Pagination struct {
	Limit      int         `json:"limit,omitempty"`
	Page       int         `json:"page,omitempty"`
	Sort       string      `json:"sort,omitempty"`
	TotalRows  int64       `json:"total_rows"`
	TotalPages int         `json:"total_pages"`
	Rows       interface{} `json:"rows"`
}

func (*Pagination) GetLimit

func (p *Pagination) GetLimit() int

func (*Pagination) GetOffset

func (p *Pagination) GetOffset() int

func (*Pagination) GetPage

func (p *Pagination) GetPage() int

func (*Pagination) GetSort

func (p *Pagination) GetSort() string

type Preload added in v0.0.5

type Preload struct {
	Name  string
	Extra func(*gorm.DB) *gorm.DB
}
type Search struct {
	Label   string `json:"label"`
	Field   string `json:"field"`
	Value   string `json:"value"`
	Enabled bool   `json:"enabled"`
}

func (*Search) ApplySearch

func (f *Search) ApplySearch(db *gorm.DB)

type TableRequest

type TableRequest struct {
	Page         int               `json:"page"`
	PerPage      int               `json:"perPage"`
	Sort         string            `json:"sort"`
	Search       map[string]string `json:"search"`
	Filters      map[string]string `json:"filters"`
	GlobalFilter Filter            `json:"global_filter"`
}

func (*TableRequest) Fill

func (r *TableRequest) Fill(req *url.URL)

func (*TableRequest) SetFilterAndSearch

func (r *TableRequest) SetFilterAndSearch(query *url.URL)

SetFilterAndSearch Parses filter and search queries and builds key value maps

type UserPrivate added in v0.0.5

type UserPrivate struct {
	ID        uint      `gorm:"primaryKey" json:"id"`
	ClientId  int       `json:"client_id"`
	Client    *Client   `gorm:"foreignkey:ClientId" json:"client,omitempty"`
	FirstName string    `json:"first_name"`
	LastName  string    `json:"last_name"`
	Username  string    `json:"username"`
	Email     string    `json:"email"`
	CreatedAt time.Time `json:"created_at"`
	UpdatedAt time.Time `json:"updated_at"`
}

func (UserPrivate) TableName added in v0.0.5

func (UserPrivate) TableName() string

type UserResource

type UserResource struct {
	AbstractResource
}

func NewUserResource

func NewUserResource(db *gorm.DB, req *http.Request) *UserResource

func (*UserResource) ApplyFilter

func (u *UserResource) ApplyFilter(db *gorm.DB)

func (*UserResource) GetFields

func (u *UserResource) GetFields() []*Field

func (*UserResource) GetFilters

func (u *UserResource) GetFilters() []*Filter

func (*UserResource) WithGlobalSearch

func (u *UserResource) WithGlobalSearch(db *gorm.DB, val string)

Jump to

Keyboard shortcuts

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