|
| 1 | +# Basic Usage Guide |
| 2 | + |
| 3 | +**DynamoPHP** is a lightweight, attribute-based _Object Data Mapper_ designed to simplify development with Amazon |
| 4 | +DynamoDB. Inspired by modern data-mapping libraries, it brings a familiar, expressive API to PHP for modeling DynamoDB |
| 5 | +entities. |
| 6 | + |
| 7 | +## Contents |
| 8 | + |
| 9 | +- [Installation](#installation) |
| 10 | +- [Concepts](#concepts) |
| 11 | +- [Getting Started](#getting-started) |
| 12 | + |
| 13 | +## Installation |
| 14 | + |
| 15 | +Install **DynamoPHP** via Composer: |
| 16 | + |
| 17 | +```bash |
| 18 | +composer require edumarques/dynamophp |
| 19 | +``` |
| 20 | + |
| 21 | +## Concepts |
| 22 | + |
| 23 | +### Entity |
| 24 | + |
| 25 | +An Entity in **DynamoPHP** is a PHP class that maps to a single item (also known as document or row) in a DynamoDB |
| 26 | +table. Entities are defined using PHP attributes. |
| 27 | + |
| 28 | +### Attribute |
| 29 | + |
| 30 | +An Attribute is an item's property (also known as field or column) that should be persisted in a DynamoDB table. |
| 31 | +Attributes are defined using PHP attributes. |
| 32 | + |
| 33 | +### PartitionKey and SortKey |
| 34 | + |
| 35 | +Compose the Primary Key of items, and reference how items are stored in and retrieved from the table. They are defined |
| 36 | +by fields in Entity's PHP attribute. |
| 37 | + |
| 38 | +### Entity Manager |
| 39 | + |
| 40 | +The Entity Manager is the main interface for performing operations like saving, fetching, querying, and deleting items |
| 41 | +from DynamoDB. |
| 42 | + |
| 43 | +## Getting Started |
| 44 | + |
| 45 | +To get started with **DynamoPHP**, you basically need to: |
| 46 | + |
| 47 | +1. Define your entity model using attributes. |
| 48 | +2. Create an instance of the EntityManager. |
| 49 | +3. Use the entity manager to read/write data. |
| 50 | + |
| 51 | +Here is an example of a simple entity: |
| 52 | + |
| 53 | +```php |
| 54 | +use EduardoMarques\DynamoPHP\Attribute\Attribute; |
| 55 | +use EduardoMarques\DynamoPHP\Attribute\Entity; |
| 56 | +use EduardoMarques\DynamoPHP\Attribute\PartitionKey; |
| 57 | +use EduardoMarques\DynamoPHP\Attribute\SortKey; |
| 58 | + |
| 59 | +#[Entity( |
| 60 | + table: 'users', |
| 61 | + partitionKey: new PartitionKey(['id']), |
| 62 | + sortKey: new SortKey(['createdAt']) |
| 63 | +)] |
| 64 | +class User |
| 65 | +{ |
| 66 | + #[Attribute] |
| 67 | + public string $id; |
| 68 | + |
| 69 | + #[Attribute(name: 'name')] |
| 70 | + public string $fullName; |
| 71 | + |
| 72 | + #[Attribute] |
| 73 | + public DateTimeInterface $createdAt; |
| 74 | +} |
| 75 | +``` |
| 76 | + |
| 77 | +Each entity must: |
| 78 | + |
| 79 | +- Declare a table name. |
| 80 | +- Define a partition key and, if the table schema requires, a sort key, via PartitionKey and SortKey objects in their |
| 81 | + respective fields. |
| 82 | +- Optionally, set persistent properties with #[Attribute]. |
| 83 | + |
| 84 | +Create an instance of EntityManager using the factory: |
| 85 | + |
| 86 | +```php |
| 87 | +use EduardoMarques\DynamoPHP\ODM\EntityManagerFactory; |
| 88 | + |
| 89 | +$entityManager = EntityManagerFactory::create([ |
| 90 | + 'region' => 'eu-central-1', |
| 91 | + 'endpoint' => 'http://localhost:4566', |
| 92 | + 'credentials' => ['key' => 'key', 'secret' => 'secret'], |
| 93 | +]); |
| 94 | +``` |
| 95 | + |
| 96 | +This internally creates a DynamoDB client using the AWS SDK and wires up all dependencies. |
| 97 | + |
| 98 | +Run the operations you need, for instance: |
| 99 | + |
| 100 | +```php |
| 101 | +// Create/update entity |
| 102 | +$user = new User(); |
| 103 | +$user->id = 'user-123'; |
| 104 | +$user->fullName = 'John Doe'; |
| 105 | +$user->createdAt = new DateTime(); |
| 106 | + |
| 107 | +$entityManager->put($user); |
| 108 | + |
| 109 | +// Fetch entity by primary key |
| 110 | +$entity = $entityManager->get(User::class, [ |
| 111 | + 'id' => 'user-123', |
| 112 | + 'createdAt' => new DateTime(), |
| 113 | +]); |
| 114 | +``` |
| 115 | + |
| 116 | +That's it! The Entity Manager will handle all the wiring of dependencies and operations, including serialization and |
| 117 | +communication with the DynamoDB client. |
0 commit comments