Skip to content

Commit

Permalink
Merge pull request #15 from marvinosswald/master
Browse files Browse the repository at this point in the history
::create() - improve compliance with laravel model by providing static create method
  • Loading branch information
kitar committed Feb 5, 2022
2 parents 602be30 + d54bab2 commit c914b83
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 1 deletion.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ A DynamoDB based Eloquent model and Query builder for Laravel.
+ [Retrieving all models](#retrieving-all-models)
+ [Retrieving a model](#retrieving-a-model)
+ [save()](#save)
+ [create()](#create)
+ [update()](#update)
+ [delete()](#delete)
+ [increment() / decrement()](#increment--decrement)
Expand Down Expand Up @@ -229,12 +230,21 @@ If the model has sort key and `sortKeyDefault` is defined:
User::find('[email protected]'); // Partition key. sortKeyDefault will be used for Sort key.
```

#### create()

```php
$user = User::create([
'email' => '[email protected]',
'type' => 'profile' // Sort key. If we don't specify this, sortKeyDefault will be used.
]);
```

#### save()

```php
$user = new User([
'email' => '[email protected]',
'type' => 'profile' // Sort key. If we don't specify this, sortKeyDefault will be used.
'type' => 'profile'
]);

$user->save();
Expand Down
14 changes: 14 additions & 0 deletions src/Kitar/Dynamodb/Model/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,20 @@ public static function all($columns = [])
return static::scan($columns);
}

/**
* Save a new model and return the instance.
*
* @param array $fillables
* @param array $options
* @return \Kitar\Dynamodb\Model\Model|$this
*/
public static function create(array $fillables = [], array $options = [])
{
$instance = new static($fillables);
$instance->save($options);
return $instance;
}

/**
* Save the model to the database.
*
Expand Down
23 changes: 23 additions & 0 deletions tests/Model/ModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,29 @@ public function it_can_save_new_instance()
$user->save();
}

/** @test */
public function it_can_static_create_new_instance()
{
$params = [
'TableName' => 'User',
'Item' => [
'partition' => [
'S' => 'p'
]
],
'ConditionExpression' => 'attribute_not_exists(#1)',
'ExpressionAttributeNames' => [
'#1' => 'partition'
]
];

$connection = $this->newConnectionMock();
$connection->shouldReceive('putItem')->with($params);
$this->setConnectionResolver($connection);

UserD::create(['partition' => 'p']);
}

/** @test */
public function it_cannot_save_new_instance_without_required_key()
{
Expand Down
19 changes: 19 additions & 0 deletions tests/Model/UserD.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Kitar\Dynamodb\Tests\Model;

use Kitar\Dynamodb\Model\Model;
use Illuminate\Auth\Authenticatable;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;

class UserD extends Model implements AuthenticatableContract
{
use Authenticatable;

protected $table = 'User';
protected $primaryKey = 'partition';
protected $fillable = [
'partition'
];
public $timestamps = false;
}

0 comments on commit c914b83

Please sign in to comment.