Skip to content

Commit 33a5c25

Browse files
committed
Support Limit and ExclusiveStartKey
1 parent a2c880c commit 33a5c25

File tree

3 files changed

+87
-1
lines changed

3 files changed

+87
-1
lines changed

src/Kitar/Dynamodb/Query/Builder.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ class Builder extends BaseBuilder
4141
'remove' => []
4242
];
4343

44+
/**
45+
* LastEvaluatedKey attribute.
46+
* @var array|null
47+
*/
48+
public $exclusive_start_key;
49+
4450
/**
4551
* ConsistentRead option.
4652
* @var boolean|null
@@ -131,6 +137,20 @@ public function key(array $key)
131137
return $this;
132138
}
133139

140+
/**
141+
* Set the ExclusiveStartKey option.
142+
* Unlike other methods, this $key should be marshaled beforehand.
143+
*
144+
* @param array $key
145+
* @return $this
146+
*/
147+
public function exclusiveStartKey($key)
148+
{
149+
$this->exclusive_start_key = $key;
150+
151+
return $this;
152+
}
153+
134154
/**
135155
* Set the ConsistentRead option.
136156
* @param bool $active
@@ -215,7 +235,6 @@ public function deleteItem($key)
215235
public function updateItem($item)
216236
{
217237
foreach ($item as $name => $value) {
218-
219238
$name = $this->expression_attributes->addName($name);
220239

221240
// If value is null, it will pass to REMOVE actions.
@@ -413,6 +432,8 @@ protected function process($query_method, $processor_method)
413432
$this->grammar->compileKey($this->key),
414433
$this->grammar->compileItem($this->item),
415434
$this->grammar->compileUpdates($this->updates),
435+
$this->grammar->compileDynamodbLimit($this->limit),
436+
$this->grammar->compileExclusiveStartKey($this->exclusive_start_key),
416437
$this->grammar->compileConsistentRead($this->consistent_read),
417438
$this->grammar->compileExpressionAttributes($this->expression_attributes),
418439
);

src/Kitar/Dynamodb/Query/Grammar.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,34 @@ public function compileUpdates($updates)
130130
];
131131
}
132132

133+
/**
134+
* Compile the Limit attribute.
135+
*
136+
* @param int|null $limit
137+
* @return array
138+
*/
139+
public function compileDynamodbLimit($limit)
140+
{
141+
if ($limit === null) {
142+
return [];
143+
}
144+
145+
return [
146+
'Limit' => $limit
147+
];
148+
}
149+
150+
public function compileExclusiveStartKey($key)
151+
{
152+
if (empty($key)) {
153+
return [];
154+
}
155+
156+
return [
157+
'ExclusiveStartKey' => $key
158+
];
159+
}
160+
133161
/**
134162
* Compile the ConsistentRead attribute.
135163
* @param bool $bool

tests/Query/BuilderTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,43 @@ public function it_can_set_key()
7474
$this->assertEquals($params, $query['params']);
7575
}
7676

77+
/** @test */
78+
public function it_can_set_limit()
79+
{
80+
$params = [
81+
'TableName' => 'ProductCatalog',
82+
'Limit' => 5
83+
];
84+
85+
$query = $this->newQuery('ProductCatalog')
86+
->limit(5)
87+
->scan();
88+
89+
$this->assertEquals($params, $query['params']);
90+
}
91+
92+
/** @test */
93+
public function it_can_set_exclusive_start_key()
94+
{
95+
$params = [
96+
'TableName' => 'ProductCatalog',
97+
'ExclusiveStartKey' => [
98+
'Id' => [
99+
'N' => '101'
100+
]
101+
]
102+
];
103+
104+
$query = $this->newQuery('ProductCatalog')
105+
->ExclusiveStartKey([
106+
'Id' => [
107+
'N' => '101'
108+
]
109+
])->scan();
110+
111+
$this->assertEquals($params, $query['params']);
112+
}
113+
77114
/** @test */
78115
public function it_can_set_consistent_read()
79116
{

0 commit comments

Comments
 (0)