diff --git a/CHANGELOG.md b/CHANGELOG.md index c99fdb1..6199698 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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` diff --git a/lib/bridgetown-prismic/api.rb b/lib/bridgetown-prismic/api.rb index 0c221eb..48d368f 100644 --- a/lib/bridgetown-prismic/api.rb +++ b/lib/bridgetown-prismic/api.rb @@ -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) diff --git a/lib/bridgetown-prismic/version.rb b/lib/bridgetown-prismic/version.rb index 2facaaf..5248685 100644 --- a/lib/bridgetown-prismic/version.rb +++ b/lib/bridgetown-prismic/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module BridgetownPrismic - VERSION = "0.1.2" + VERSION = "0.2.0" end diff --git a/test/test_bridgetown_prismic.rb b/test/test_bridgetown_prismic.rb index 74f44a0..55b728d 100644 --- a/test/test_bridgetown_prismic.rb +++ b/test/test_bridgetown_prismic.rb @@ -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