Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/misantron/dynamite

AWS DynamoDB data fixtures
https://github.com/misantron/dynamite

data-fixtures dynamodb hacktoberfest php

Last synced: 30 days ago
JSON representation

AWS DynamoDB data fixtures

Awesome Lists containing this project

README

        

# Dynamite - AWS DynamoDB fixtures

[![Build Status](https://img.shields.io/github/actions/workflow/status/misantron/dynamite/build.yml?style=flat-square)](https://github.com/misantron/dynamite/actions)
[![Code Coverage](https://img.shields.io/codecov/c/gh/misantron/dynamite.svg?style=flat-square)](https://app.codecov.io/gh/misantron/dynamite)
[![Packagist](https://img.shields.io/packagist/v/misantron/dynamite.svg?style=flat-square)](https://packagist.org/packages/misantron/dynamite)

Provide a simple way to manage and execute the loading of data fixtures for AWS DynamoDB storage.
Can use client from [AWS PHP SDK](https://aws.amazon.com/sdk-for-php/) or [Async AWS](https://async-aws.com/) under the hood.
Library code design is heavily inspired by [doctrine/data-fixtures](https://github.com/doctrine/data-fixtures).

## Install

The preferred way to install is through [Composer](https://getcomposer.org).
Run this command to install the latest stable version:

```shell
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
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
setTableName('Users')
->addRecords([
new Record([
Value::stringValue('Id', 'e5502ec2-42a7-408b-9f03-f8e162b6257e'),
Value::stringValue('Email', '[email protected]'),
Value::boolValue('Active', true),
]),
new Record([
Value::stringValue('Id', 'f0cf458c-4fc0-4dd8-ba5b-eca6dba9be63'),
Value::stringValue('Email', '[email protected]'),
Value::boolValue('Active', true),
]),
])
;
}
}
```

### Tables and fixtures loading

It's possible to provide fixtures loading path:

```php
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
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
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

```shell
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