Skip to content
This repository has been archived by the owner on Sep 27, 2023. It is now read-only.

Commit

Permalink
Automatically paginate through results sets
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredcwhite committed Nov 22, 2021
1 parent e60db22 commit 0ecfd59
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

...

## 0.2.0

- Automatically paginate through results sets

## 0.1.2

- Add hash option to `provide_data`
Expand Down
25 changes: 21 additions & 4 deletions lib/bridgetown-prismic/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,27 @@ def configure_prismic # rubocop:disable Metrics/AbcSize
def query_prismic(custom_type, options = {})
Bridgetown.logger.info "Prismic API:", "Loading #{custom_type.to_s.green}..."

BridgetownPrismic
.api
.query(Prismic::Predicates.at("document.type", custom_type.to_s), options)
.results
results = []
page = 1
finalpage = false
options["pageSize"] ||= 100 # pull in as much data as possible for a single request

until finalpage
options["page"] = page

response = BridgetownPrismic
.api
.query(Prismic::Predicates.at("document.type", custom_type.to_s), options)

results += response.results
if response.total_pages > page
page += 1
else
finalpage = true
end
end

results
end

def query_prismic_and_generate_resources_for(klass)
Expand Down
2 changes: 1 addition & 1 deletion lib/bridgetown-prismic/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module BridgetownPrismic
VERSION = "0.1.2"
VERSION = "0.2.0"
end
8 changes: 8 additions & 0 deletions test/test_bridgetown_prismic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,12 @@ def setup
assert_equal "This is a test page", @resource.data.title
end
end

context "pagination queries" do
should "pull in all data" do
builder = BridgetownPrismic::Builder.new
builder.configure_prismic
assert_equal 2, builder.query_prismic(:blog_post, { "pageSize" => 1 }).length
end
end
end

0 comments on commit 0ecfd59

Please sign in to comment.