Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add support for chain columns, Add PageStart to override default starting page instead of 0 #18

Merged
merged 1 commit into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ version: 2
jobs:
build:
docker:
- image: golang:1.15-buster
working_directory: /go/src/github.com/toguser/paginate
- image: golang:1.16-buster
working_directory: /go/src/github.com/morkid/paginate
steps:
- checkout
- run: go test -v
90 changes: 52 additions & 38 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,9 @@ var req *http.Request = ...
// or
// var req *fasthttp.Request

model := db.Where("id > ?", 1).Model(&Article{})
stmt := db.Where("id > ?", 1).Model(&Article{})
pg := paginate.New()
page := pg.Response(model, req, &[]Article{})
// or
page := pg.With(model).Request(req).Response(&[]Article{})
page := pg.With(stmt).Request(req).Response(&[]Article{})

log.Println(page.Total)
log.Println(page.Items)
Expand All @@ -72,10 +70,6 @@ pg := paginate.New(&paginate.Config{
```
see more about [customize default configuration](#customize-default-configuration).

> Note that `Response` was marked as a deprecated function. Please use `With` instead.
> Old: `pg.Response(model, req, &[]Article{})`,
> New: `pg.With(model).Request(req).Response(&[]Article{})`

## Pagination Result

```js
Expand Down Expand Up @@ -227,9 +221,9 @@ func main() {
pg := paginate.New()

http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
model := db.Joins("User").Model(&Article{})
paginated := pg.Response(model, r, &[]Article{})
j, _ := json.Marshal(paginated)
stmt := db.Joins("User").Model(&Article{})
page := pg.With(stmt).Request(r).Response(&[]Article{})
j, _ := json.Marshal(page)
w.Header().Set("Content-type", "application/json")
w.Write(j)
})
Expand All @@ -253,9 +247,9 @@ func main() {
pg := paginate.New()

fasthttp.ListenAndServe(":3000", func(ctx *fasthttp.RequestCtx) {
model := db.Joins("User").Model(&Article{})
paginated := pg.Response(model, &ctx.Request, &[]Article{})
j, _ := json.Marshal(paginated)
stmt := db.Joins("User").Model(&Article{})
page := pg.With(stmt).Request(&ctx.Request).Response(&[]Article{})
j, _ := json.Marshal(page)
ctx.SetContentType("application/json")
ctx.SetBody(j)
})
Expand All @@ -276,9 +270,9 @@ func main() {
pg := paginate.New()
app := mux.NewRouter()
app.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
model := db.Joins("User").Model(&Article{})
paginated := pg.Response(model, req, &[]Article{})
j, _ := json.Marshal(paginated)
stmt := db.Joins("User").Model(&Article{})
page := pg.With(stmt).Request(req).Response(&[]Article{})
j, _ := json.Marshal(page)
w.Header().Set("Content-type", "application/json")
w.Write(j)
}).Methods("GET")
Expand All @@ -302,8 +296,9 @@ func main() {
pg := paginate.New()
app := fiber.New()
app.Get("/", func(c *fiber.Ctx) error {
model := db.Joins("User").Model(&Article{})
return c.JSON(pg.Response(model, c.Request(), &[]Article{}))
stmt := db.Joins("User").Model(&Article{})
page := pg.With(stmt).Request(c.Request()).Response(&[]Article{})
return c.JSON(page)
})

app.Listen(":3000")
Expand All @@ -325,8 +320,9 @@ func main() {
pg := paginate.New()
app := echo.New()
app.GET("/", func(c echo.Context) error {
model := db.Joins("User").Model(&Article{})
return c.JSON(200, pg.Response(model, c.Request(), &[]Article{}))
stmt := db.Joins("User").Model(&Article{})
page := pg.With(stmt).Request(c.Request()).Response(&[]Article{})
return c.JSON(200, page)
})

app.Logger.Fatal(app.Start(":3000"))
Expand All @@ -348,8 +344,9 @@ func main() {
pg := paginate.New()
app := gin.Default()
app.GET("/", func(c *gin.Context) {
model := db.Joins("User").Model(&Article{})
c.JSON(200, pg.Response(model, c.Request, &[]Article{}))
stmt := db.Joins("User").Model(&Article{})
page := pg.With(stmt).Request(c.Request).Response(&[]Article{})
c.JSON(200, page)
})
app.Run(":3000")
}
Expand All @@ -372,8 +369,9 @@ func main() {
app := martini.Classic()
app.Use(render.Renderer())
app.Get("/", func(req *http.Request, r render.Render) {
model := db.Joins("User").Model(&Article{})
r.JSON(200, pg.Response(model, req, &[]Article{}))
stmt := db.Joins("User").Model(&Article{})
page := pg.With(stmt).Request(req).Response(&[]Article{})
r.JSON(200, page)
})
app.Run()
}
Expand All @@ -392,9 +390,9 @@ func main() {
// var db *gorm.DB
pg := paginate.New()
web.Get("/", func(c *context.Context) {
model := db.Joins("User").Model(&Article{})
c.Output.JSON(
pg.Response(model, c.Request, &[]Article{}), false, false)
stmt := db.Joins("User").Model(&Article{})
page := pg.With(stmt).Request(c.Request).Response(&[]Article{})
c.Output.JSON(page, false, false)
})
web.Run(":3000")
}
Expand Down Expand Up @@ -529,6 +527,7 @@ Single array member is known as **Logical Operator**.
// WHERE age = 20 OR age = 25
```


You are allowed to send array inside a value.
```js
["age", "between", [20, 30] ]
Expand All @@ -540,6 +539,19 @@ You are allowed to send array inside a value.
// WHERE age NOT IN(20, 21, 22, 23, 24, 25, 26, 26)
```

Define chain columns with same value separated by comma.
```js
// Example 1
["price,discount", ">", 10]
// Produces:
// WHERE price > 10 OR discount > 25

// Example 2
["deleted_at,expiration_date", null]
// Produces:
// WHERE deleted_at IS NULL OR expiration_date IS NULL
```

You can filter nested condition with deep array.
```js
[
Expand Down Expand Up @@ -592,7 +604,9 @@ Config | Type | Default | Description
Operator | `string` | `OR` | Default conditional operator if no operator specified.<br>For example<br>`GET /user?filters=[["name","like","jo"],["age",">",20]]`,<br>produces<br>`SELECT * FROM user where name like '%jo' OR age > 20`
FieldWrapper | `string` | `LOWER(%s)` | FieldWrapper for `LIKE` operator *(for postgres default is: `LOWER((%s)::text)`)*
DefaultSize | `int64` | `10` | Default size or limit per page
SmartSearch | `bool` | `false` | Enable smart search *(Experimental feature)*
PageStart | `int64` | `0` | Set start page, default `0` if not set. `total_pages` , `max_page` and `page` variable will be affected if you set `PageStart` greater than `0`
LikeAsIlikeDisabled | `bool` | `false` | By default, paginate using Case Insensitive on `LIKE` operator. Instead of using `ILIKE`, you can use `LIKE` operator to find what you want. You can set `LikeAsIlikeDisabled` to `true` if you need this feature to be disabled.
SmartSearchEnabled | `bool` | `false` | Enable smart search *(Experimental feature)*
CustomParamEnabled | `bool` | `false` | Enable custom request parameter
FieldSelectorEnabled | `bool` | `false` | Enable partial response with specific fields. Comma separated per field. eg: `?fields=title,user.name`
SortParams | `[]string` | `[]string{"sort"}` | if `CustomParamEnabled` is `true`,<br>you can set the `SortParams` with custom parameter names.<br>For example: `[]string{"sorting", "ordering", "other_alternative_param"}`.<br>The following requests will capture same result<br>`?sorting=-name`<br>or `?ordering=-name`<br>or `?other_alternative_param=-name`<br>or `?sort=-name`
Expand Down Expand Up @@ -620,15 +634,15 @@ override := func(article *Article) {
}

var articles []Article
model := db.Joins("User").Model(&Article{})
stmt := db.Joins("User").Model(&Article{})

pg := paginate.New()
result := pg.Response(model, httpRequest, &articles)
page := pg.With(stmt).Request(httpRequest).Response(&articles)
for index := range articles {
override(&articles[index])
}

log.Println(result.Items)
log.Println(page.Items)

```

Expand Down Expand Up @@ -656,9 +670,9 @@ type UserNullable {
```go
// usage
nameAndIDOnly := []string{"name","id"}
model := db.Model(&User{})
stmt := db.Model(&User{})

page := pg.With(model).
page := pg.With(stmt).
Request(req).
Fields(nameAndIDOnly).
Response([]&UserNullable{})
Expand Down Expand Up @@ -706,7 +720,7 @@ func main() {
CacheAdapter: gocache.NewInMemoryCache(adapterConfig),
})

page := pg.With(model).
page := pg.With(stmt).
Request(req).
Cache("article"). // set cache name
Response(&[]Article{})
Expand All @@ -731,7 +745,7 @@ func main() {
CacheAdapter: gocache.NewDiskCache(adapterConfig),
})

page := pg.With(model).
page := pg.With(stmt).
Request(req).
Cache("article"). // set cache name
Response(&[]Article{})
Expand Down Expand Up @@ -763,7 +777,7 @@ func main() {
CacheAdapter: cache.NewRedisCache(adapterConfig),
})

page := pg.With(model).
page := pg.With(stmt).
Request(req).
Cache("article").
Response(&[]Article{})
Expand Down Expand Up @@ -801,7 +815,7 @@ func main() {
CacheAdapter: cache.NewElasticCache(adapterConfig),
})

page := pg.With(model).
page := pg.With(stmt).
Request(req).
Cache("article").
Response(&[]Article{})
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/morkid/paginate

go 1.15
go 1.16

require (
github.com/iancoleman/strcase v0.1.3
Expand Down
Loading
Loading