-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c6448e6
commit f54f619
Showing
14 changed files
with
673 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# This workflow will build a golang project | ||
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go | ||
|
||
name: Go | ||
|
||
on: | ||
push: | ||
branches: [ "master", "dev" ] | ||
pull_request: | ||
branches: [ "master" ] | ||
|
||
jobs: | ||
|
||
build: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v4 | ||
|
||
- name: Set up Go | ||
uses: actions/setup-go@v4 | ||
with: | ||
go-version: '1.20' | ||
|
||
- name: Build | ||
run: go build -v ./... | ||
|
||
- name: Test | ||
run: go test ./... -coverprofile=./cover.out -covermode=atomic -coverpkg=./... | ||
|
||
- name: check test coverage | ||
uses: vladopajic/go-test-coverage@v2 | ||
with: | ||
config: ./.testcoverage.yml | ||
## when token is not specified (value '') this feature is turned off | ||
## in this example badge is created and committed only for main branch | ||
git-token: ${{ github.ref_name == 'main' && secrets.GITHUB_TOKEN || '' }} | ||
## name of branch where badges are stored | ||
## ideally this should be orphan branch (see below how to create this branch) | ||
git-branch: badges |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# (mandatory) | ||
# Path to coverprofile file (output of `go test -coverprofile` command). | ||
# | ||
# For cases where there are many coverage profiles, such as when running | ||
# unit tests and integration tests separately, you can combine all those | ||
# profiles into one. In this case, the profile should have a comma-separated list | ||
# of profile files, e.g., 'cover_unit.out,cover_integration.out'. | ||
profile: cover.out | ||
|
||
local-prefix: "github.com/manuelarte/pagorminator" | ||
|
||
# Holds coverage thresholds percentages, values should be in range [0-100] | ||
threshold: | ||
# (optional; default 0) | ||
# The minimum coverage that each file should have | ||
file: 50 | ||
|
||
# (optional; default 0) | ||
# The minimum coverage that each package should have | ||
package: 50 | ||
|
||
# (optional; default 0) | ||
# The minimum total coverage project should have | ||
total: 50 | ||
|
||
# Holds regexp rules which will exclude matched files or packages | ||
# from coverage statistics | ||
exclude: | ||
# Exclude files or packages matching their paths | ||
paths: | ||
- ^internal/model.go$ | ||
- ^pkg/bar # exclude package `pkg/bar` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Changelog | ||
|
||
All notable changes to this project will be documented in this file. | ||
|
||
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), | ||
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). | ||
|
||
## [Unreleased] | ||
|
||
### TODO | ||
|
||
- Add code coverage badge | ||
- Check for fmt | ||
- Add more examples | ||
|
||
### Added | ||
|
||
- Added pagorminator plugin for gorm | ||
- Added examples |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,30 @@ | ||
# pagorminator | ||
Pagination plugin for Gorm | ||
[![Go](https://github.com/manuelarte/pagorminator/actions/workflows/go.yml/badge.svg)](https://github.com/manuelarte/pagorminator/actions/workflows/go.yml) | ||
# 📃 pagorminator | ||
|
||
Gorm plugin to add pagination to your select queries | ||
|
||
## 😍 How to install it | ||
|
||
> go get github.com/manuelarte/pagorminator | ||
## 🎯 How to use it | ||
|
||
```go | ||
var products []*Products | ||
// give me the first 10 products | ||
db.Scopes(pagorminator.WithPagination(pagorminator.PageRequestOf(0, 10))).Find(&products) | ||
``` | ||
|
||
## 🎓 Examples | ||
|
||
- [Simple](./examples/simple/main.go) | ||
|
||
Simple query with no filters (where clause) | ||
|
||
- Filtered | ||
|
||
Using where to filter | ||
|
||
## Contact | ||
|
||
- 📧 [email protected] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
module simple | ||
|
||
go 1.18 | ||
|
||
require ( | ||
gorm.io/driver/sqlite v1.5.6 | ||
gorm.io/gorm v1.25.12 | ||
) | ||
|
||
require ( | ||
github.com/jinzhu/inflection v1.0.0 // indirect | ||
github.com/jinzhu/now v1.1.5 // indirect | ||
github.com/manuelarte/pagorminator v0.0.0-20241109213332-29069dcc8d67 // indirect | ||
github.com/mattn/go-sqlite3 v1.14.24 // indirect | ||
golang.org/x/text v0.20.0 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= | ||
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= | ||
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ= | ||
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= | ||
github.com/manuelarte/pagorminator v0.0.0-20241109213332-29069dcc8d67 h1:HhZkJJJ8/xalo8HK4ENMG+7YsfvliSv8ALti4Sa2Y9s= | ||
github.com/manuelarte/pagorminator v0.0.0-20241109213332-29069dcc8d67/go.mod h1:e7ZYAl1XwI3uc0rOXmfF4FToPSS+C65DM4sPXwRNkKs= | ||
github.com/mattn/go-sqlite3 v1.14.24 h1:tpSp2G2KyMnnQu99ngJ47EIkWVmliIizyZBfPrBWDRM= | ||
github.com/mattn/go-sqlite3 v1.14.24/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y= | ||
golang.org/x/text v0.19.0 h1:kTxAhCbGbxhK0IwgSKiMO5awPoDQ0RpfiVYBfK860YM= | ||
golang.org/x/text v0.19.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY= | ||
golang.org/x/text v0.20.0 h1:gK/Kv2otX8gz+wn7Rmb3vT96ZwuoxnQlY+HlJVj7Qug= | ||
golang.org/x/text v0.20.0/go.mod h1:D4IsuqiFMhST5bX19pQ9ikHC2GsaKyk/oF+pn3ducp4= | ||
gorm.io/driver/sqlite v1.5.6 h1:fO/X46qn5NUEEOZtnjJRWRzZMe8nqJiQ9E+0hi+hKQE= | ||
gorm.io/driver/sqlite v1.5.6/go.mod h1:U+J8craQU6Fzkcvu8oLeAQmi50TkwPEhHDEjQZXDah4= | ||
gorm.io/gorm v1.25.12 h1:I0u8i2hWQItBq1WfE0o2+WuL9+8L21K9e2HHSTE/0f8= | ||
gorm.io/gorm v1.25.12/go.mod h1:xh7N7RHfYlNc5EmcI/El95gXusucDrQnHXe0+CgWcLQ= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"github.com/manuelarte/pagorminator" | ||
"gorm.io/driver/sqlite" | ||
"gorm.io/gorm" | ||
) | ||
|
||
type Product struct { | ||
gorm.Model | ||
Code string | ||
Price uint | ||
} | ||
|
||
func (p Product) String() string { | ||
return fmt.Sprintf("Product{Code: %s, Price: %d}", p.Code, p.Price) | ||
} | ||
|
||
func main() { | ||
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.PaGormMinator{}) | ||
// Migrate the schema | ||
db.AutoMigrate(&Product{}) | ||
|
||
// Create | ||
db.Create(&Product{Code: "D42", Price: 100}) | ||
|
||
// Read | ||
var products []*Product | ||
pageRequest, _ := pagorminator.PageRequestOf(0, 1) | ||
db.Scopes(pagorminator.WithPagination(pageRequest)).First(&products) | ||
for _, product := range products { | ||
fmt.Printf("PageRequest: {Page: %d, Size: %d, TotalElements: %d, TotalPages: %d\n", | ||
pageRequest.GetPage(), pageRequest.GetSize(), pageRequest.GetTotalElements(), pageRequest.GetTotalPages()) | ||
println(product.String()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
module github.com/manuelarte/pagorminator | ||
|
||
go 1.18 | ||
|
||
require ( | ||
gorm.io/driver/sqlite v1.5.6 | ||
gorm.io/gorm v1.25.12 | ||
) | ||
|
||
require ( | ||
github.com/jinzhu/inflection v1.0.0 // indirect | ||
github.com/jinzhu/now v1.1.5 // indirect | ||
github.com/mattn/go-sqlite3 v1.14.22 // indirect | ||
golang.org/x/text v0.19.0 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= | ||
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= | ||
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ= | ||
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= | ||
github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU= | ||
github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y= | ||
golang.org/x/text v0.19.0 h1:kTxAhCbGbxhK0IwgSKiMO5awPoDQ0RpfiVYBfK860YM= | ||
golang.org/x/text v0.19.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY= | ||
gorm.io/driver/sqlite v1.5.6 h1:fO/X46qn5NUEEOZtnjJRWRzZMe8nqJiQ9E+0hi+hKQE= | ||
gorm.io/driver/sqlite v1.5.6/go.mod h1:U+J8craQU6Fzkcvu8oLeAQmi50TkwPEhHDEjQZXDah4= | ||
gorm.io/gorm v1.25.12 h1:I0u8i2hWQItBq1WfE0o2+WuL9+8L21K9e2HHSTE/0f8= | ||
gorm.io/gorm v1.25.12/go.mod h1:xh7N7RHfYlNc5EmcI/El95gXusucDrQnHXe0+CgWcLQ= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package internal | ||
|
||
type PageRequestImpl struct { | ||
Page int | ||
Size int | ||
TotalPages int | ||
TotalElements int | ||
} | ||
|
||
func (p PageRequestImpl) GetOffset() int { | ||
return (p.Page - 1) * p.Size | ||
} | ||
|
||
func (p PageRequestImpl) GetPage() int { | ||
return p.Page | ||
} | ||
|
||
func (p PageRequestImpl) GetSize() int { | ||
return p.Size | ||
} | ||
|
||
func (p PageRequestImpl) GetTotalPages() int { | ||
return p.TotalPages | ||
} | ||
|
||
func (p PageRequestImpl) GetTotalElements() int { | ||
return p.TotalElements | ||
} | ||
|
||
func (p PageRequestImpl) IsUnPaged() bool { | ||
return p.Size == 0 && p.Page == 0 | ||
} |
Oops, something went wrong.