diff --git a/README.md b/README.md index 82a557b..8e51f4f 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,7 @@ Supports all key types - primary hash key and composite keys. * [save](#save) / [saveAsync()](#saveasync) * [delete](#delete) / [deleteAsync()](#deleteasync) * [chunk](#chunk) + * [pagedCount](#pagedcount) * [limit() and take()](#limit-and-take) * [firstOrFail()](#firstorfail) * [findOrFail()](#findorfail) @@ -277,6 +278,19 @@ $model->chunk(10, function ($records) { }); ``` +#### pagedCount() + +```php +$countResult = $model->pagedCount(); +if($countResult->lastKey) { + //lastEvaluatedKey is already set to $model + //$countResultNext will contain counts from next page + $countResultNext = $model->pagedCount(); + //... +} + +``` + #### limit() and take() ```php diff --git a/src/DynamoDbQueryBuilder.php b/src/DynamoDbQueryBuilder.php index 54e584b..a68445e 100644 --- a/src/DynamoDbQueryBuilder.php +++ b/src/DynamoDbQueryBuilder.php @@ -676,6 +676,22 @@ public function count() return $res['Count']; } + + public function pagedCount() + { + $limit = isset($this->limit) ? $this->limit : static::MAX_LIMIT; + + $raw = $this->toDynamoDbQuery(['count(*)'], $limit); + + if ($raw->op === 'Scan') { + $res = $this->client->scan($raw->query); + } else { + $res = $this->client->query($raw->query); + } + $this->lastEvaluatedKey = Arr::get($res, 'LastEvaluatedKey'); + + return (object)['count' => $res['Count'], 'scanned_count' => $res['ScannedCount'], 'lastKey' => $this->lastEvaluatedKey]; + } public function decorate(Closure $closure) {