From d406957589423606b0b4b887d00be0972edf9513 Mon Sep 17 00:00:00 2001 From: Claudio La Barbera Date: Sun, 26 Feb 2023 12:50:49 +0100 Subject: [PATCH] Allow KeyConditionExpression when model has composite key and partition 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 --- .gitignore | 1 + config/dynamodb.php | 2 +- src/ConditionAnalyzer/Analyzer.php | 12 ++++++++++-- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 93eedeb..abbc4a6 100644 --- a/.gitignore +++ b/.gitignore @@ -4,5 +4,6 @@ dynamodb_local_test.db /nbproject *.log .php_cs.cache +.idea .phpunit.result.cache diff --git a/config/dynamodb.php b/config/dynamodb.php index 79486a4..6aca280 100644 --- a/config/dynamodb.php +++ b/config/dynamodb.php @@ -39,7 +39,7 @@ ], 'aws_iam_role' => [ 'region' => env('DYNAMODB_REGION'), - 'debug' => true, + 'debug' => env('DYNAMODB_DEBUG'), ], 'local' => [ 'credentials' => [ diff --git a/src/ConditionAnalyzer/Analyzer.php b/src/ConditionAnalyzer/Analyzer.php index 1d023e5..7cf9435 100644 --- a/src/ConditionAnalyzer/Analyzer.php +++ b/src/ConditionAnalyzer/Analyzer.php @@ -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; @@ -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