-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from misantron/fixtures-refactoring
Library refactoring
- Loading branch information
Showing
48 changed files
with
1,383 additions
and
1,469 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,12 @@ | ||
# Dynamite | ||
# Dynamite - AWS DynamoDB fixtures | ||
|
||
[![Build Status](https://img.shields.io/github/workflow/status/misantron/dynamite/build.svg?style=flat-square)](https://github.com/misantron/dynamite/actions) | ||
[![Code Coverage](https://img.shields.io/codacy/coverage/14793b443be444dbb19c02ddca1b0118.svg?style=flat-square)](https://app.codacy.com/gh/misantron/dynamite/files) | ||
[![Code Quality](https://img.shields.io/codacy/grade/14793b443be444dbb19c02ddca1b0118.svg?style=flat-square)](https://app.codacy.com/gh/misantron/dynamite) | ||
[![Packagist](https://img.shields.io/packagist/v/misantron/dynamite.svg?style=flat-square)](https://packagist.org/packages/misantron/dynamite) | ||
|
||
AWS DynamoDB migrations and seeding tool | ||
Provide a simple way to manage and execute the loading of data fixtures for AWS DynamoDB storage. | ||
Library code design is heavily inspired by [doctrine/data-fixtures](https://github.com/doctrine/data-fixtures). | ||
|
||
## Install | ||
|
||
|
@@ -16,46 +17,58 @@ Run this command to install the latest stable version: | |
composer require --dev misantron/dynamite | ||
``` | ||
|
||
## Examples | ||
## Loading fixtures | ||
|
||
### Create table | ||
### Create table creation class | ||
|
||
This feature is optional. | ||
Fixture classes must implement `Dynamite\TableInterface` interface to be visible for a loader. | ||
|
||
```php | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Migrations; | ||
namespace Fixtures; | ||
|
||
use Dynamite\AbstractTable; | ||
use Dynamite\TableInterface; | ||
|
||
final class CreateUsersTable extends \Dynamite\AbstractMigration | ||
final class UsersTable extends AbstractTable implements TableInterface | ||
{ | ||
public function up(): void | ||
public function configure(): void | ||
{ | ||
$this | ||
->setTableName('Users') | ||
->addAttribute('Id', 'S') | ||
->addAttribute('Email', 'S') | ||
->addAttributes([ | ||
['Id', 'S'], | ||
['Email', 'S'], | ||
]) | ||
->addHashKey('Id') | ||
->addGlobalSecondaryIndex('Emails', 'KEYS_ONLY', 'Email') | ||
->setProvisionedThroughput(1, 1) | ||
->create() | ||
; | ||
} | ||
} | ||
``` | ||
|
||
### Create seeder | ||
### Create a fixture loading class | ||
|
||
Fixture classes must implement `Dynamite\FixtureInterface` interface to be visible for a loader. | ||
|
||
```php | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Seeders; | ||
namespace Fixtures; | ||
|
||
use Dynamite\AbstractFixture; | ||
use Dynamite\FixtureInterface; | ||
|
||
final class UsersTableSeeder extends \Dynamite\AbstractSeeder | ||
final class UserFixtures extends AbstractFixture implements FixtureInterface | ||
{ | ||
public function seed(): void | ||
public function configure(): void | ||
{ | ||
$this | ||
->setTableName('Users') | ||
|
@@ -69,8 +82,67 @@ final class UsersTableSeeder extends \Dynamite\AbstractSeeder | |
'Email' => ['S' => '[email protected]'], | ||
], | ||
]) | ||
->save() | ||
; | ||
} | ||
} | ||
``` | ||
|
||
### Tables and fixtures loading | ||
|
||
It's possible to provide fixtures loading path: | ||
|
||
```php | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Dynamite\Loader; | ||
use Symfony\Component\Validator\Validation; | ||
use Symfony\Component\Serializer\Serializer; | ||
|
||
$validator = Validation::createValidator(); | ||
$serializer = new Serializer(); | ||
|
||
$loader = new Loader($validator, $serializer); | ||
$loader->loadFromDirectory('/path/to/YourFixtures'); | ||
``` | ||
|
||
or loading each fixture or table class manually: | ||
|
||
```php | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use App\Fixtures; | ||
|
||
$loader->addTable(new UsersTable()); | ||
$loader->addFixture(new UserFixtures()); | ||
``` | ||
|
||
### Create tables and executing fixtures | ||
|
||
To create database schema and load the fixtures in storage you should do the following: | ||
|
||
```php | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use AsyncAws\DynamoDb\DynamoDbClient; | ||
use Dynamite\Executor; | ||
use Symfony\Component\Validator\Validation; | ||
use Symfony\Component\Serializer\Serializer; | ||
|
||
$validator = Validation::createValidator(); | ||
$serializer = new Serializer(); | ||
$dynamoDbClient = new DynamoDbClient(); | ||
|
||
$loader = new Loader($validator, $serializer); | ||
$loader->loadFromDirectory('/path/to/YourFixtures'); | ||
|
||
$executor = new Executor($dynamoDbClient); | ||
$executor->execute($loader->getFixtures(), $loader->getTables()); | ||
``` | ||
|
||
**Important!** Each executor class comes with a purger class which executed before, drop tables and truncate data. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.