{"id":36813323,"url":"https://github.com/dentech-floss/pagination","last_synced_at":"2026-01-12T13:51:16.948Z","repository":{"id":57696973,"uuid":"493976623","full_name":"dentech-floss/pagination","owner":"dentech-floss","description":"Provides generic support for pagination towards a datasource","archived":false,"fork":false,"pushed_at":"2024-06-07T11:44:08.000Z","size":14,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2024-06-20T06:31:30.993Z","etag":null,"topics":["golang","gorm","grpc","orm","pagination"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dentech-floss.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":"2022-05-19T08:01:50.000Z","updated_at":"2024-06-07T07:38:13.000Z","dependencies_parsed_at":"2024-06-07T08:50:51.305Z","dependency_job_id":"51b9d34d-f3f0-4b17-bfbb-60c0adca27e6","html_url":"https://github.com/dentech-floss/pagination","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"purl":"pkg:github/dentech-floss/pagination","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dentech-floss%2Fpagination","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dentech-floss%2Fpagination/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dentech-floss%2Fpagination/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dentech-floss%2Fpagination/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dentech-floss","download_url":"https://codeload.github.com/dentech-floss/pagination/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dentech-floss%2Fpagination/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28339608,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-12T12:22:26.515Z","status":"ssl_error","status_checked_at":"2026-01-12T12:22:10.856Z","response_time":98,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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","gorm","grpc","orm","pagination"],"created_at":"2026-01-12T13:51:16.173Z","updated_at":"2026-01-12T13:51:16.923Z","avatar_url":"https://github.com/dentech-floss.png","language":"Go","readme":"# pagination\n\nProvides generic support for pagination towards a datasource, only SQL is supported out of the box but it is easy to provide additional impl since it's based on this simple interface:\n\n```go\ntype Page interface {\n    Token() string\n    Size() int\n    Offset() int\n    NextToken(resultSize int) *string\n}\n```\n\nThis is based on the [list pagination design pattern](https://cloud.google.com/apis/design/design_patterns#list_pagination) suggested by Google when working with protocol buffers as well as [gorm scopes](https://gorm.io/docs/scopes.html#Pagination).\n\nDo also check out the [dentech-floss/orm](https://github.com/dentech-floss/orm) lib which goes hand in hand with this lib.\n\n## Install\n\n```\ngo get github.com/dentech-floss/pagination@v0.1.0\n```\n\n## Usage\n\nSo based on the mentioned design pattern above, we have designed our gRPC api like this to provide support for pagination:\n\n```proto\nmessage FindAppointmentsRequest {\n  repeated string clinic_ids = 1 [json_name = \"clinic_id\"];\n\n  google.protobuf.StringValue page_token = 11 [json_name = \"page_token\"];\n  google.protobuf.Int32Value page_size = 12 [json_name = \"page_size\"];\n}\n```\n\n```proto\nmessage FindAppointmentsResponse {\n  repeated AppointmentDTO appointments = 1;\n\n  string page_token = 11 [json_name = \"page_token\"];\n  int32 page_size = 12 [json_name = \"page_size\"];\n  google.protobuf.StringValue next_page_token = 13 [json_name = \"next_page_token\"];\n}\n```\n\nThen in the gRPC server we create a SQL page that we pass to the repository tier:\n\n```go\npackage example\n\nimport (\n    \"github.com/dentech-floss/pagination/pkg/pagination\"\n\n    patient_gateway_service_v1 \"go.buf.build/dentechse/go-grpc-gateway-openapiv2/dentechse/patient-api-gateway/api/patient/v1\"\n)\n\nconst (\n\tDEFAULT_FIND_APPOINTMENTS_PAGE_SIZE = 100\n\tMAX_FIND_APPOINTMENTS_PAGE_SIZE     = 1000\n)\n\nfunc (s *PatientGatewayServiceV1) FindAppointments(\n\tctx context.Context,\n\trequest *patient_gateway_service_v1.FindAppointmentsRequest,\n) (*patient_gateway_service_v1.FindAppointmentsResponse, error) {\n\n    var pageToken *string = nil\n    if request.PageToken != nil {\n        pageToken = \u0026request.PageToken.Value\n    }\n\n    var pageSize *int = nil\n    if request.PageSize != nil {\n        tmp := int(request.PageSize.Value)\n        pageSize = \u0026tmp\n    }\n\n    page, err := pagination.NewSqlPage(\n        pageToken,\n        pageSize,\n        DEFAULT_FIND_APPOINTMENTS_PAGE_SIZE,\n        MAX_FIND_APPOINTMENTS_PAGE_SIZE,\n    )\n    if err != nil {\n        // handle the error\n    }\n\n    appointments, err := s.repo.FindAppointmentsForClinics(ctx, clinicIds, page)\n    if err != nil {\n        // handle the error\n    }\n\n    return \u0026patient_gateway_service_v1.FindAppointmentsResponse{\n        Appointments: s.appointmentsToDTOs(appointments),\n        PageToken:     page.Token(),\n        PageSize:      int32(page.Size()),\n        NextPageToken: util.StringToWrapper(page.NextToken(len(appointments))),\n    }\n}\n```\n\nThe repository tier along with a GORM/SQL implementation looks something like this:\n\n```go\npackage example\n\nimport (\n    \"github.com/dentech-floss/pagination/pkg/pagination\"\n)\n\ntype Repository interface {\n    FindAppointmentsForClinics(ctx context.Context, clinicIds []int32, page pagination.Page) ([]*model.Appointment, error)\n}\n```\n\n```go\npackage example\n\nimport (\n    \"github.com/dentech-floss/pagination/pkg/pagination\"\n\n    \"gorm.io/gorm\"\n    \"gorm.io/gorm/clause\"\n)\n\nfunc (r *sqlRepository) FindAppointmentsForClinics(\n    ctx context.Context,\n    clinicIds []int32,\n    page pagination.Page,\n) ([]*model.Appointment, error) {\n\n    appointments := make([]*model.Appointment, 0)\n    if err := r.db.\n        WithContext(ctx). // to propagate the active span for tracing\n        Where(\"clinic_id IN ?\", clinicIds).\n        Order(\"start_time asc\").\n        Scopes(paginationScope(page)).\n        Find(\u0026appointments).Error; err != nil {\n        return nil, err\n    }\n    return appointments, nil\n}\n\nfunc paginationScope(page pagination.Page) func(db *gorm.DB) *gorm.DB {\n    return func(db *gorm.DB) *gorm.DB {\n        return db.Offset(page.Offset()).Limit(page.Size())\n    }\n}\n```","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdentech-floss%2Fpagination","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdentech-floss%2Fpagination","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdentech-floss%2Fpagination/lists"}