pagefilter

package module
v0.1.7 Latest Latest
Warning

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

Go to latest
Published: Feb 10, 2025 License: Apache-2.0 Imports: 10 Imported by: 5

README

PageFilter

This library contains utilities for writing a filterable, pageable and sortable list endpoint

Notes

Documentation for the basic technique used can be found here.

Documentation

Index

Constants

View Source
const (
	// Equal is the equal comparison operator
	Equal = "eq"

	// LessThan is the less than comparison operator
	LessThan = "lt"

	// GreaterThan is the greater than comparison operator
	GreaterThan = "gt"

	// Like is the partial match operator
	Like = "like"

	// DefaultSortBy is the default sort by key
	DefaultSortBy = "id"

	QueryLastVal = "last_val"
	QueryLastID  = "last_id"
	QuerySortBy  = "sort_by"
	QuerySortDir = "sort_dir"
	QueryLimit   = "limit"
)

Variables

View Source
var (
	ErrInvalidPaginatorDetails = errors.New("invalid paginator details")
)
View Source
var (
	// ErrNoDestination is returned when the destination is nil
	ErrNoDestination = errors.New("destination is nil")
)

Functions

This section is empty.

Types

type DB

type DB interface {
	Get(dest any, query string, args ...any) error
	Select(dest any, query string, args ...any) error
}

DB is the common interface for database operations

This should work with database/sql.DB and database/sql.Tx.

type Filter

type Filter interface {
	Joiner
	Wherer
}

type Grouper

type Grouper interface {
	Group() []string
}

Grouper represents something which can provide a group by to filter a sql query. Grouper support should only ever be used with a filter that adds the table/column refs that it needs to function, otherwise you will likely have a bad time.

type Joiner

type Joiner interface {
	Join() (string, []any)
}

Joiner represents something which can provide joins for an SQL query.

type MockDB

type MockDB struct {
	mock.Mock
}

MockDB is an autogenerated mock type for the DB type

func NewMockDB

func NewMockDB(t interface {
	mock.TestingT
	Cleanup(func())
}) *MockDB

NewMockDB creates a new instance of MockDB. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. The first argument is typically a *testing.T value.

func (*MockDB) Get

func (_m *MockDB) Get(dest interface{}, query string, args ...interface{}) error

Get provides a mock function with given fields: dest, query, args

func (*MockDB) Select

func (_m *MockDB) Select(dest interface{}, query string, args ...interface{}) error

Select provides a mock function with given fields: dest, query, args

type MockFilter

type MockFilter struct {
	mock.Mock
}

MockFilter is an autogenerated mock type for the Filter type

func NewMockFilter

func NewMockFilter(t interface {
	mock.TestingT
	Cleanup(func())
}) *MockFilter

NewMockFilter creates a new instance of MockFilter. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. The first argument is typically a *testing.T value.

func (*MockFilter) Join

func (_m *MockFilter) Join() (string, []interface{})

Join provides a mock function with no fields

func (*MockFilter) Where

func (_m *MockFilter) Where() (string, []interface{})

Where provides a mock function with no fields

type MockGrouper

type MockGrouper struct {
	mock.Mock
}

MockGrouper is an autogenerated mock type for the Grouper type

func NewMockGrouper

func NewMockGrouper(t interface {
	mock.TestingT
	Cleanup(func())
}) *MockGrouper

NewMockGrouper creates a new instance of MockGrouper. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. The first argument is typically a *testing.T value.

func (*MockGrouper) Group

func (_m *MockGrouper) Group() []string

Group provides a mock function with no fields

type MockJoiner

type MockJoiner struct {
	mock.Mock
}

MockJoiner is an autogenerated mock type for the Joiner type

func NewMockJoiner

func NewMockJoiner(t interface {
	mock.TestingT
	Cleanup(func())
}) *MockJoiner

NewMockJoiner creates a new instance of MockJoiner. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. The first argument is typically a *testing.T value.

func (*MockJoiner) Join

func (_m *MockJoiner) Join() (string, []interface{})

Join provides a mock function with no fields

type MockWhereTyper

type MockWhereTyper struct {
	mock.Mock
}

MockWhereTyper is an autogenerated mock type for the WhereTyper type

func NewMockWhereTyper

func NewMockWhereTyper(t interface {
	mock.TestingT
	Cleanup(func())
}) *MockWhereTyper

NewMockWhereTyper creates a new instance of MockWhereTyper. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. The first argument is typically a *testing.T value.

func (*MockWhereTyper) Where

func (_m *MockWhereTyper) Where() (string, []interface{})

