Skip to content

Commit

Permalink
Allow KeyConditionExpression when model has composite key and partiti…
Browse files Browse the repository at this point in the history
…on key only is passed in where (#254)

* Allow KeyConditionExpression when model has composite key and partition key only is passed in where

* fix: check if all conditions columns are present in model key names

* fix: array_search can return 0 so we can't use only ! but we have to check if the result is === false

* aws_iam_role debug taken from env

* excluded .idea folder from git

* changed composer info

* fix check on wrong place

* use APP_DEBUG if DYNAMO_DEBUG is not set

* fixed check on condition

* only DYNAMODB_DEBUG

---------

Co-authored-by: Claudio La Barbera <[email protected]>
  • Loading branch information
thebatclaudio and Claudio La Barbera authored Feb 26, 2023
1 parent 9638116 commit d406957
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ dynamodb_local_test.db
/nbproject
*.log
.php_cs.cache
.idea

.phpunit.result.cache
2 changes: 1 addition & 1 deletion config/dynamodb.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
],
'aws_iam_role' => [
'region' => env('DYNAMODB_REGION'),
'debug' => true,
'debug' => env('DYNAMODB_DEBUG'),
],
'local' => [
'credentials' => [
Expand Down
12 changes: 10 additions & 2 deletions src/ConditionAnalyzer/Analyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,18 @@ public function isExactSearch()
return false;
}

if (count($this->conditions) !== count($this->model->getKeyNames())) {
return false;
}

foreach ($this->conditions as $condition) {
if (Arr::get($condition, 'type') !== ComparisonOperator::EQ) {
return false;
}

if (array_search(Arr::get($condition, 'column'), $this->model->getKeyNames()) === false) {
return false;
}
}

return true;
Expand Down Expand Up @@ -202,8 +210,8 @@ private function hasValidQueryOperator($hash, $range = null)
$hashConditionType = $this->getCondition($hash)['type'] ?? null;
$validQueryOp = ComparisonOperator::isValidQueryDynamoDbOperator($hashConditionType);

if ($validQueryOp && $range) {
$rangeConditionType = $this->getCondition($range)['type'] ?? null;
if ($validQueryOp && $range && $this->getCondition($range) !== null) {
$rangeConditionType = $this->getCondition($range)['type'];
$validQueryOp = ComparisonOperator::isValidQueryDynamoDbOperator(
$rangeConditionType,
true
Expand Down

0 comments on commit d406957

Please sign in to comment.