Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/datek/gopaq
Limit-offset pagination for GORM queries.
https://github.com/datek/gopaq
gorm pagination query
Last synced: about 1 month ago
JSON representation
Limit-offset pagination for GORM queries.
- Host: GitHub
- URL: https://github.com/datek/gopaq
- Owner: DAtek
- License: mit
- Created: 2022-12-11T12:41:25.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2024-01-22T19:13:00.000Z (11 months ago)
- Last Synced: 2024-10-20T12:26:47.801Z (2 months ago)
- Topics: gorm, pagination, query
- Language: Go
- Homepage:
- Size: 12.7 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: changelog.md
- License: LICENSE
Awesome Lists containing this project
README
[![codecov](https://codecov.io/gh/DAtek/gopaq/graph/badge.svg?token=WWY2L6G56Y)](https://codecov.io/gh/DAtek/gopaq)
# GOPAQ - Gorm Paginated Query
Limit-offset pagination for [GORM](https://gorm.io/) queries.
## Example
```go
package mainimport (
"fmt"
"os""github.com/DAtek/gopaq"
"gorm.io/driver/postgres"
"gorm.io/gorm"
"gorm.io/gorm/schema"
)type Plant struct {
Id uint
Type string
Name string
}// You have to implement this function
func getSession() *gorm.DB {
return nil
}func main() {
session := getSession()for i := 0; i < 10; i++ {
session.Create(&Plant{})
}plant1 := &Plant{Name: "Banana"}
session.Create(plant1)plant2 := &Plant{Name: "Apple"}
session.Create(plant2)query := session.Model(&Plant{}).Order("name DESC")
result, _ := gopaq.FindWithPagination(query, []*Plant{}, 1, 2)fmt.Printf("plant1: %v\n", result.Items[0])
fmt.Printf("plant2: %v\n", result.Items[1])
fmt.Printf("returned items: %v\n", len(result.Items))
fmt.Printf("total: %v\n", result.Total)
}```
Output:
```
plant1: &{11 Banana}
plant2: &{12 Apple}
returned items: 2
total: 12
```