diff --git a/lib/rasti/db/query.rb b/lib/rasti/db/query.rb index f6956e9..d681d35 100644 --- a/lib/rasti/db/query.rb +++ b/lib/rasti/db/query.rb @@ -74,6 +74,14 @@ def each(&block) all.each(&block) end + def each_page(size:, &block) + dataset.each_page(size) do |page| + page.each do |row| + block.call collection_class.model.new(with_graph(row)) + end + end + end + def graph(*relations) build_query relations_graph: relations_graph.merge(relations: relations) end diff --git a/spec/minitest_helper.rb b/spec/minitest_helper.rb index c40dc54..44944c6 100644 --- a/spec/minitest_helper.rb +++ b/spec/minitest_helper.rb @@ -132,6 +132,8 @@ class Minitest::Spec let :db do Sequel.connect(driver).tap do |db| + db.extension :pagination + db.create_table :users do primary_key :id String :name, null: false, unique: true diff --git a/spec/query_spec.rb b/spec/query_spec.rb index 59a1734..7fa9055 100644 --- a/spec/query_spec.rb +++ b/spec/query_spec.rb @@ -174,6 +174,17 @@ users_query.detect(id: 3).must_equal User.new(id: 3, name: 'User 3') end + it 'Each page' do + user_pages = [] + users_query.each_page(size: 2) do | page | + user_pages << page + end + user_pages.size.must_equal 10 + user_pages.each_with_index do | user_page, i | + user_page.must_equal User.new(id: i+1, name: "User #{i+1}") + end + end + it 'Where' do users_query.where(id: 3).all.must_equal [User.new(id: 3, name: 'User 3')] end