88use Mehedi \LaravelDynamoDB \Concerns \BuildQueries ;
99use Mehedi \LaravelDynamoDB \Query \Builder as QueryBuilder ;
1010use Mehedi \LaravelDynamoDB \Query \FetchMode ;
11- use Mehedi \LaravelDynamoDB \Query \ReturnValues ;
1211use Illuminate \Database \Eloquent \ModelNotFoundException ;
1312
1413/**
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}
0 commit comments