misantron / dynamite
AWS DynamoDB data fixtures loader
Installs: 5 197
Dependents: 0
Suggesters: 0
Security: 0
Stars: 2
Watchers: 3
Forks: 0
Open Issues: 3
Requires
- php: ^8.1
- psr/log: ^2.0 || ^3.0
- symfony/console: ^5.4 || ^6.4 || ^7.0
- symfony/property-access: ^5.4 || ^6.4 || ^7.0
- symfony/serializer: ^5.4 || ^6.4 || ^7.0
- symfony/validator: ^5.4 || ^6.4 || ^7.0
- webmozart/assert: ^1.11
Requires (Dev)
- async-aws/dynamo-db: ^3.1
- aws/aws-sdk-php: 3.301.6
- fakerphp/faker: ^1.23
- phpstan/extension-installer: ^1.3
- phpstan/phpstan: ^1.10
- phpstan/phpstan-deprecation-rules: ^1.1
- phpstan/phpstan-phpunit: ^1.3
- phpstan/phpstan-strict-rules: ^1.5
- phpunit/phpunit: ^10.5 || ^11.0
- symfony/error-handler: ^5.4 || ^6.4 || ^7.0
- symplify/easy-coding-standard: ^12.1
- vimeo/psalm: ^5.23
Suggests
- async-aws/dynamo-db: Async first DynamoDb client from Symfony Core developers
- aws/aws-sdk-php: Official PHP AWS SDK library
This package is auto-updated.
Last update: 2024-12-22 13:44:33 UTC
README
Provide a simple way to manage and execute the loading of data fixtures for AWS DynamoDB storage.
Can use client from AWS PHP SDK or Async AWS under the hood.
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\Attribute\Groups; use Dynamite\Enum\KeyTypeEnum; use Dynamite\Enum\ProjectionTypeEnum; use Dynamite\Enum\ScalarAttributeTypeEnum; use Dynamite\TableInterface; use Dynamite\Schema\Attribute; #[Groups(['group1'])] // groups can be used optionally with console command final class UsersTable extends AbstractTable implements TableInterface { protected function configure(): void { $this ->setTableName('Users') ->addAttributes([ new Attribute('Id', ScalarAttributeTypeEnum::String, KeyTypeEnum::Hash), new Attribute('Email', ScalarAttributeTypeEnum::String), ]) ->addGlobalSecondaryIndex( 'Emails', ProjectionTypeEnum::KeysOnly, '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\Attribute\Groups; use Dynamite\FixtureInterface; use Dynamite\Schema\Record; use Dynamite\Schema\Value; #[Groups(['group1'])] // groups can be used optionally with console command final class UserFixtures extends AbstractFixture implements FixtureInterface { protected function configure(): void { $this ->setTableName('Users') ->addRecords([ new Record([ Value::stringValue('Id', 'e5502ec2-42a7-408b-9f03-f8e162b6257e'), Value::stringValue('Email', 'john.doe@example.com'), Value::boolValue('Active', true), ]), new Record([ Value::stringValue('Id', 'f0cf458c-4fc0-4dd8-ba5b-eca6dba9be63'), Value::stringValue('Email', 'robert.smith@example.com'), Value::boolValue('Active', true), ]), ]) ; } }
Tables and fixtures loading
It's possible to provide fixtures loading path:
<?php declare(strict_types=1); use Dynamite\Loader; use Dynamite\Serializer\PropertyNameConverter; use Symfony\Component\Serializer\Normalizer\BackedEnumNormalizer; use Symfony\Component\Serializer\Normalizer\ObjectNormalizer; use Symfony\Component\Serializer\Serializer; use Symfony\Component\Validator\Mapping\Loader\AnnotationLoader; use Symfony\Component\Validator\Validation; $validator = Validation::createValidatorBuilder() ->addLoader(new AnnotationLoader()) ->getValidator() ; $serializer = new Serializer([ new BackedEnumNormalizer(), new ObjectNormalizer(null, new PropertyNameConverter()), ]); $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 Dynamite\Client; use Dynamite\Executor; use Dynamite\Loader; use Dynamite\Serializer\PropertyNameConverter; use Symfony\Component\Serializer\Normalizer\BackedEnumNormalizer; use Symfony\Component\Serializer\Normalizer\ObjectNormalizer; use Symfony\Component\Serializer\Serializer; use Symfony\Component\Validator\Mapping\Loader\AnnotationLoader; use Symfony\Component\Validator\Validation; $validator = Validation::createValidatorBuilder() ->addLoader(new AnnotationLoader()) ->getValidator() ; $serializer = new Serializer([ new BackedEnumNormalizer(), new ObjectNormalizer(null, new PropertyNameConverter()), ]); $clientFactory = new ClientFactory($serializer); $loader = new Loader($validator, $serializer); $loader->loadFromDirectory('/path/to/YourFixtures'); $groups = ['group1']; // loading fixtures belong to the selected group only $executor = new Executor($clientFactory->createAsyncAwsClient()); $executor->execute($loader->getFixtures($groups), $loader->getTables($groups));
Important! Each executor class comes with a purger class which executed before, drop tables and truncate data.
Load fixtures via console command
bin/console dynamite:fixtures:load --path path/to/fixtures
Debug logger
Execution process debug logs can be enabled by passing PSR-3 logger into executor:
<?php declare(strict_types=1); use Dynamite\Executor; // PSR-3 compatible implementation of Psr\Log\LoggerInterface $logger = new Logger(); $executor = new Executor($client, logger: $logger);