paginateutil

package
v0.0.0-...-d761283 Latest Latest
Warning

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

Go to latest
Published: Sep 8, 2024 License: MIT Imports: 0 Imported by: 0

README

paginateutil

This package provides a utility for paginating data. It should be used in conjunction with database queries to paginate results.

Use these utilities to paginate results and, above all, to return a common structure in the different places where pagination is performed, maintaining consistency throughout the project.

Usage

1. Define your queries:
  • PaginateCount: This function should return the total number of records that match the query.
  • Paginate: This function should return the paginated records.
2. Create the offset from the request parameters:

Use the CreateOffsetFromParams function to create the offset from the request parameters needed to paginate the results.

3. Define the signature of your pagination function:

The signature of your wrapper function should be:

type PaginateXYZParams struct {
  Page      int
  Limit     int
  ABCFilter sql.NullString
}

func PaginateXYZ(
  ctx context.Context,
  params PaginateXYZParams,
) (
  paginateutil.PaginateResponse,
  []XYZ,
  error
)
Example

Refer to internal/service/backups/paginate_backups.go for an example of how to use the paginateutil package.

Notes

  • Default Values: Ensure to set reasonable default values for pagination parameters (page and limit) if they are not provided.
  • Common Response Structure: Use CreatePaginateResponse to generate a common response structure that includes information about pagination, such as the total number of items, the current page, and the number of items per page.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateOffsetFromParams

func CreateOffsetFromParams(paginateParams PaginateParams) int

CreateOffsetFromParams creates an offset from the given PaginateParams.

Types

type PaginateParams

type PaginateParams struct {
	Limit int `query:"limit" form:"limit" json:"limit"`
	Page  int `query:"page" form:"page" json:"page"`
}

PaginateParams are the parameters for a paginated request.

type PaginateResponse

type PaginateResponse struct {
	TotalItems      int  `json:"total_items"`
	TotalPages      int  `json:"total_pages"`
	ItemsPerPage    int  `json:"items_per_page"`
	PreviousPage    int  `json:"previous_page"`
	HasPreviousPage bool `json:"has_previous_page"`
	CurrentPage     int  `json:"current_page"`
	NextPage        int  `json:"next_page"`
	HasNextPage     bool `json:"has_next_page"`
}

PaginateResponse is the response for a paginated request.

func CreatePaginateResponse

func CreatePaginateResponse(
	paginateParams PaginateParams,
	totalItems int,
) PaginateResponse

CreatePaginateResponse creates a PaginateResponse from the given parameters.

Jump to

Keyboard shortcuts

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