Skip to content

manuelarte/pagorminator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

91 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ“ƒ PaGorminator

Go coverage Go Report Card Go Reference OpenSSF Best Practices version

Gorm plugin to add Pagination to your select queries

logo

⬇️ How to install it

go get -u -v github.com/manuelarte/pagorminator

🎯 How to use it

Basic Usage

// Initialize GORM with PaGorminator plugin
db, err := gorm.Open(sqlite.Open("file:mem?mode=memory&cache=shared"), &gorm.Config{})
if err != nil {
    panic("failed to connect database")
}
db.Use(pagorminator.PaGorminator{})

// Create a page request (page 0, size 10)
pageRequest, err := pagorminator.NewPageRequest(0, 10)
if err != nil {
    // Handle error
}

// Apply pagination to your query
var products []*Product
db.Clauses(pageRequest).Find(&products)

// Access pagination information
fmt.Printf("Total elements: %d\n", pageRequest.GetTotalElements())
fmt.Printf("Total pages: %d\n", pageRequest.GetTotalPages())

Pagination Parameters

The pagination struct contains the following data:

  • page: page number, e.g. 0 (zero-based indexing)
  • size: page size, e.g. 10
  • sort: to apply sorting, e.g. id desc

The plugin will automatically calculate the total amount of elements. The pagination instance provides GetTotalElements() and GetTotalPages() methods to retrieve the total counts. The pagination starts at index 0, e.g., if the total pages is 6, then the pagination index goes from 0 to 5.

πŸš€ Features

Sorting

You can add sorting to your pagination request:

// Single sort criterion
pageRequest, err := pagorminator.NewPageRequest(0, 10, 
    pagorminator.MustOrder("id", pagorminator.DESC))

// Multiple sort criteria
pageRequest, err := pagorminator.NewPageRequest(0, 10, 
    pagorminator.MustOrder("name", pagorminator.ASC),
    pagorminator.MustOrder("id", pagorminator.DESC))

Unpaged Requests

If you want to retrieve all records without pagination:

// Create an unpaged request
unpaged := pagorminator.UnPaged()
db.Clauses(unpaged).Find(&products)

Debug Mode

You can enable debug mode to see the SQL queries:

// Enable debug mode
db.Use(pagorminator.PaGorminator{Debug: true})

Examples

Check the examples in the ./examples folder for more detailed usage patterns.

About

πŸ“ƒ Pagination using clause for Gorm

Topics

Resources

License

Contributing

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •