Skip to content

Commit

Permalink
Support ScanIndexForward option
Browse files Browse the repository at this point in the history
  • Loading branch information
kitar committed Oct 27, 2021
1 parent 9554d45 commit 524180e
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 0 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ A DynamoDB based Eloquent model and Query builder for Laravel.
* [Working with Queries](#working-with-queries)
+ [query() and keyCondition()](#query-and-keycondition)
+ [keyConditionBetween()](#keyconditionbetween)
+ [Sort order](#sort-order)
* [Working with Scans](#working-with-scans)
+ [scan()](#scan)
* [Filtering the Results](#filtering-the-results)
Expand Down Expand Up @@ -559,6 +560,17 @@ $response = DB::table('Thread')
->query();
```

#### Sort order

`query` results are always sorted by the sort key value. To reverse the order, set the `ScanIndexForward` parameter to `false`.

```php
$response = DB::table('Thread')
->keyCondition('ForumName', '=', 'Amazon DynamoDB')
->scanIndexForward(false)
->query();
```

### Working with Scans

#### scan()
Expand Down
19 changes: 19 additions & 0 deletions src/Kitar/Dynamodb/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ class Builder extends BaseBuilder
'remove' => []
];

/**
* ScanIndexForward option.
*/
public $scan_index_forward;

/**
* LastEvaluatedKey option.
* @var array|null
Expand Down Expand Up @@ -153,6 +158,19 @@ public function key(array $key)
return $this;
}

/**
* Set the ScanIndexForward option.
*
* @param bool $bool
* @return $this
*/
public function scanIndexForward($bool)
{
$this->scan_index_forward = $bool;

return $this;
}

/**
* Set the ExclusiveStartKey option.
*
Expand Down Expand Up @@ -523,6 +541,7 @@ protected function process($query_method, $processor_method)
$this->grammar->compileItem($this->item),
$this->grammar->compileUpdates($this->updates),
$this->grammar->compileDynamodbLimit($this->limit),
$this->grammar->compileScanIndexForward($this->scan_index_forward),
$this->grammar->compileExclusiveStartKey($this->exclusive_start_key),
$this->grammar->compileConsistentRead($this->consistent_read),
$this->grammar->compileExpressionAttributes($this->expression_attributes)
Expand Down
11 changes: 11 additions & 0 deletions src/Kitar/Dynamodb/Query/Grammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,17 @@ public function compileDynamodbLimit($limit)
];
}

public function compileScanIndexForward($bool)
{
if (is_null($bool)) {
return [];
}

return [
'ScanIndexForward' => $bool
];
}

public function compileExclusiveStartKey($key)
{
if (empty($key)) {
Expand Down
19 changes: 19 additions & 0 deletions tests/Query/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,25 @@ public function it_can_set_limit()
$this->assertEquals($params, $query['params']);
}

/** @test */
public function it_can_set_scan_index_forward()
{
$params = [
'TableName' => 'ProductCatalog',
'ScanIndexForward' => false,
'Key' => [
'Id' => [
'N' => '101'
]
]
];
$query = $this->newQuery('ProductCatalog')
->scanIndexForward(false)
->getItem(['Id'=> 101]);

$this->assertEquals($params, $query['params']);
}

/** @test */
public function it_can_set_exclusive_start_key()
{
Expand Down

0 comments on commit 524180e

Please sign in to comment.