Where provides a mock function with no fields

func (*MockWhereTyper) WhereType

func (_m *MockWhereTyper) WhereType() WhereType

WhereType provides a mock function with no fields

type MockWherer

type MockWherer struct {
	mock.Mock
}

MockWherer is an autogenerated mock type for the Wherer type

func NewMockWherer

func NewMockWherer(t interface {
	mock.TestingT
	Cleanup(func())
}) *MockWherer

NewMockWherer creates a new instance of MockWherer. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. The first argument is typically a *testing.T value.

func (*MockWherer) Where

func (_m *MockWherer) Where() (string, []interface{})

Where provides a mock function with no fields

type MultiFilter

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

func NewMultiFilter

func NewMultiFilter() *MultiFilter

func (*MultiFilter) Add

func (m *MultiFilter) Add(f any)

func (*MultiFilter) Group

func (m *MultiFilter) Group() []string

func (*MultiFilter) Join

func (m *MultiFilter) Join() (sqlStr string, args []any)

func (*MultiFilter) Where

func (m *MultiFilter) Where() (sqlStr string, args []any)

type PaginatedResponse

type PaginatedResponse[T comparable] struct {
	// Items is the list of items that are returned.
	Items []*T `json:"items"`

	// Total is the total number of items that have been found in the source.
	Total int64 `json:"total"`
}

PaginatedResponse is a response that contains a list of items and the total number of items. This is useful for when you want to return a paginated list of items.

type Paginator

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

Paginator is the struct that provides the paging.

func NewPaginator

func NewPaginator(db DB, table, idKey string, f Filter) *Paginator

NewPaginator creates a new paginator

func (*Paginator) Counts

func (p *Paginator) Counts(dest *int64) error

Counts returns the total number of records in the table given the provided filters. This does not take into account of the current pivot or limit.

func (*Paginator) First

func (p *Paginator) First() (string, error)

First is used when no details are provided which could give us the pivot point It will pick a start depending on the provided sort and filters.

func (*Paginator) ParseRequest

func (p *Paginator) ParseRequest(req *http.Request, sortColumns ...string) error

ParseRequest parses the request to handle retrieving all the pagination and sorting parameters

func (*Paginator) Pivot

func (p *Paginator) Pivot() (string, error)

Pivot finds the pivot point in the data.

func (*Paginator) Retrieve

func (p *Paginator) Retrieve(pivot string, dest any) error

Retrieve pulls the next page given the pivot point and requires a destination *[]struct to load the data into.

func (*Paginator) SetDetails

func (p *Paginator) SetDetails(paginatorDetails *PaginatorDetails, sortColumns ...string) error

SetDetails sets paginator details from the passed in arguments.

type PaginatorDetails

type PaginatorDetails struct {
	Limit   int
	LastVal string
	LastID  string
	SortBy  string
	SortDir string
	// contains filtered or unexported fields
}

PaginatorDetails contains pagination details

func DetailsFromRequest

func DetailsFromRequest(req *http.Request) (*PaginatorDetails, error)

DetailsFromRequest retrieves the paginator details from the request.

func GetPaginatorDetails

func GetPaginatorDetails(
	limit *common.LimitParam,
	lastVal *common.LastValue,
	lastID *common.LastId,
	sortBy *common.SortBy,
	sortDir *common.SortDirection,
) *PaginatorDetails

GetPaginatorDetails loads paginator details from a request. Requests have each pagination detail determined separately by codegen.

func (*PaginatorDetails) RemoveLimit

func (p *PaginatorDetails) RemoveLimit()

RemoveLimit removes the limit from the paginator details.

type WhereType

type WhereType string
const (
	WhereTypeAnd WhereType = "AND"
	WhereTypeOr  WhereType = "OR"
)

func (WhereType) IsValid

func (w WhereType) IsValid() bool

type WhereTyper

type WhereTyper interface {
	Wherer
	WhereType() WhereType
}

WhereTyper is an interface that can be used to specify the type of WHERE clause to use. By using this interface, you can specify whether to use an "AND" or "OR" WHERE clause.

type Wherer

type Wherer interface {
	Where() (string, []any)
}

Wherer is an interface that can be used to specify the WHERE clause to use. By using this interface, the package will default to using an "AND" WHERE clause. If you want to use an "OR" WHERE clause, you can use the WhereTyper interface instead.

Directories

Path Synopsis
Package common provides primitives to interact with the openapi HTTP API.
Package common provides primitives to interact with the openapi HTTP API.

Jump to

Keyboard shortcuts

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