{"id":22353726,"url":"https://github.com/rosberry/go-pagination","last_synced_at":"2025-03-26T12:27:51.947Z","repository":{"id":88763932,"uuid":"314473766","full_name":"rosberry/go-pagination","owner":"rosberry","description":"Simple library to convert cursor to query and back","archived":false,"fork":false,"pushed_at":"2022-02-21T12:26:25.000Z","size":296,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-31T13:43:02.023Z","etag":null,"topics":["golang","library"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rosberry.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-11-20T07:06:16.000Z","updated_at":"2023-10-10T12:04:22.000Z","dependencies_parsed_at":null,"dependency_job_id":"87327eca-c4d9-4cd1-ae41-740426cd8d34","html_url":"https://github.com/rosberry/go-pagination","commit_stats":null,"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rosberry%2Fgo-pagination","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rosberry%2Fgo-pagination/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rosberry%2Fgo-pagination/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rosberry%2Fgo-pagination/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rosberry","download_url":"https://codeload.github.com/rosberry/go-pagination/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245652706,"owners_count":20650542,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["golang","library"],"created_at":"2024-12-04T13:09:35.250Z","updated_at":"2025-03-26T12:27:51.927Z","avatar_url":"https://github.com/rosberry.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Pagination library\n\nPackage go-pagination was designed to paginate simple RESTful APIs with [GORM](https://github.com/go-gorm/gorm) and [Gin](https://github.com/gin-gonic/gin).\nIt uses cursor-based strategy, that avoids many of the pitfalls of \"offset–limit\" pagination.\n\n## Usage\n\n#### Model\n\n```go\npackage models\n\nimport \"github.com/rosberry/go-pagination\"\n\n\ntype User struct {\n\tID   uint\n\tName string\n\tRole uint `json:\"roleID\" cursor:\"roleID\"`\n}\n\nfunc GetUsersList(role uint, paginator *pagination.Paginator) []User {\n\tvar users []User\n\tq := db.DB.Model(\u0026User{}).Where(\"role = ?\", role)\n\n\terr := paginator.Find(q, \u0026users)\n\tif err != nil {\n\t\tlog.Println(err)\n\t\treturn nil\n\t}\n\n\treturn users\n}\n```\n\n#### Controllers\n\n```go\npackage controllers\n\nimport \"github.com/rosberry/go-pagination\"\n\ntype (\n\tusersListResponse struct {\n\t\tResult     bool\n\t\tUsers      []userData\n\t\tPagination *pagination.PageInfo\n\t}\n)\n\n\nfunc UsersList(c *gin.Context) {\n\tpaginator, err := pagination.New(pagination.Options{\n\t\tGinContext: c,\n\t\tDB: models.GetDB(),\n\t\tModel: \u0026models.User{},\n\t\tLimit: 5,\n\t\tDefaultCursor: nil,\n\t})\n\tif err != nil {\n\t\tlog.Println(err)\n\t}\n\n\tusers := models.GetUsersList(0, paginator)\n\n\tc.JSON(200, usersListResponse{\n\t\tResult:     true,\n\t\tUsers:      usersListToData(users),\n\t\tPagination: paginator.PageInfo,\n\t})\n}\n```\n\n### Customize request\n\nIf you want to get values in a special way, you can customize the functions to find the values you need.\nYou must implement functions `RequestGetter` type\n\n```go\ntype RequestGetter  func(c *gin.Context) (query string)\n```\n\nfor example\n\n```go\nfunc cursorGetter(c *gin.Context) (query string) {\n\tcursorQuery := c.Request.Header.Get(\"customCursorFromHeader\")\n\treturn cursorQuery\n}\n\nfunc sortingGetter(c *gin.Context) (query string) {\n\tsortingQuery := c.Query(\"sort\")\n\treturn sortingQuery\n}\n```\n\nand pass the functions as `Options.CustomRequest` (type `RequestOptions`) in `pagination.New()` function.\n\n```go\npaginator, err := New(Options{\n\t\tGinContext: c,\n\t\tLimit:      uint(limit),\n\t\tDB:         db,\n\t\tModel:      \u0026Material{},\n\t\tCustomRequest: \u0026RequestOptions{\n\t\t\tCursor: func(c *gin.Context) (query string) {\n\t\t\t\tcursorQuery := c.Request.Header.Get(\"customCursorFromHeader\")\n\t\t\t\treturn cursorQuery\n\t\t\t},\n\t\t\tSorting: func(c *gin.Context) (query string) {\n\t\t\t\tsortingQuery := c.Query(\"sort\")\n\t\t\t\treturn sortingQuery\n\t\t\t},\n\t\t},\n\t})\n```\n\n- `query` for `cursor`/`after`/`before` - base64 string\n- `query` for `sorting` - json string\n\n## Client-Server interaction\n\nRequest:\n\n```\nGET /items\n```\n\n```\nGET /items?sorting=%5B%7B%22field%22:%22id%22,%22direction%22:%22desc%22%7D%5D\n```\n\nSorting query parameters is JSON:\n\n```\n[\n    {\n        \"field\": \"Name\",\n        \"direction\": \"asc\"\n    },\n    {\n        \"field\": \"UpdatedAt\",\n        \"direction\": \"desc\"\n    }\n]\n```\n\nfield name in query parameters should have name from response. If name in response is different from name in model - you need add tag cursor:\"fieldName\" in model\n\nResponse:\n\n```\n{\n    \"result\": true,\n    \"data\": {\n    },\n    \"pagination\": {\n\t\t\"hasPrev\": false,\n\t\t\"hasNext\": true,\n\t\t\"prev\": \"ew2YWxU0Cn01ZSI6ogICJID=\",\n\t\t\"next\": \"ewogICJ2YWx1ZSI6IDU0Cn0=\",\n\t\t\"totalRows\": 10,\n\t\t\"rangeTruncated\": true\n    }\n}\n```\n\nRequest next page:\n\n```\nGET /items?cursor=ew2YWxU0Cn01ZSI6ogICJID\n```\n\nResponse (end):\n\n```\n{\n    \"result\": true,\n    \"data\": null,\n    \"pagination:\" null\n}\n```\n\n### Before / After\n\nYou can use `after`/`before` params instead of `cursor` in request\n\n```\nGET /items?after=e2sdw2wO0WDwwW\u0026before=sdqqwDsdDq2Pd1\n```\n\nThe `after` parameter is typically sent by the client to get the next page, while `before` is used to get the prior page.\n\nClients MAY use the `after` and `before` parameters together on the same request. These are called “range pagination requests”, as the client is asking for all the results starting from immediately after the `after` cursor and continuing up until the `before` cursor.\n\nFor range pagination requests, the server uses a `limit` to determine the maximum page size. In other words, the page size used will depend on the value of the `limit` parameter or the maximum page size.\n\nIf the number of results that satisfy both the `after` and `before` constraints exceeds the used page size, the server responds with the same paginated data that it would have if the `before` parameter had not been provided. However, in this case the server MUST also add `\"rangeTruncated\": true` to the pagination metadata to indicate to the client that the paginated data does not contain all the results it requested.\n\n### Field naming\n\n\u003cimg src=\"docs/diag/pagination_naming.png\" /\u003e\n\n## About\n\n\u003cimg src=\"https://github.com/rosberry/Foundation/blob/master/Assets/full_logo.png?raw=true\" height=\"100\" /\u003e\n\nThis project is owned and maintained by [Rosberry](http://rosberry.com). We build mobile apps for users worldwide 🌏.\n\nCheck out our [open source projects](https://github.com/rosberry), read [our blog](https://medium.com/@Rosberry) or give us a high-five on 🐦 [@rosberryapps](http://twitter.com/RosberryApps).\n\n## License\n\nThis project is available under the MIT license. See the LICENSE file for more info.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frosberry%2Fgo-pagination","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frosberry%2Fgo-pagination","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frosberry%2Fgo-pagination/lists"}