-
Notifications
You must be signed in to change notification settings - Fork 0
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
0 parents
commit a1ad026
Showing
29 changed files
with
1,409 additions
and
0 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,21 @@ | ||
# If you prefer the allow list template instead of the deny list, see community template: | ||
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore | ||
# | ||
# Binaries for programs and plugins | ||
*.exe | ||
*.exe~ | ||
*.dll | ||
*.so | ||
*.dylib | ||
|
||
# Test binary, built with `go test -c` | ||
*.test | ||
|
||
# Output of the go coverage tool, specifically when used with LiteIDE | ||
*.out | ||
|
||
# Dependency directories (remove the comment below to include it) | ||
# vendor/ | ||
|
||
# Go workspace file | ||
go.work |
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,105 @@ | ||
# pagi: Pagination Helper Package | ||
|
||
pagi is a Go package designed to simplify pagination in your applications. It provides utility functions and types to handle pagination parameters, sorting, filtering, and generating pagination links. | ||
|
||
## Installation | ||
|
||
To install pagi, use `go get`: | ||
|
||
```bash | ||
go get -u github.com/billowdev/pagi | ||
``` | ||
|
||
## Usage | ||
|
||
### 1. Import the Package | ||
|
||
```go | ||
import "github.com/billowdev/pagi" | ||
``` | ||
|
||
### 2. Pagination Parameters | ||
|
||
The `PagingParams` type represents pagination parameters including limits, page numbers, sorting, and filtering. | ||
|
||
### 3. Sorting | ||
|
||
The `SortParams` type defines parameters for sorting, including the field to sort by, sorting order, and default sorting order. | ||
|
||
### 4. Filtering | ||
|
||
The `CommonTimeFilters` type contains common time-based filters such as date ranges and timestamps. | ||
|
||
### 5. Utility Functions | ||
|
||
#### AddWhereClauseIfNotEmpty | ||
|
||
```go | ||
func AddWhereClauseIfNotEmpty(query *gorm.DB, columnName string, filterValue string, filterType string) *gorm.DB | ||
``` | ||
|
||
Adds a WHERE clause to the query if the filter value is not empty, based on the provided column name, filter value, and filter type. | ||
|
||
#### ApplyFilter | ||
|
||
```go | ||
func ApplyFilter(query *gorm.DB, column string, value interface{}, filterType string) *gorm.DB | ||
``` | ||
|
||
Applies filtering to the query based on the provided column, value, and filter type. | ||
|
||
#### ApplyCommaFilter | ||
|
||
```go | ||
func ApplyCommaFilter(query *gorm.DB, columnName, filterValue string) *gorm.DB | ||
``` | ||
|
||
Applies filtering to the query for comma-separated values in the specified column. | ||
|
||
#### ApplyCommaFilterWithJoin | ||
|
||
```go | ||
func ApplyCommaFilterWithJoin(query *gorm.DB, joinTable, joinCondition, columnName, filterValue string) *gorm.DB | ||
``` | ||
|
||
Applies filtering to the query with a JOIN condition for comma-separated values. | ||
|
||
#### ApplyDatetimeFilters | ||
|
||
```go | ||
func ApplyDatetimeFilters(query *gorm.DB, filter CommonTimeFilters) *gorm.DB | ||
``` | ||
|
||
Applies datetime filtering to the query based on the provided CommonTimeFilters. | ||
|
||
#### ApplyDatetimePreloadFilters | ||
|
||
```go | ||
func ApplyDatetimePreloadFilters(query *gorm.DB, filter CommonTimeFilters, preloadKey string) *gorm.DB | ||
``` | ||
|
||
Applies datetime filtering with preloading to the query based on the provided CommonTimeFilters. | ||
|
||
## Examples | ||
|
||
### Basic Pagination | ||
|
||
```go | ||
params := pagi.PagingParams{} | ||
params.Limit = 10 | ||
params.Page = 1 | ||
params.Sort = "id" | ||
params.Order = "asc" | ||
params.BaseURL = "https://example.com" | ||
``` | ||
|
||
|
||
## License | ||
|
||
pagi is licensed under the MIT License. See the [LICENSE](https://github.com/billowdev/pagi/blob/main/LICENSE) file for details. | ||
|
||
|
||
|
||
## Contributing | ||
|
||
- Contributions are welcome! Feel free to open issues or submit pull requests on the [GitHub repository.](https://github.com/billowdev/pagi) |
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,49 @@ | ||
package pagi | ||
|
||
// PagingParams represents pagination parameters including limits, sorting, total rows, and filters. | ||
type PagingParams[FilterType interface{}] struct { | ||
Limit int `json:"limit"` | ||
Page int `json:"page"` | ||
Sort string `json:"sort"` | ||
Order string `json:"order"` | ||
TotalRows int64 `json:"total_rows"` | ||
TotalPages int `json:"total_pages"` | ||
BaseURL string `json:"base_url"` | ||
Filters FilterType `json:"filters"` | ||
} | ||
|
||
// SortParams contains parameters for sorting. | ||
type SortParams struct { | ||
Sort string // Field to sort by | ||
Order string // Sorting order ("ASC" or "DESC") | ||
DefaultOrderBy string // Default | ||
} | ||
|
||
// PagingLinks contains links for next and previous pages. | ||
type PagingLinks struct { | ||
Next string `json:"next"` | ||
Previous string `json:"previous"` | ||
} | ||
|
||
// PagingInfo provides information about pagination, including links, total count, page number, page size, total pages, and rows. | ||
type PagingInfo[T interface{}] struct { | ||
Links PagingLinks `json:"links"` | ||
Total int64 `json:"total"` | ||
Page int `json:"page"` | ||
PageSize int `json:"page_size"` | ||
TotalPages int `json:"total_pages"` | ||
Rows T `json:"rows,omitempty"` | ||
} | ||
|
||
// CommonTimeFilters contains common time-based filters such as date ranges and timestamps. | ||
type CommonTimeFilters struct { | ||
DateField string `json:"date_field"` | ||
CreatedAfter string `json:"created_after"` | ||
UpdatedAfter string `json:"updated_after"` | ||
CreatedBefore string `json:"created_before"` | ||
UpdatedBefore string `json:"updated_before"` | ||
CreatedAt string `json:"created_at"` | ||
UpdatedAt string `json:"updated_at"` | ||
StartDate string `json:"start_date"` | ||
EndDate string `json:"end_date"` | ||
} |
Oops, something went wrong.