{"id":28546401,"url":"https://github.com/ulule/paging","last_synced_at":"2025-07-07T06:30:55.712Z","repository":{"id":51369831,"uuid":"44519848","full_name":"ulule/paging","owner":"ulule","description":"A small set of utilities to paginate your data in Go","archived":false,"fork":false,"pushed_at":"2021-05-10T06:24:32.000Z","size":37,"stargazers_count":140,"open_issues_count":0,"forks_count":21,"subscribers_count":10,"default_branch":"master","last_synced_at":"2025-06-09T23:43:58.037Z","etag":null,"topics":[],"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/ulule.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}},"created_at":"2015-10-19T08:06:03.000Z","updated_at":"2025-03-04T10:39:56.000Z","dependencies_parsed_at":"2022-08-25T15:01:40.442Z","dependency_job_id":null,"html_url":"https://github.com/ulule/paging","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/ulule/paging","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ulule%2Fpaging","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ulule%2Fpaging/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ulule%2Fpaging/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ulule%2Fpaging/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ulule","download_url":"https://codeload.github.com/ulule/paging/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ulule%2Fpaging/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":264027473,"owners_count":23546080,"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":[],"created_at":"2025-06-09T23:39:17.726Z","updated_at":"2025-07-07T06:30:55.706Z","avatar_url":"https://github.com/ulule.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# paging\n\n**A small set of utilities to paginate your data in Go.**\n\n## Installation\n\n```bash\n$ go get github.com/ulule/paging\n```\n\n## Usage\n\nFirst, import the package:\n\n```go\nimport \"github.com/ulule/paging\"\n```\n\nNow, you can access the package through `paging` namespace.\n\nThere are two types of paginators:\n- `OffsetPaginator`: uses a database offset. Returns the total of elements.\n- `CursorPaginator`: uses a database condition like `ID \u003e ?` or `creation_date \u003c ?`. Does not return the total number of items but increases performances.\n\nIt works in four steps:\n\n* Create a store (which is basically where your entities are stored)\n* Create paginator options (or use default ones)\n* Create an `OffsetPaginator` or a `CursorPaginator` instance with: your store, the HTTP request, and options\n* Call the `paginator.Page()` method to process the pagination\n* Call the `paginator.Previous()` method to get the previous paginator instance. (Previous page isn't available for cursor pagination system)\n* Call the `paginator.Next()` method to get the next paginator instance\n\nExample with OffsetPaginator and GORM:\n\n```go\n// Before anything... create a slice for your GORM models.\n// Let's assume we have 100 users in our database.\nusers := []User{}\n\n// Step 1: create the store. It takes your database connection pointer, a\n// pointer to models and the GORM \"ORDER BY\" string.\nstore, err := paging.NewGORMStore(\u0026db, \u0026users)\nif err != nil {\n        log.Fatal(err)\n}\n\n// Step 2: create options. Here, we use the default ones (see below).\noptions := paging.NewOptions()\n\n// Step 3: create a paginator instance and pass your store, your current HTTP\n// request and your options as arguments.\nrequest, _ := http.NewRequest(\"GET\", \"http://example.com?limit=20\u0026offset=0\", nil)\npaginator := paging.NewOffsetPaginator(store, request, options)\n\n// Step 4: call the paginator.Page() method to get the page instance.\nerr := paginator.Page()\nif err != nil {\n        log.Fatal(err)\n}\n\n// Your paginator instance contains everything you need.\nassert.True(int64(20), paginator.Limit)\nassert.True(int64(0), paginator.Offset)\nassert.True(int64(100), paginator.Count)\nassert.False(paginator.PreviousURI.Valid) // It's a null string because no previous page\nassert.True(paginator.NextURI.Valid)\nassert.Equal( \"?limit=20\u0026offset=20\", paginator.NextURI.String)\n\n// And our \"users\" slice is now populated with 20 users ordered by name.\nassert.Equal(20, len(users))\n\n// Now get the next page.\nnextPaginator, err := paginator.Next()\nif err != nil {\n        log.Fatal(err)\n}\n\n// Or the previous page.\npreviousPaginator, err := paginator.Previous()\nif err != nil {\n        log.Fatal(err)\n}\n```\n\nPaginator options are:\n\n* `DefaultLimit` (`int64`): the number of items per page (defaults to `20`)\n* `MaxLimit` (`int64`): the maximum limit that can be set (defaults to `20`)\n* `LimitKeyName` (`string`): the query string key name for limit (defaults to `limit`)\n* `OffsetKeyName` (`string`): the query string key name for offset (defaults to `offset`)\n* `CursorOptions.Mode` (`string`): set type of cursor, an `idCursor` or a `dateCursor` (time.Time) (defaults to `idCursor`)\n* `CursorOptions.KeyName` (`string`): the query string key name for the cursor (defaults to `since`)\n* `CursorOptions.DBName` (`string`): the cursor's database column name (defaults to `id`)\n* `CursorOptions.StructName` (`string`): the cursor struct field name (defaults to `ID`)\n* `CursorOptions.Reverse` (`bool`): if true, order is reversed (DESC) (defaults to `false`)\n\n## Contributing\n\n* Ping us on twitter [@thoas](https://twitter.com/thoas), [@oibafsellig](https://twitter.com/oibafsellig), [@NotDrana](https://twitter.com/notdrana)\n* Fork the [project](https://github.com/ulule/paging)\n* Fix [bugs](https://github.com/ulule/paging/issues)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fulule%2Fpaging","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fulule%2Fpaging","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fulule%2Fpaging/lists"}