From 843d2137fa22c78b653ff43a5895e10b5b2da00b Mon Sep 17 00:00:00 2001 From: WalkerSumida Date: Wed, 23 Jan 2019 22:08:35 +0900 Subject: [PATCH 01/10] feat: add accessor(last_evaluated_key) --- lib/dynamodb/api/base.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/dynamodb/api/base.rb b/lib/dynamodb/api/base.rb index 202ae5a..7a9ba92 100644 --- a/lib/dynamodb/api/base.rb +++ b/lib/dynamodb/api/base.rb @@ -6,6 +6,7 @@ module Dynamodb module Api class Base # :nodoc: include Relation + attr_accessor :last_evaluated_key def all raise NotImplementedError, "You must implement #{self.class}##{__method__}" From 17a79425222d79800116fdb1abceedb984e081ec Mon Sep 17 00:00:00 2001 From: WalkerSumida Date: Sat, 2 Feb 2019 20:49:24 +0900 Subject: [PATCH 02/10] refact: move before --- spec/dynamodb/api/scan_spec.rb | 36 +++++++++++++++++----------------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/spec/dynamodb/api/scan_spec.rb b/spec/dynamodb/api/scan_spec.rb index 1c0b0a6..633c5d1 100644 --- a/spec/dynamodb/api/scan_spec.rb +++ b/spec/dynamodb/api/scan_spec.rb @@ -1,23 +1,23 @@ RSpec.describe Dynamodb::Api::Scan do - describe '#all' do - before do - items = [ - { - id: '1', maker_id: 1, maker: 'Honda', model: 'Accord', release_date: 19760508, status: 0, - }, - { - id: '2', maker_id: 2, maker: 'Toyota', model: 'CROWN', release_date: 19550101, status: 0, - }, - { - id: '3', maker_id: 3, maker: 'Tesla', model: 'Model S', release_date: 20120601, status: 0, - }, - { - id: '4', maker_id: 1, maker: 'Honda', model: 'S2000', release_date: 19980101, status: 1, - }, - ] - DynamodbHelper.new.create_dummy_data(items) - end + before do + items = [ + { + id: '1', maker_id: 1, maker: 'Honda', model: 'Accord', release_date: 19760508, status: 0, + }, + { + id: '2', maker_id: 2, maker: 'Toyota', model: 'CROWN', release_date: 19550101, status: 0, + }, + { + id: '3', maker_id: 3, maker: 'Tesla', model: 'Model S', release_date: 20120601, status: 0, + }, + { + id: '4', maker_id: 1, maker: 'Honda', model: 'S2000', release_date: 19980101, status: 1, + }, + ] + DynamodbHelper.new.create_dummy_data(items) + end + describe '#all' do context 'select clause' do it 'works' do scan = Dynamodb::Api.scan From 71f4d4ba494bd54fb9a18291b8ca5d5d88bf6dd1 Mon Sep 17 00:00:00 2001 From: WalkerSumida Date: Sun, 3 Feb 2019 16:21:56 +0900 Subject: [PATCH 03/10] test: add #next spec --- spec/dynamodb/api/scan_spec.rb | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/spec/dynamodb/api/scan_spec.rb b/spec/dynamodb/api/scan_spec.rb index 633c5d1..f8f1edd 100644 --- a/spec/dynamodb/api/scan_spec.rb +++ b/spec/dynamodb/api/scan_spec.rb @@ -68,4 +68,28 @@ end end end + + describe '#next' do + context 'exists last_evaluated_key' do + it 'returns next items' do + scan = Dynamodb::Api.scan + scan.from('cars'). + limit(2) + result = scan.all + expect(result.items.map { |i| i['id'] }).to eq(%w(1 4)) + result = scan.next + expect(result.items.map { |i| i['id'] }).to eq(%w(3 2)) + end + end + + context 'not exists last_evaluated_key' do + it 'returns nil' do + scan = Dynamodb::Api.scan + scan.from('cars') + _result = scan.all + result = scan.next + expect(result).to be nil + end + end + end end From 29b307e86e857a88d39672e27512869d96f07745 Mon Sep 17 00:00:00 2001 From: WalkerSumida Date: Mon, 11 Feb 2019 15:58:43 +0900 Subject: [PATCH 04/10] feat: add #next to Scan --- lib/dynamodb/api/scan.rb | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/dynamodb/api/scan.rb b/lib/dynamodb/api/scan.rb index 76414b0..8cac323 100644 --- a/lib/dynamodb/api/scan.rb +++ b/lib/dynamodb/api/scan.rb @@ -6,7 +6,18 @@ module Dynamodb module Api class Scan < Base # :nodoc: def all - Adapter.client.scan(build_query) + result = Adapter.client.scan(build_query) + @last_evaluated_key = result.last_evaluated_key + result + end + + def next + return nil if @last_evaluated_key.blank? + result = Adapter.client.scan( + build_query.merge(exclusive_start_key: @last_evaluated_key) + ) + @last_evaluated_key = result.last_evaluated_key + result end private From abb08a9668bddacc35b26cf8455cae81a887e6d8 Mon Sep 17 00:00:00 2001 From: WalkerSumida Date: Mon, 11 Feb 2019 16:07:30 +0900 Subject: [PATCH 05/10] refact: before section --- spec/dynamodb/api/query_spec.rb | 36 ++++++++++++++++----------------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/spec/dynamodb/api/query_spec.rb b/spec/dynamodb/api/query_spec.rb index ba00512..a7f1f58 100644 --- a/spec/dynamodb/api/query_spec.rb +++ b/spec/dynamodb/api/query_spec.rb @@ -1,23 +1,23 @@ RSpec.describe Dynamodb::Api::Query do - describe '#all' do - before do - items = [ - { - id: '1', maker_id: 1, maker: 'Honda', model: 'Accord', release_date: 19760508, status: 0, - }, - { - id: '2', maker_id: 2, maker: 'Toyota', model: 'CROWN', release_date: 19550101, status: 0, - }, - { - id: '3', maker_id: 3, maker: 'Tesla', model: 'Model S', release_date: 20120601, status: 0, - }, - { - id: '4', maker_id: 1, maker: 'Honda', model: 'S2000', release_date: 19980101, status: 1, - }, - ] - DynamodbHelper.new.create_dummy_data(items) - end + before do + items = [ + { + id: '1', maker_id: 1, maker: 'Honda', model: 'Accord', release_date: 19760508, status: 0, + }, + { + id: '2', maker_id: 2, maker: 'Toyota', model: 'CROWN', release_date: 19550101, status: 0, + }, + { + id: '3', maker_id: 3, maker: 'Tesla', model: 'Model S', release_date: 20120601, status: 0, + }, + { + id: '4', maker_id: 1, maker: 'Honda', model: 'S2000', release_date: 19980101, status: 1, + }, + ] + DynamodbHelper.new.create_dummy_data(items) + end + describe '#all' do context 'where clause' do it 'works(only hash key)' do query = Dynamodb::Api.query From 0a7c82a93d2328884cf29869eafe8e31f3987ef5 Mon Sep 17 00:00:00 2001 From: WalkerSumida Date: Sun, 24 Feb 2019 15:12:05 +0900 Subject: [PATCH 06/10] test: add Query#next method spec --- spec/dynamodb/api/query_spec.rb | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/spec/dynamodb/api/query_spec.rb b/spec/dynamodb/api/query_spec.rb index a7f1f58..9f13863 100644 --- a/spec/dynamodb/api/query_spec.rb +++ b/spec/dynamodb/api/query_spec.rb @@ -69,4 +69,31 @@ end end end + + describe '#next' do + context 'exists last_evaluated_key' do + it 'returns next items' do + query = Dynamodb::Api.query + query.from('cars').index('index_maker_id_release_date'). + where(['maker_id', '=', 1]). + limit(1) + result = query.all + expect(result.items.map { |i| i['id'] }).to eq(%w(4)) + result = query.next + expect(result.items.map { |i| i['id'] }).to eq(%w(1)) + end + end + + context 'not exists last_evaluated_key' do + it 'returns nil' do + query = Dynamodb::Api.query + query.from('cars').index('index_maker_id_release_date'). + where(['maker_id', '=', 1]). + limit(2) + _result = query.all + result = query.next + expect(result).to be nil + end + end + end end From 0012fcd27dbe981307b2507a1054594ea7d5ec7d Mon Sep 17 00:00:00 2001 From: WalkerSumida Date: Sun, 24 Feb 2019 15:42:37 +0900 Subject: [PATCH 07/10] feat: add Query#next --- lib/dynamodb/api/query.rb | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/dynamodb/api/query.rb b/lib/dynamodb/api/query.rb index 67ee1ad..10b43b0 100644 --- a/lib/dynamodb/api/query.rb +++ b/lib/dynamodb/api/query.rb @@ -6,7 +6,19 @@ module Dynamodb module Api class Query < Base # :nodoc: def all - Adapter.client.query(build_query) + result = Adapter.client.query(build_query) + @last_evaluated_key = result.last_evaluated_key + result + end + + def next + return nil if @last_evaluated_key.blank? + result = Adapter.client.query( + build_query.merge(exclusive_start_key: @last_evaluated_key) + ) + @last_evaluated_key = result.last_evaluated_key + return nil if result.count.zero? + result end private From 902fe2a068c5ceebee94ca851aefaf82083fbc4d Mon Sep 17 00:00:00 2001 From: WalkerSumida Date: Sun, 24 Feb 2019 16:02:25 +0900 Subject: [PATCH 08/10] docs: add #next docs --- README.md | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/README.md b/README.md index ba1addc..b02b1c6 100644 --- a/README.md +++ b/README.md @@ -74,6 +74,22 @@ items = scan.all.items |3 |3 |Model S |0.20120601e8 |0 | |4 |1 |S2000 |0.19980101e8 |1 | + +#### Next(Paging) + +```ruby +scan = Dynamodb::Api.scan +scan.from('cars'). + limit(1) +_items = scan.all.items +items = scan.next.items +``` + +| id | maker_id(Partition key) | model | release_date(Sort key) | status | +|:---|:---|:---|:---|:---| +|2 |2 |CROWN |0.19550101e8 |0 | + + ### Query https://docs.aws.amazon.com/sdkforruby/api/Aws/DynamoDB/Client.html#query-instance_method @@ -148,6 +164,22 @@ items = query.all.items |:---|:---|:---|:---|:---| |1 |1 |Accord |0.19760508e8 |0 | +#### Next(Paging) + +```ruby +query = Dynamodb::Api.query +query.from('cars').index('index_maker_id_release_date'). + where(['maker_id', '=', 1]). + order('asc'). # default: 'desc' + limit(1) +_items = query.all.items +items = query.next.items +``` + +| id | maker_id(Partition key) | model | release_date(Sort key) | status | +|:---|:---|:---|:---|:---| +|4 |1 |S2000 |0.19980101e8 |1 | + #### Expression Attribute Names Words reserved in DynamoDB can not be used without special processing. From e0c6a0935dd5700f84165ed8f7fb37f64308b2e1 Mon Sep 17 00:00:00 2001 From: WalkerSumida Date: Sun, 24 Feb 2019 18:09:22 +0900 Subject: [PATCH 09/10] docs: add changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3f4f92f..d3e6de2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] +### Added +- [#39](https://github.com/walkersumida/dynamodb-api/pull/39) `next` method ## [0.7.0] - 2018-12-23 ### Added From 75ab2082de63b8ce92692c2cecfd38f345cae4e2 Mon Sep 17 00:00:00 2001 From: WalkerSumida Date: Sun, 24 Feb 2019 18:10:40 +0900 Subject: [PATCH 10/10] docs: add [Unreleased] labebl --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index b02b1c6..582dd52 100644 --- a/README.md +++ b/README.md @@ -75,7 +75,7 @@ items = scan.all.items |4 |1 |S2000 |0.19980101e8 |1 | -#### Next(Paging) +#### Next(Paging) [Unreleased] ```ruby scan = Dynamodb::Api.scan @@ -164,7 +164,7 @@ items = query.all.items |:---|:---|:---|:---|:---| |1 |1 |Accord |0.19760508e8 |0 | -#### Next(Paging) +#### Next(Paging) [Unreleased] ```ruby query = Dynamodb::Api.query