@@ -46,7 +46,7 @@ class Builder
4646 * @var string[]
4747 */
4848 protected $ passthru = [
49- 'toArray ' , 'insert '
49+ 'toArray ' , 'insert ' , ' putItemBatch ' , ' deleteItemBatch '
5050 ];
5151
5252 public function __construct (QueryBuilder $ query )
@@ -113,6 +113,89 @@ public function first(array $columns = ['*'], string $mode = FetchMode::QUERY):
113113 return is_null ($ item ) ? null : $ this ->model ->newFromBuilder ($ item );
114114 }
115115
116+ /**
117+ * Get the first record matching the attributes or create it.
118+ *
119+ * @param array|string $key
120+ * @param array $values
121+ * @return Model
122+ */
123+ public function firstOrCreate ($ key , array $ values = []): Model
124+ {
125+ $ model = $ this ->firstOrNew ($ key , $ values );
126+
127+ if ($ model ->exists ) {
128+ return $ model ;
129+ }
130+
131+ $ model ->save ();
132+
133+ return $ model ;
134+ }
135+
136+ /**
137+ * Get the first record matching the attributes or instantiate it.
138+ *
139+ * @param array $key
140+ * @param array $values
141+ * @return Model
142+ */
143+ public function firstOrNew (array $ key , array $ values = [])
144+ {
145+ $ model = $ this ->find ($ key );
146+
147+ return is_null ($ model ) ? $ this ->newModelInstance ($ values )->setKey ($ key ) : $ model ;
148+ }
149+
150+ /**
151+ * Create or update a record matching the attributes, and fill it with values.
152+ *
153+ * @param array $key
154+ * @param array $values
155+ * @return Model
156+ */
157+ public function updateOrCreate (array $ key , array $ values = [])
158+ {
159+ $ model = $ this ->firstOrNew ($ key , $ values );
160+
161+ $ model ->exists ? $ model ->update ($ values ) : $ model ->save ();
162+
163+ return $ model ;
164+ }
165+
166+ /**
167+ * Alias of getItemBatch
168+ *
169+ * @param $keys
170+ * @param int $chunkSize
171+ * @alias getItemBatch()
172+ * @return \Illuminate\Support\Collection
173+ */
174+ public function findMany ($ keys , int $ chunkSize = 100 )
175+ {
176+ return $ this ->getItemBatch ($ keys , $ chunkSize );
177+ }
178+
179+ /**
180+ * Find many models in a single request
181+ *
182+ * @param $keys
183+ * @param int $chunkSize
184+ * @return \Illuminate\Support\Collection
185+ */
186+ public function getItemBatch ($ keys , int $ chunkSize = 100 )
187+ {
188+ $ primaryKey = $ this ->model ->getKeysName ();
189+
190+ $ keys = array_map (function ($ key ) use ($ primaryKey ) {
191+ return array_combine ($ primaryKey , $ key );
192+ }, $ keys );
193+
194+ return $ this ->query ->getItemBatch ($ keys , $ chunkSize )->transform (function ($ item ) {
195+ return $ this ->model ->newFromBuilder ($ item );
196+ });
197+ }
198+
116199 /**
117200 * Query items from database using query mode
118201 *
@@ -160,9 +243,9 @@ protected function checkFetchMode($mode)
160243 * Save a new model and return the instance.
161244 *
162245 * @param array $attributes
163- * @return Model|$this
246+ * @return Model
164247 */
165- public function create (array $ attributes = [])
248+ public function create (array $ attributes = []): Model
166249 {
167250 return tap ($ this ->newModelInstance ($ attributes ), function (Model $ instance ) {
168251 $ instance ->save ();
@@ -191,7 +274,6 @@ public function update(array $values, string $returnValues = ReturnValues::NONE)
191274 return $ this ->query ->update ($ values );
192275 }
193276
194-
195277 /**
196278 * Query items from database using scan mode
197279 *
@@ -213,7 +295,7 @@ public function scan($columns = [])
213295 * Create a new instance of the model being queried.
214296 *
215297 * @param array $attributes
216- * @return \Illuminate\Database\Eloquent\ Model|static
298+ * @return Model
217299 */
218300 public function newModelInstance (array $ attributes = [])
219301 {
0 commit comments