matthieuwerner/dynamodb-storable

This component, based on the Symfony serializer and async-aws, is a human-readable andquick abstraction to easily store serialized objects in DynamoDB.

v0.1.0 2022-06-19 10:16 UTC

This package is auto-updated.

Last update: 2022-11-19 17:51:52 UTC


README

CI status

DynamoDB Storable

This component, based on the Symfony serializer and async-aws, is a human-readable and quick abstraction to easily store serialized objects in DynamoDB 🚀.

This storage use an existing DynamoDB table and create entries with the following structure : {"key", "namespace", "value", "class", "date""}.

Installation

composer require matthieuwerner/dynamodb-storable 

Usage

Adding service

Option 1: auto wiring (Symfony/Laravel)

use Storable\Storage;

protected function anyAction(Storage $storage): string
{
    $storage->set('key', 'value');
    // ...
}

Option 2: instantiate class

use Storable\Storage;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Serializer;

protected function anyAction(): string
{
    $encoders = [new JsonEncoder()];
    $normalizers = [new ObjectNormalizer()];
    $storage = new Storage($dynamoDbClientMock, new Serializer($normalizers, $encoders));

    $storage->set('key', 'value');
    // ...
}

Write

// String 
$storage->set('key', 'value');

// Object 
$myObject  = new MyObject();
$storage->setObject($myObject);

/!\ Working with objects need that they implement "StorableInterface"

Read

// String 
$storage->get('key'); // Return a string

// Object 
$storage->get($objectId); // Return an object

Remove

$storage->remove('key');

Working with namespaces

// Get all objects in a namespace
$storage->getByNamespace('namespace');

// Remove all objects in a namespace
$storage->removeNamespace('namespace');

Changing default values

// Change the default table name (default is "storage")
$storage->setTable('newTableName');

// Change the default namespace (default is "main")
$storage->setNamespace('newNamespaceName');

// Change the default attribute "key" in the table structure
$storage->setAttributeKey('newKeyAttribute');

// Change the default attribute "namespace" in the table structure
$storage->setAttributeNamespace('newNamespaceAttribute');

// Change the default attribute "value" in the table structure
$storage->setAttributeValue('newValueAttribute');

// Change the default attribute "class" in the table structure
$storage->setAttributeClass('newClassAttribute');

// Change the default attribute "date" in the table structure
$storage->setAttributeDate('newDateAttribute');