Skip to content

Commit 147b252

Browse files
committed
Update fetch mode
1 parent cb2c9dc commit 147b252

File tree

7 files changed

+142
-236
lines changed

7 files changed

+142
-236
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@
3232
"illuminate/support": "^6|^7|^8",
3333
"illuminate/pagination": "^6|^7|^8",
3434
"illuminate/database": "^6|^7|^8",
35-
"aws/aws-sdk-php": "^3"
35+
"aws/aws-sdk-php": "^3",
36+
"ext-json": "*"
3637
},
3738
"extra": {
3839
"laravel": {

src/Concerns/BuildQueries.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,23 @@
22

33
namespace Mehedi\LaravelDynamoDB\Concerns;
44

5-
use Mehedi\LaravelDynamoDB\Query\FetchMode;
6-
75
trait BuildQueries
86
{
97
/**
108
* Chunk the results of the query.
119
*
1210
* @param int $count
1311
* @param callable $callback
14-
* @param string $mode
1512
* @return bool
1613
*/
17-
public function chunk(int $count, callable $callback, string $mode = FetchMode::QUERY): bool
14+
public function chunk($count, callable $callback): bool
1815
{
19-
$this->limit($count);
16+
$this->limit((int) $count);
2017

2118
$page = 1;
2219

2320
do {
24-
$results = $this->get([], $mode);
21+
$results = $this->get([]);
2522

2623
if (call_user_func_array($callback, [$results, $page]) === false) {
2724
return false;

src/DynamoDBConnection.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ class DynamoDBConnection extends Connection
4242

4343
public function __construct($config)
4444
{
45+
parent::__construct(null);
46+
4547
$this->config = $config;
4648
$this->makeClient($config);
4749

@@ -167,4 +169,12 @@ public function raw($value): RawExpression
167169
{
168170
return new RawExpression($value);
169171
}
172+
173+
/**
174+
* @inheritDoc
175+
*/
176+
public function disconnect()
177+
{
178+
$this->setClient(null);
179+
}
170180
}

src/Eloquent/Builder.php

Lines changed: 27 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use Mehedi\LaravelDynamoDB\Concerns\BuildQueries;
99
use Mehedi\LaravelDynamoDB\Query\Builder as QueryBuilder;
1010
use Mehedi\LaravelDynamoDB\Query\FetchMode;
11-
use Mehedi\LaravelDynamoDB\Query\ReturnValues;
1211
use Illuminate\Database\Eloquent\ModelNotFoundException;
1312

1413
/**
@@ -19,10 +18,11 @@
1918
* @method Builder keyCondition(string $column, $operator, $value = null)
2019
* @method Builder keyConditionBetween(string $column, string $from, string $to)
2120
* @method Builder keyConditionBeginsWith(string $column, string $value)
21+
* @method Builder getQuery()
2222
*
2323
* @see \Mehedi\LaravelDynamoDB\Query\Builder
2424
*/
25-
class Builder
25+
class Builder extends \Illuminate\Database\Eloquent\Builder
2626
{
2727
use ForwardsCalls, BuildQueries;
2828

@@ -49,24 +49,18 @@ class Builder
4949
'toArray', 'insert', 'putItemBatch', 'deleteItemBatch'
5050
];
5151

52-
public function __construct(QueryBuilder $query)
53-
{
54-
$this->query = $query;
55-
}
56-
5752
/**
5853
* Get a paginator only supporting simple next link.
5954
*
6055
* @param int $perPage
6156
* @param array $columns
6257
* @param string $cursorName
6358
* @param null $cursor
64-
* @param string $mode
6559
* @return \Mehedi\LaravelDynamoDB\Pagination\CursorPaginator
6660
*/
67-
public function cursorPaginate(int $perPage, array $columns = [], $cursorName = 'cursor', $cursor = null, $mode = FetchMode::QUERY)
61+
public function cursorPaginate($perPage = null, $columns = [], $cursorName = 'cursor', $cursor = null)
6862
{
69-
return $this->query->cursorPaginate($perPage, $columns, $cursorName, $cursor, $mode)
63+
return $this->query->cursorPaginate($perPage, $columns, $cursorName, $cursor)
7064
->through(function ($item) {
7165
return $this->model->newFromBuilder($item);
7266
});
@@ -79,7 +73,7 @@ public function cursorPaginate(int $perPage, array $columns = [], $cursorName =
7973
* @param array $columns
8074
* @return Model|null
8175
*/
82-
public function find($key, array $columns = [])
76+
public function find($key, $columns = [])
8377
{
8478
if (is_string($key)) {
8579
$this->key($key);
@@ -88,10 +82,10 @@ public function find($key, array $columns = [])
8882
}
8983

9084
if (! empty($columns)) {
91-
$this->query->select(...$columns);
85+
$this->query->select($columns);
9286
}
9387

94-
$item = $this->query->from($this->model->getTable())->find();
88+
$item = $this->query->from($this->model->getTable())->getItem();
9589

9690
return array_key_exists('Item', $item) ? $this->model->newFromBuilder($item['Item']) : null;
9791
}
@@ -105,7 +99,7 @@ public function find($key, array $columns = [])
10599
*
106100
* @throws ModelNotFoundException
107101
*/
108-
public function findOrFail($key, array $columns = [])
102+
public function findOrFail($key, $columns = [])
109103
{
110104
$model = $this->find($key, $columns);
111105

@@ -121,12 +115,11 @@ public function findOrFail($key, array $columns = [])
121115
* Get the first item
122116
*
123117
* @param string[] $columns
124-
* @param string $mode
125118
* @return Model|null
126119
*/
127-
public function first(array $columns = ['*'], string $mode = FetchMode::QUERY): ?Model
120+
public function first($columns = []): ?Model
128121
{
129-
$item = $this->query->first($columns, $mode);
122+
$item = $this->query->first($columns);
130123

131124
return is_null($item) ? null : $this->model->newFromBuilder($item);
132125
}
@@ -138,7 +131,7 @@ public function first(array $columns = ['*'], string $mode = FetchMode::QUERY):
138131
* @param array $values
139132
* @return Model
140133
*/
141-
public function firstOrCreate($key, array $values = []): Model
134+
public function firstOrCreate(array $key = [], array $values = []): Model
142135
{
143136
$model = $this->firstOrNew($key, $values);
144137

@@ -158,7 +151,7 @@ public function firstOrCreate($key, array $values = []): Model
158151
* @param array $values
159152
* @return Model
160153
*/
161-
public function firstOrNew(array $key, array $values = [])
154+
public function firstOrNew(array $key = [], array $values = [])
162155
{
163156
$model = $this->find($key);
164157

@@ -172,7 +165,7 @@ public function firstOrNew(array $key, array $values = [])
172165
* @param array $values
173166
* @return Model
174167
*/
175-
public function updateOrCreate(array $key, array $values = [])
168+
public function updateOrCreate(array $key = [], array $values = [])
176169
{
177170
$model = $this->firstOrNew($key, $values);
178171

@@ -185,31 +178,34 @@ public function updateOrCreate(array $key, array $values = [])
185178
* Alias of getItemBatch
186179
*
187180
* @param $keys
188-
* @param int $chunkSize
189-
* @alias getItemBatch()
181+
* @param array $columns
190182
* @return \Illuminate\Support\Collection
183+
* @alias getItemBatch()
191184
*/
192-
public function findMany($keys, int $chunkSize = 100)
185+
public function findMany($keys, $columns = [])
193186
{
194-
return $this->getItemBatch($keys, $chunkSize);
187+
if (!empty($columns)) {
188+
$this->select($columns);
189+
}
190+
191+
return $this->getItemBatch($keys);
195192
}
196193

197194
/**
198195
* Find many models in a single request
199196
*
200197
* @param $keys
201-
* @param int $chunkSize
202198
* @return \Illuminate\Support\Collection
203199
*/
204-
public function getItemBatch($keys, int $chunkSize = 100)
200+
public function getItemBatch($keys)
205201
{
206202
$primaryKey = $this->model->getKeysName();
207203

208204
$keys = array_map(function ($key) use ($primaryKey) {
209205
return array_combine($primaryKey, $key);
210206
}, $keys);
211207

212-
return $this->query->getItemBatch($keys, $chunkSize)->transform(function ($item) {
208+
return $this->query->getItemBatch($keys, $this->readChunkSize)->transform(function ($item) {
213209
return $this->model->newFromBuilder($item);
214210
});
215211
}
@@ -235,14 +231,13 @@ public function query(array $columns = [])
235231
* Get items collection
236232
*
237233
* @param array $columns
238-
* @param string $mode
239234
* @return ItemCollection
240235
*/
241-
public function get(array $columns = [], string $mode = FetchMode::QUERY)
236+
public function get($columns = [])
242237
{
243-
$this->checkFetchMode($mode);
238+
$this->checkFetchMode($this->query->fetchMode);
244239

245-
return call_user_func_array([$this, $mode], [$columns]);
240+
return call_user_func_array([$this, $this->query->fetchMode], [$columns]);
246241
}
247242

248243
/**
@@ -270,24 +265,13 @@ public function create(array $attributes = []): Model
270265
});
271266
}
272267

273-
/**
274-
* Get the query builder instance
275-
*
276-
* @return QueryBuilder
277-
*/
278-
public function getQuery()
279-
{
280-
return $this->query;
281-
}
282-
283268
/**
284269
* Update model
285270
*
286271
* @param array $values
287-
* @param string $returnValues
288272
* @return array
289273
*/
290-
public function update(array $values, string $returnValues = ReturnValues::NONE): array
274+
public function update(array $values)
291275
{
292276
return $this->query->update($values);
293277
}
@@ -309,43 +293,6 @@ public function scan($columns = [])
309293
});
310294
}
311295

312-
/**
313-
* Create a new instance of the model being queried.
314-
*
315-
* @param array $attributes
316-
* @return Model
317-
*/
318-
public function newModelInstance(array $attributes = [])
319-
{
320-
return $this->model->newInstance($attributes)->setConnection(
321-
$this->query->connection->getName()
322-
);
323-
}
324-
325-
/**
326-
* Set a model instance for the model being queried.
327-
*
328-
* @param Model $model
329-
* @return $this
330-
*/
331-
public function setModel(Model $model): Builder
332-
{
333-
$this->model = $model;
334-
$this->query->from($model->getTable());
335-
336-
return $this;
337-
}
338-
339-
/**
340-
* Get the model instance being queried.
341-
*
342-
* @return Model
343-
*/
344-
public function getModel(): Model
345-
{
346-
return $this->model;
347-
}
348-
349296
/**
350297
* Set model key
351298
*
@@ -373,32 +320,4 @@ public function key($primaryKey, $sortKey = null): Builder
373320

374321
return $this;
375322
}
376-
377-
/**
378-
* Get the connection
379-
*
380-
* @return \Illuminate\Database\ConnectionInterface|\Mehedi\LaravelDynamoDB\DynamoDBConnection
381-
*/
382-
public function getConnection()
383-
{
384-
return $this->query->getConnection();
385-
}
386-
387-
/**
388-
* Handle non existence method calling
389-
*
390-
* @param $method
391-
* @param $parameters
392-
* @return $this|false|mixed
393-
*/
394-
public function __call($method, $parameters)
395-
{
396-
if (in_array($method, $this->passthru)) {
397-
return call_user_func_array([$this->query, $method], $parameters);
398-
}
399-
400-
$this->forwardCallTo($this->query, $method, $parameters);
401-
402-
return $this;
403-
}
404323
}

src/Pagination/CursorPaginator.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,24 @@ public function nextCursor()
106106
return null;
107107
}
108108

109+
/**
110+
* Render the paginator using the given view.
111+
*
112+
* @param string|null $view
113+
* @param array $data
114+
* @return \Illuminate\Contracts\Support\Htmlable
115+
*/
116+
public function links($view = null, $data = [])
117+
{
118+
return $this->render($view, $data);
119+
}
120+
109121
/**
110122
* Render the paginator using a given view.
111123
*
112124
* @param string|null $view
113125
* @param array $data
114-
* @return \Illuminate\Contracts\View\View
126+
* @return \Illuminate\Contracts\Support\Htmlable
115127
*/
116128
public function render($view = null, $data = [])
117129
{
@@ -141,7 +153,6 @@ public function toArray()
141153
*
142154
* @return array
143155
*/
144-
#[ReturnTypeWillChange]
145156
public function jsonSerialize()
146157
{
147158
return $this->toArray();

0 commit comments

Comments
 (0)