matthieuwerner/dynamodb-storable

该组件基于Symfony serializer和async-aws,是一个人类可读且快速的抽象,可以轻松地将序列化对象存储在DynamoDB中。

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

This package is auto-updated.

Last update: 2024-09-19 21:29:18 UTC


README

CI status

DynamoDB Storable

此组件基于Symfony serializer和async-aws,是一个人类可读且快速的抽象,可以轻松地将序列化对象存储在DynamoDB中 🚀。

此存储使用现有的DynamoDB表,并创建具有以下结构的条目:{"key", "namespace", "value", "class", "date""}

安装

composer require matthieuwerner/dynamodb-storable 

使用

添加服务

选项 1:自动绑定(Symfony/Laravel)

use Storable\Storage;

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

选项 2:实例化类

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');
    // ...
}

写入

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

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

/!\ 与对象一起工作时,需要它们实现 "StorableInterface" 接口

读取

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

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

删除

$storage->remove('key');

处理命名空间

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

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

更改默认值

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