Skip to content

Commit c914b83

Browse files
authored
Merge pull request #15 from marvinosswald/master
::create() - improve compliance with laravel model by providing static create method
2 parents 602be30 + d54bab2 commit c914b83

File tree

4 files changed

+67
-1
lines changed

4 files changed

+67
-1
lines changed

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ A DynamoDB based Eloquent model and Query builder for Laravel.
1616
+ [Retrieving all models](#retrieving-all-models)
1717
+ [Retrieving a model](#retrieving-a-model)
1818
+ [save()](#save)
19+
+ [create()](#create)
1920
+ [update()](#update)
2021
+ [delete()](#delete)
2122
+ [increment() / decrement()](#increment--decrement)
@@ -229,12 +230,21 @@ If the model has sort key and `sortKeyDefault` is defined:
229230
User::find('foo@bar.com'); // Partition key. sortKeyDefault will be used for Sort key.
230231
```
231232

233+
#### create()
234+
235+
```php
236+
$user = User::create([
237+
'email' => 'foo@bar.com',
238+
'type' => 'profile' // Sort key. If we don't specify this, sortKeyDefault will be used.
239+
]);
240+
```
241+
232242
#### save()
233243

234244
```php
235245
$user = new User([
236246
'email' => 'foo@bar.com',
237-
'type' => 'profile' // Sort key. If we don't specify this, sortKeyDefault will be used.
247+
'type' => 'profile'
238248
]);
239249

240250
$user->save();

src/Kitar/Dynamodb/Model/Model.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,20 @@ public static function all($columns = [])
129129
return static::scan($columns);
130130
}
131131

132+
/**
133+
* Save a new model and return the instance.
134+
*
135+
* @param array $fillables
136+
* @param array $options
137+
* @return \Kitar\Dynamodb\Model\Model|$this
138+
*/
139+
public static function create(array $fillables = [], array $options = [])
140+
{
141+
$instance = new static($fillables);
142+
$instance->save($options);
143+
return $instance;
144+
}
145+
132146
/**
133147
* Save the model to the database.
134148
*

tests/Model/ModelTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,29 @@ public function it_can_save_new_instance()
431431
$user->save();
432432
}
433433

434+
/** @test */
435+
public function it_can_static_create_new_instance()
436+
{
437+
$params = [
438+
'TableName' => 'User',
439+
'Item' => [
440+
'partition' => [
441+
'S' => 'p'
442+
]
443+
],
444+
'ConditionExpression' => 'attribute_not_exists(#1)',
445+
'ExpressionAttributeNames' => [
446+
'#1' => 'partition'
447+
]
448+
];
449+
450+
$connection = $this->newConnectionMock();
451+
$connection->shouldReceive('putItem')->with($params);
452+
$this->setConnectionResolver($connection);
453+
454+
UserD::create(['partition' => 'p']);
455+
}
456+
434457
/** @test */
435458
public function it_cannot_save_new_instance_without_required_key()
436459
{

tests/Model/UserD.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Kitar\Dynamodb\Tests\Model;
4+
5+
use Kitar\Dynamodb\Model\Model;
6+
use Illuminate\Auth\Authenticatable;
7+
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
8+
9+
class UserD extends Model implements AuthenticatableContract
10+
{
11+
use Authenticatable;
12+
13+
protected $table = 'User';
14+
protected $primaryKey = 'partition';
15+
protected $fillable = [
16+
'partition'
17+
];
18+
public $timestamps = false;
19+
}

0 commit comments

Comments
 (0)