misantron/dynamite

AWS DynamoDB data fixtures loader

v1.0.1 2022-07-18 12:18 UTC

This package is auto-updated.

Last update: 2022-11-21 14:48:45 UTC


README

Build Status Code Coverage Code Quality Packagist

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.

Install

The preferred way to install is through Composer. Run this command to install the latest stable version:

composer require --dev misantron/dynamite

Loading fixtures

Create table creation class

This feature is optional.
Fixture classes must implement Dynamite\TableInterface interface to be visible for a loader.

<?php

declare(strict_types=1);

namespace Fixtures;

use Dynamite\AbstractTable;
use Dynamite\TableInterface;

final class UsersTable extends AbstractTable implements TableInterface
{
    protected function configure(): void
    {
        $this
            ->setTableName('Users')
            ->addAttributes([
                ['Id', 'S'],
                ['Email', 'S'],
            ])
            ->addHashKey('Id')
            ->addGlobalSecondaryIndex('Emails', 'KEYS_ONLY', 'Email')
            ->setProvisionedThroughput(1, 1)
        ;
    }
}

Create a fixture loading class

Fixture classes must implement Dynamite\FixtureInterface interface to be visible for a loader.

<?php

declare(strict_types=1);

namespace Fixtures;

use Dynamite\AbstractFixture;
use Dynamite\FixtureInterface;

final class UserFixtures extends AbstractFixture implements FixtureInterface
{
    protected function configure(): void
    {
        $this
            ->setTableName('Users')
            ->addItems([
                [
                    'Id' => ['S' => 'e5502ec2-42a7-408b-9f03-f8e162b6257e'],
                    'Email' => ['S' => 'john.doe@example.com'],
                ],
                [
                    'Id' => ['S' => 'f0cf458c-4fc0-4dd8-ba5b-eca6dba9be63'],
                    'Email' => ['S' => 'robert.smith@example.com'],
                ],  
            ])
        ;
    }
}

Tables and fixtures loading

It's possible to provide fixtures loading path:

<?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

declare(strict_types=1);

$loader->addTable(new \App\Fixtures\UsersTable());
$loader->addFixture(new \App\Fixtures\UserFixtures());

Create tables and executing fixtures

To create database schema and load the fixtures in storage you should do the following:

<?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.

Debug logger

Execution process debug logs can be enabled by passing PSR-3 logger into executor:

<?php

declare(strict_types=1);

use AsyncAws\DynamoDb\DynamoDbClient;
use Dynamite\Executor;

// PSR-3 compatible implementation of Psr\Log\LoggerInterface
$logger = new Logger();

$executor = new Executor($dynamoDbClient, logger: $logger);