Skip to content

Commit 52b6fd8

Browse files
committed
Make the model interface more fluent
1 parent b836136 commit 52b6fd8

File tree

3 files changed

+81
-27
lines changed

3 files changed

+81
-27
lines changed

README.md

Lines changed: 43 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ This package provides QueryBuilder for DynamoDB.
2626
- [Using Global Secondary Indexes in DynamoDB](#using-global-secondary-indexes-in-dynamodb)
2727
- [Querying a Global Secondary Index](#querying-a-global-secondary-index)
2828
- [Models](#models)
29-
- [Binding model to the QueryBuilder](#binding-model-to-the-querybuilder)
29+
- [Basic Usage of Model](#basic-usage-of-model)
30+
- [Calling Builder methods through model](#calling-builder-methods-through-model)
31+
- [CRUD operations](#crud-operations)
3032
- [Authentication](#authentication)
3133
- [Make user model](#make-user-model)
3234
- [Make custom user provider](#make-custom-user-provider)
@@ -290,15 +292,17 @@ $response = DB::table('Reply')
290292

291293
## Models
292294

293-
`Kitar\Dynamodb\Model\Model` is a experimental Model implementation for this package.
295+
`Kitar\Dynamodb\Model\Model` is an experimental Model implementation for this package.
294296

295-
It works like Eloquent Model, but there is no model querying features. Model's features to interact DynamoDB is only `save`, `update` and `delete`.
297+
It works like Eloquent Model, but instead of forwarding calls to "Model" Query, we forward call directly to "DynamoDB" Query.
296298

297-
Instead we don't have model query, we'll tell the QueryBuilder to instantiate DynamoDB response with the specified model.
299+
Because there is no "Model" Query, we can't use handy methods like `find` `create` `firstOrCreate` or something like that. We only have `save` `update` and `delete` for the Model methods to interact with DynamoDB. However, when we query through Model, DynamoDB's response items will be automatically converted to the model instance.
298300

299-
### Binding model to the QueryBuilder
301+
### Basic Usage of Model
300302

301-
For example, we have some user model below.
303+
Let's say we have some Authenticatable User model. (we'll use this example in the Authentication section as well)
304+
305+
Most attributes are the same as the original Eloquent Model, but there are few DynamoDB specific attributes. `table` `primaryKey` `sortKey` and `sortKeyDefault`.
302306

303307
```php
304308
<?php
@@ -349,35 +353,52 @@ class User extends Model implements AuthenticatableContract
349353
}
350354
```
351355

352-
Then, let QueryBuilder know model class by `usingModel`.
356+
#### Calling Builder methods through model
353357

354358
```php
355-
$response = DB::table('table_name')
356-
->usingModel(App\User::class)
357-
->getItem([
358-
'id' => '[email protected]',
359-
'type' => 'profile'
360-
]);
361-
```
359+
$response = User::getItem([
360+
'id' => '[email protected]',
361+
'type' => 'profile'
362+
]);
362363

363-
`$response['Item']` will be the User model instance.
364+
$user = $response['Item'];
365+
```
364366

365-
> behind the scene when specifying `usingModel`, Query Processor is converting each items to model instance with `(new $modelClass)-newFromBuilder($item)`.
367+
```php
368+
$response = User::filter('type', '=', 'profile')
369+
->scan();
366370

367-
After retrieving the model instance, we can `save`, `update` and `delete` in the same manner as Eloquent Model.
371+
$users = $response['Items'];
372+
```
368373

369-
Also, we can create new instance like below.
374+
#### CRUD operations
370375

371376
```php
372-
$user = new App\User([
377+
// create
378+
$newUser = new User([
373379
'id' => '[email protected]',
374380
'type' => 'profile'
375381
]);
376382

377-
$user->save();
378-
```
383+
$newUser->save();
384+
385+
// read
386+
$existingUser = User::getItem([
387+
'id' => '[email protected]',
388+
'type' => 'profile'
389+
])['Item'];
379390

380-
However, because we don't have model query, we can't use `create` or `firstOrCreate` or things like that.
391+
// update
392+
$existingUser->foo = 'bar';
393+
$existingUser->save();
394+
395+
$existingUser->update([
396+
'foo' => 'barbar'
397+
]);
398+
399+
// delete
400+
$existingUser->delete();
401+
```
381402

382403
## Authentication
383404

src/Kitar/Dynamodb/Model/AuthUserProvider.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use Illuminate\Contracts\Auth\Authenticatable;
66
use Illuminate\Contracts\Auth\UserProvider as BaseUserProvider;
7-
use Illuminate\Support\Facades\DB;
87
use Illuminate\Support\Facades\Hash;
98

109
class AuthUserProvider implements BaseUserProvider
@@ -28,9 +27,9 @@ public function retrieveById($identifier)
2827

2928
$this->model->$identifierName = $identifier;
3029

31-
$response = DB::table($this->model->getTable())
32-
->usingModel(get_class($this->model))
33-
->getItem($this->model->getKey());
30+
$response = $this->model->getItem(
31+
$this->model->getKey()
32+
);
3433

3534
return $response['Item'];
3635
}

src/Kitar/Dynamodb/Model/Model.php

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public function getKey()
7171
*/
7272
public function newQuery()
7373
{
74-
return $this->getConnection()->table($this->table);
74+
return $this->getConnection()->table($this->table)->usingModel(static::class);
7575
}
7676

7777
/**
@@ -238,4 +238,38 @@ public function delete()
238238

239239
return true;
240240
}
241+
242+
/**
243+
* @inheritdoc
244+
*/
245+
public function __call($method, $parameters)
246+
{
247+
$allowedBuilderMethods = [
248+
"index",
249+
"key",
250+
"exclusiveStartKey",
251+
"consistentRead",
252+
"dryRun",
253+
"getItem",
254+
"putItem",
255+
"deleteItem",
256+
"updateItem",
257+
"scan",
258+
"filter",
259+
"filterIn",
260+
"filterBetween",
261+
"condition",
262+
"conditionIn",
263+
"conditionBetween",
264+
"keyCondition",
265+
"keyConditionIn",
266+
"keyConditionBetween",
267+
];
268+
269+
if (! in_array($method, $allowedBuilderMethods)) {
270+
static::throwBadMethodCallException($method);
271+
}
272+
273+
return $this->forwardCallTo($this->newQuery(), $method, $parameters);
274+
}
241275
}

0 commit comments

Comments
 (0)