misantron/dynamite

AWS DynamoDB 数据固定工具加载器

v2.2.3 2024-03-24 06:10 UTC

README

Build Status Code Coverage Packagist

提供了一种简单的方法来管理和执行AWS DynamoDB存储的数据固定加载。
底层可以使用AWS PHP SDKAsync AWS
库代码设计深受doctrine/data-fixtures的启发。

安装

推荐通过Composer安装。运行以下命令安装最新稳定版本:

composer require --dev misantron/dynamite

加载固定工具

创建表创建类

此功能是可选的。
固定工具类必须实现Dynamite\TableInterface接口以便在加载器中可见。

<?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)
        ;
    }
}

创建固定工具加载类

固定工具类必须实现Dynamite\FixtureInterface接口以便在加载器中可见。

<?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),
                ]),
            ])
        ;
    }
}

表格和固定工具加载

可以提供固定工具加载路径

<?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');

或手动加载每个固定工具或表格类

<?php

declare(strict_types=1);

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

创建表格并执行固定工具

要创建数据库模式并在存储中加载固定工具,您应该执行以下操作:

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

注意!每个执行器类都附带一个清理器类,它在执行前会先删除表格和截断数据。

通过控制台命令加载固定工具

bin/console dynamite:fixtures:load --path path/to/fixtures

调试记录器

可以通过将PSR-3记录器传递给执行器来启用执行过程调试日志。

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