Skip to content

Commit

Permalink
Merge pull request #40 from raphaelcangucu/fixLastEvaluatedKey
Browse files Browse the repository at this point in the history
Error with the Dynamo Pagination
  • Loading branch information
kitar authored Dec 15, 2023
2 parents a2223b8 + f89077f commit 89efb21
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 3 deletions.
40 changes: 40 additions & 0 deletions src/Kitar/Dynamodb/Helpers/Collection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Kitar\Dynamodb\Helpers;

use Illuminate\Support\Collection as BaseCollection;

class Collection extends BaseCollection
{
/**
* @var array
*/
private $meta;

/**
* @param array $meta
* @return $this
*/
public function setMeta($meta)
{
$this->meta = $meta;

return $this;
}

/**
* Get meta data.
*/
public function getMeta()
{
return $this->meta;
}

/**
* Get LastEvaluatedKey from meta
*/
public function getLastEvaluatedKey()
{
return $this->meta['LastEvaluatedKey'] ?? null;
}
}
15 changes: 12 additions & 3 deletions src/Kitar/Dynamodb/Query/Processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

namespace Kitar\Dynamodb\Query;

use Aws\Result;
use Aws\DynamoDb\Marshaler;
use Aws\Result;
use Illuminate\Database\Query\Processors\Processor as BaseProcessor;
use Kitar\Dynamodb\Helpers\Collection;

class Processor extends BaseProcessor
{
Expand Down Expand Up @@ -56,6 +57,7 @@ public function processSingleItem(Result $awsResponse, $modelClass = null)
$item = (new $modelClass)->newFromBuilder($response['Item']);
unset($response['Item']);
$item->setMeta($response ?? null);

return $item;
}

Expand All @@ -72,7 +74,7 @@ public function processMultipleItems(Result $awsResponse, $modelClass = null)
return $response;
}

$items = collect();
$items = new Collection([]);

foreach ($response['Items'] as $item) {
$item = (new $modelClass)->newFromBuilder($item);
Expand All @@ -81,10 +83,16 @@ public function processMultipleItems(Result $awsResponse, $modelClass = null)

unset($response['Items']);

return $items->map(function ($item) use ($response) {
$items = $items->map(function ($item) use ($response) {
$item->setMeta($response);

return $item;
});

// set meta at the collection level
$items->setMeta($response);

return $items;
}

public function processBatchGetItems(Result $awsResponse, $modelClass = null)
Expand All @@ -108,6 +116,7 @@ public function processBatchGetItems(Result $awsResponse, $modelClass = null)

return $items->map(function ($item) use ($response) {
$item->setMeta($response);

return $item;
});
}
Expand Down

0 comments on commit 89efb21

Please sign in to comment.