Download the PHP package cpliakas/dynamo-db-odm without Composer
On this page you can find all versions of the php package cpliakas/dynamo-db-odm. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download cpliakas/dynamo-db-odm
More information about cpliakas/dynamo-db-odm
Files in cpliakas/dynamo-db-odm
Package dynamo-db-odm
Short Description A lightweight ODM for DynamoDB
License MIT
Homepage https://github.com/cpliakas/dynamo-db-odm
Informations about the package dynamo-db-odm
DynamoDB ODM
A lightweight, no-frills ODM (Object Document Mapper) for DynamoDB.
NOTE: This library only works with the 2.x version of the AWS SDK for PHP. The changes in the 3.x version of the SDK make it much easier to work with, and this library provides less value than before (which is a good thing, less code!). It is unclear as to whether or not a port to the 3.x SDK will be useful.
Why?
Amazon provides an SDK that connects to DynamoDB. Why would you want to use an ODM on top of it?
- Allows developers to define their data model in the codebase
- Facilitates readable code by wrapping complex data structures with an OO API
- Adds logical extension points via Symfony's EventDispatcher component
- Optionally enforces entity integrity
- Facilitates password hashing, data encryption, random string generation, etc.
Installation
DynamoDB ODM can be installed with Composer by adding it as a dependency to your project's composer.json file.
Please refer to Composer's documentation for more detailed installation and usage instructions.
Usage
Defining Entities
Entities are classes that extend Cpliakas\DynamoDb\ODM\Entity
and model
different types of documents. Metadata, such as the table name and hash /
range key attributes, are defined in static properties and accessed through the
static methods defined in Cpliakas\DynamoDb\ODM\EntityInterface
.
NOTE: Other ODMs use annotations to define metadata. This pattern can improve DX for applications with a large number of entities and improve performance when proper caching is implemented. However, this library intentionally chooses to use statics to define metadata since it is a lighter-weight solution for the applications this project is intended to be used in.
Initializing The Document Manager
The document manager is responsible for instantiating entity classes and reading / writing documents to DynamoDB.
CRUD Operations
Create a document.
Read, update, and delete the document.
NOTE: Other ODMs use the unit of work pattern when persisting data to the backend. Due to the nature of DynamoDB and the desire to keep this library lightweight, we opted not to use this pattern.
Composite Primary Keys
Pass an array as the primary key parameter when an entity's table uses a hash and range primary key type.
Query and Scan Commands
You can either pass the raw data structure defined by the AWS SDK for PHP as the second parameter or use the object oriented wrapper to build the search conditions. The example below uses the OO wrapper.
Attribute Transformers
Transformers convert attribute values set through the entity object to something else.
The following example builds upon the book entity above to transform \DateTime
objects set as the created
attribute to Unix timestamps.
Set a \DateTime
object as the created
attribute and create the document.
The value is stored as a Unix timestamp in DynamoDB.
Attribute Renderers
Renderers convert the value stored in DynamoDB to something that is normalized or native to PHP when it is accessed.
The following example is the opposite of the use case above. It converts the
Unix timestamp stored in DynamoDB to a \DateTime
object.
Add the following statement to the Book
object's constructor like we did with
the transformer.
Read the document from DynamoDB. Accessing the created
attribute will return
a \DateTime
object.