https://github.com/manuelarte/pagorminator
Pagination plugin for Gorm
https://github.com/manuelarte/pagorminator
go golang golang-library gorm gorm-orm gorm-plugin library pagination plugin
Last synced: 17 days ago
JSON representation
Pagination plugin for Gorm
- Host: GitHub
- URL: https://github.com/manuelarte/pagorminator
- Owner: manuelarte
- License: mit
- Created: 2024-11-09T21:36:31.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-12-24T13:34:21.000Z (over 1 year ago)
- Last Synced: 2024-12-24T14:41:56.123Z (over 1 year ago)
- Topics: go, golang, golang-library, gorm, gorm-orm, gorm-plugin, library, pagination, plugin
- Language: Go
- Homepage:
- Size: 33.2 KB
- Stars: 3
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# 📃 PaGorminator
[](https://github.com/manuelarte/pagorminator/actions/workflows/ci.yml)

[](https://goreportcard.com/report/github.com/manuelarte/pagorminator)
[](https://pkg.go.dev/github.com/manuelarte/pagorminator)
[](https://www.bestpractices.dev/projects/10813)
[](https://sonarcloud.io/summary/new_code?id=manuelarte_pagorminator)

Gorm plugin to add **Pagination** to your select queries

## ⬇️ How to install it
```bash
go get -u -v github.com/manuelarte/pagorminator
```
## 🎯 How to use it
### Basic Usage
```go
// 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:
```go
// Single sort criterion
pageRequest, err := pagorminator.NewPageRequest(0, 10, pagorminator.Desc("id"))
// Multiple sort criteria
pageRequest, err := pagorminator.NewPageRequest(
0,
10,
pagorminator.Asc("name"),
pagorminator.Desc("price")
)
```
### Unpaged Requests
If you want to retrieve all records without pagination:
```go
// Create an unpaged request
unpaged := pagorminator.UnPaged()
db.Clauses(unpaged).Find(&products)
```
#### Debug Mode
You can enable debug mode to see the SQL queries:
```go
// Enable debug mode
db.Use(pagorminator.PaGorminator{Debug: true})
```
## Examples
Check the examples in the [./examples](./examples) folder for more detailed usage patterns.