Skip to content

Commit 524180e

Browse files
committed
Support ScanIndexForward option
1 parent 9554d45 commit 524180e

File tree

4 files changed

+61
-0
lines changed

4 files changed

+61
-0
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ A DynamoDB based Eloquent model and Query builder for Laravel.
3838
* [Working with Queries](#working-with-queries)
3939
+ [query() and keyCondition()](#query-and-keycondition)
4040
+ [keyConditionBetween()](#keyconditionbetween)
41+
+ [Sort order](#sort-order)
4142
* [Working with Scans](#working-with-scans)
4243
+ [scan()](#scan)
4344
* [Filtering the Results](#filtering-the-results)
@@ -559,6 +560,17 @@ $response = DB::table('Thread')
559560
->query();
560561
```
561562

563+
#### Sort order
564+
565+
`query` results are always sorted by the sort key value. To reverse the order, set the `ScanIndexForward` parameter to `false`.
566+
567+
```php
568+
$response = DB::table('Thread')
569+
->keyCondition('ForumName', '=', 'Amazon DynamoDB')
570+
->scanIndexForward(false)
571+
->query();
572+
```
573+
562574
### Working with Scans
563575

564576
#### scan()

src/Kitar/Dynamodb/Query/Builder.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ class Builder extends BaseBuilder
4242
'remove' => []
4343
];
4444

45+
/**
46+
* ScanIndexForward option.
47+
*/
48+
public $scan_index_forward;
49+
4550
/**
4651
* LastEvaluatedKey option.
4752
* @var array|null
@@ -153,6 +158,19 @@ public function key(array $key)
153158
return $this;
154159
}
155160

161+
/**
162+
* Set the ScanIndexForward option.
163+
*
164+
* @param bool $bool
165+
* @return $this
166+
*/
167+
public function scanIndexForward($bool)
168+
{
169+
$this->scan_index_forward = $bool;
170+
171+
return $this;
172+
}
173+
156174
/**
157175
* Set the ExclusiveStartKey option.
158176
*
@@ -523,6 +541,7 @@ protected function process($query_method, $processor_method)
523541
$this->grammar->compileItem($this->item),
524542
$this->grammar->compileUpdates($this->updates),
525543
$this->grammar->compileDynamodbLimit($this->limit),
544+
$this->grammar->compileScanIndexForward($this->scan_index_forward),
526545
$this->grammar->compileExclusiveStartKey($this->exclusive_start_key),
527546
$this->grammar->compileConsistentRead($this->consistent_read),
528547
$this->grammar->compileExpressionAttributes($this->expression_attributes)

src/Kitar/Dynamodb/Query/Grammar.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,17 @@ public function compileDynamodbLimit($limit)
152152
];
153153
}
154154

155+
public function compileScanIndexForward($bool)
156+
{
157+
if (is_null($bool)) {
158+
return [];
159+
}
160+
161+
return [
162+
'ScanIndexForward' => $bool
163+
];
164+
}
165+
155166
public function compileExclusiveStartKey($key)
156167
{
157168
if (empty($key)) {

tests/Query/BuilderTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,25 @@ public function it_can_set_limit()
108108
$this->assertEquals($params, $query['params']);
109109
}
110110

111+
/** @test */
112+
public function it_can_set_scan_index_forward()
113+
{
114+
$params = [
115+
'TableName' => 'ProductCatalog',
116+
'ScanIndexForward' => false,
117+
'Key' => [
118+
'Id' => [
119+
'N' => '101'
120+
]
121+
]
122+
];
123+
$query = $this->newQuery('ProductCatalog')
124+
->scanIndexForward(false)
125+
->getItem(['Id'=> 101]);
126+
127+
$this->assertEquals($params, $query['params']);
128+
}
129+
111130
/** @test */
112131
public function it_can_set_exclusive_start_key()
113132
{

0 commit comments

Comments
 (0)