diff --git a/src/Kitar/Dynamodb/Model/Model.php b/src/Kitar/Dynamodb/Model/Model.php index 1698f56..cb58bd3 100644 --- a/src/Kitar/Dynamodb/Model/Model.php +++ b/src/Kitar/Dynamodb/Model/Model.php @@ -343,6 +343,7 @@ public function __call($method, $parameters) { $allowedBuilderMethods = [ "select", + "count", "take", "limit", "index", diff --git a/src/Kitar/Dynamodb/Query/Builder.php b/src/Kitar/Dynamodb/Query/Builder.php index a1b51ed..cdbbeba 100644 --- a/src/Kitar/Dynamodb/Query/Builder.php +++ b/src/Kitar/Dynamodb/Query/Builder.php @@ -120,6 +120,12 @@ class Builder extends BaseBuilder */ protected $key_condition_query; + /** + * The attributes to be returned in the result + * @var string + */ + protected $selectAttributes = 'ALL_ATTRIBUTES'; + /** * Create a new query builder instance. * @@ -260,6 +266,27 @@ public function getWhereAs() return $this->where_as; } + /** + * Set select attributes + * + * @param string $selectAttributes + * @return $this + */ + protected function selectAttributes(string $selectAttributes) { + $this->selectAttributes = $selectAttributes; + + return $this; + } + + /** + * Get the selectAttributes attribute. + * + * @return string + */ + public function getSelectAttributes() { + return $this->selectAttributes; + } + /** * Get item. * @@ -368,6 +395,13 @@ public function batchWriteItem($request_items = []) return $this->process('batchWriteItem', null); } + public function count($columns = '*') { + // reset columns selection + $this->select([])->selectAttributes('COUNT'); + + return (int) $this->process('clientQuery', 'processCount'); + } + /** * @inheritdoc */ @@ -575,12 +609,13 @@ public function newQuery() * @param string $processor_method * @return array|\Illuminate\Support\Collection|\Aws\Result */ - protected function process($query_method, $processor_method) + protected function process($query_method, $processor_method = null) { // Compile columns and wheres attributes. // These attributes needs to interact with ExpressionAttributes during compile, // so it need to run before compileExpressionAttributes. $params = array_merge( + $this->grammar->compileSelectAttributes($this->selectAttributes), $this->grammar->compileProjectionExpression($this->columns, $this->expression_attributes), $this->grammar->compileConditions($this->filter_query), $this->grammar->compileConditions($this->condition_query), diff --git a/src/Kitar/Dynamodb/Query/Grammar.php b/src/Kitar/Dynamodb/Query/Grammar.php index 16413f3..dc7d555 100644 --- a/src/Kitar/Dynamodb/Query/Grammar.php +++ b/src/Kitar/Dynamodb/Query/Grammar.php @@ -46,6 +46,22 @@ public function __construct() $this->marshaler = new Marshaler; } + /** + * Compile the Select attribute. + * + * @param $select_attributes + * @return array + */ + public function compileSelectAttributes($select_attributes) { + if ($select_attributes === 'ALL_ATTRIBUTES') { + return []; + } + + return [ + 'Select' => $select_attributes, + ]; + } + /** * Compile the TableName attribute. * diff --git a/src/Kitar/Dynamodb/Query/Processor.php b/src/Kitar/Dynamodb/Query/Processor.php index 40d2fba..413121d 100644 --- a/src/Kitar/Dynamodb/Query/Processor.php +++ b/src/Kitar/Dynamodb/Query/Processor.php @@ -45,6 +45,18 @@ protected function unmarshal(Result $res) return $responseArray; } + public function processCount(Result $awsResponse, $modelClass = null) { + $response = $this->unmarshal($awsResponse); + + if (empty($modelClass)) { + return $response; + } + + if (! empty($response['Count'])) { + return $response['Count']; + } + } + public function processSingleItem(Result $awsResponse, $modelClass = null) { $response = $this->unmarshal($awsResponse);