Skip to content

Commit

Permalink
Merge pull request #39 from walkersumida/issues/paging
Browse files Browse the repository at this point in the history
Issues/paging
  • Loading branch information
walkersumida authored Feb 25, 2019
2 parents 5c7068e + 75ab208 commit b6f1d43
Show file tree
Hide file tree
Showing 7 changed files with 147 additions and 38 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:/walkersumida/dynamodb-api/pull/39) `next` method

## [0.7.0] - 2018-12-23
### Added
Expand Down
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,22 @@ items = scan.all.items
|3 |3 |Model S |0.20120601e8 |0 |
|4 |1 |S2000 |0.19980101e8 |1 |


#### Next(Paging) [Unreleased]

```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

Expand Down Expand Up @@ -148,6 +164,22 @@ items = query.all.items
|:---|:---|:---|:---|:---|
|1 |1 |Accord |0.19760508e8 |0 |

#### Next(Paging) [Unreleased]

```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.
Expand Down
1 change: 1 addition & 0 deletions lib/dynamodb/api/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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__}"
Expand Down
14 changes: 13 additions & 1 deletion lib/dynamodb/api/query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 12 additions & 1 deletion lib/dynamodb/api/scan.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
63 changes: 45 additions & 18 deletions spec/dynamodb/api/query_spec.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
60 changes: 42 additions & 18 deletions spec/dynamodb/api/scan_spec.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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

0 comments on commit b6f1d43

Please sign in to comment.