This repository has been archived by the owner on Oct 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 5a4d485
Showing
15 changed files
with
739 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# .gitattributes | ||
tests/ export-ignore | ||
.gitattributes export-ignore | ||
.gitignore export-ignore | ||
.travis.yml export-ignore | ||
phpunit.xml.dist export-ignore | ||
|
||
# Auto detect text files and perform LF normalization | ||
* text=auto |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# Contributing | ||
|
||
First of all, **thank you** for contributing! | ||
|
||
Here are a few rules to follow in order to ease code reviews and merging: | ||
|
||
- follow [PSR-1](http://www.php-fig.org/psr/1/) and [PSR-2](http://www.php-fig.org/psr/2/) | ||
- run the test suite | ||
- write (or update) unit tests when applicable | ||
- write documentation for new features | ||
- use [commit messages that make sense](http://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html) | ||
|
||
One may ask you to [squash your commits](http://gitready.com/advanced/2009/02/10/squashing-commits-with-rebase.html) too. This is used to "clean" your pull request before merging it (we don't want commits such as `fix tests`, `fix 2`, `fix 3`, etc.). | ||
|
||
When creating your pull request on GitHub, please write a description which gives the context and/or explains why you are creating it. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
/vendor/ | ||
/composer.phar | ||
/composer.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
language: php | ||
|
||
notifications: | ||
email: | ||
on_success: never | ||
|
||
php: | ||
- 7.2 | ||
- 7.3 | ||
|
||
matrix: | ||
fast_finish: true | ||
include: | ||
- php: 7.2 | ||
env: dependencies=lowest | ||
|
||
cache: | ||
directories: | ||
- $HOME/.composer/cache | ||
|
||
before_script: | ||
- composer install -n | ||
- if [ "$dependencies" = "lowest" ]; then composer update --prefer-lowest --prefer-stable -n; fi; | ||
|
||
script: | ||
- vendor/bin/phpunit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2019 Matthieu Napoli | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
DynamoDB object mapper. Like Doctrine or Eloquent, but for DynamoDB. | ||
|
||
[](https://travis-ci.org/bref/dynamap) | ||
[](https://packagist.org/packages/bref/dynamap) | ||
[](https://packagist.org/packages/bref/dynamap) | ||
|
||
**This library is currently in an experimental status and is not meant to be used in production.** | ||
|
||
## Installation | ||
|
||
``` | ||
composer require bref/dynamap | ||
``` | ||
|
||
## Usage | ||
|
||
```php | ||
$dynamap = Dynamap::fromOptions([ | ||
'region' => 'us-east-1', | ||
], $mapping); | ||
|
||
$dynamap->save($myObject); | ||
$myObject = $dynamap->get('table', 'key'); | ||
$objects = $dynamap->getAll('table'); | ||
``` | ||
|
||
## Contributing | ||
|
||
To run tests locally: | ||
|
||
- start DynamoDB local with `docker-compose up` or `docker-compose start` | ||
- run `phpunit` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{ | ||
"name": "bref/dynamap", | ||
"description": "DynamoDB object mapper", | ||
"keywords": ["dynamodb", "mapper", "orm", "aws", "object", "bref"], | ||
"license": "MIT", | ||
"type": "library", | ||
"autoload": { | ||
"psr-4": { | ||
"Dynamap\\": "src/" | ||
} | ||
}, | ||
"autoload-dev": { | ||
"psr-4": { | ||
"Dynamap\\Test\\": "tests/" | ||
} | ||
}, | ||
"require": { | ||
"php": "^7.2", | ||
"aws/aws-sdk-php": "^3.91", | ||
"symfony/property-info": "^4.2" | ||
}, | ||
"require-dev": { | ||
"phpunit/phpunit": "^8.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
version: '3' | ||
services: | ||
dynamodb: | ||
image: amazon/dynamodb-local | ||
ports: | ||
- '8000:8000' | ||
# `-jar DynamoDBLocal.jar` is necessary for the `command` option to work | ||
# For the rest see https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBLocal.UsageNotes.html | ||
command: '-jar DynamoDBLocal.jar -inMemory' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<phpunit colors="true" | ||
bootstrap="./vendor/autoload.php"> | ||
|
||
<testsuites> | ||
<testsuite name="Test suite"> | ||
<directory>./tests/</directory> | ||
</testsuite> | ||
</testsuites> | ||
|
||
<filter> | ||
<whitelist processUncoveredFilesFromWhitelist="true"> | ||
<directory suffix=".php">src</directory> | ||
</whitelist> | ||
</filter> | ||
|
||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Dynamap; | ||
|
||
use Aws\DynamoDb\DynamoDbClient; | ||
|
||
class Dynamap | ||
{ | ||
/** @var DynamoDbClient */ | ||
private $dynamoDb; | ||
|
||
/** @var Mapping */ | ||
private $mappingObj; | ||
|
||
public function __construct(DynamoDbClient $dynamoDb, array $mapping) | ||
{ | ||
$this->dynamoDb = $dynamoDb; | ||
$this->mappingObj = new Mapping($mapping); | ||
} | ||
|
||
public static function fromOptions(array $options, array $mapping) | ||
{ | ||
$options['version'] = $options['version'] ?? 'latest'; | ||
|
||
return new static(new DynamoDbClient($options), $mapping); | ||
} | ||
|
||
public function getAll(string $table): array | ||
{ | ||
$items = $this->dynamoDb->scan([ | ||
'TableName' => $table, | ||
])['Items']; | ||
|
||
return $this->mapList($items, $table); | ||
} | ||
|
||
/** | ||
* Get item by primary key. | ||
*/ | ||
public function get(string $table, $key) | ||
{ | ||
$tableMapping = $this->mappingObj->getTableMapping($table); | ||
|
||
$item = $this->dynamoDb->getItem([ | ||
'TableName' => $table, | ||
'Key' => $this->buildKeyQuery($key, $tableMapping), | ||
])['Item']; | ||
|
||
return $this->map($item, $table); | ||
} | ||
|
||
public function save(object $object): void | ||
{ | ||
$reflectedObject = new \ReflectionObject($object); | ||
$tableMapping = $this->mappingObj->getTableFromClassName($reflectedObject->getName()); | ||
|
||
$item = []; | ||
foreach ($tableMapping->getKeyMapping() as $fieldMapping) { | ||
$property = $reflectedObject->getProperty($fieldMapping->name()); | ||
$property->setAccessible(true); | ||
$fieldValue = $property->getValue($object); | ||
if ($fieldValue === null) { | ||
throw new \Exception('The object cannot have a null key'); | ||
} | ||
$item[$fieldMapping->name()] = $fieldMapping->dynamoDbQueryValue($fieldValue); | ||
} | ||
|
||
foreach ($tableMapping->getFieldsMapping() as $fieldMapping) { | ||
$property = $reflectedObject->getProperty($fieldMapping->name()); | ||
$property->setAccessible(true); | ||
$fieldValue = $property->getValue($object); | ||
// If the value is null we skip sending it (we cannot explicitly send null) | ||
if ($fieldValue !== null) { | ||
$item[$fieldMapping->name()] = $fieldMapping->dynamoDbQueryValue($fieldValue); | ||
} | ||
} | ||
|
||
$this->dynamoDb->putItem([ | ||
'TableName' => $tableMapping->getTableName(), | ||
'Item' => $item, | ||
]); | ||
} | ||
|
||
public function partialUpdate(string $table, $itemKey, array $values): void | ||
{ | ||
$tableMapping = $this->mappingObj->getTableMapping($table); | ||
|
||
$key = $this->buildKeyQuery($itemKey, $tableMapping); | ||
|
||
$updateExpressionParts = []; | ||
$updateValues = []; | ||
foreach ($values as $fieldName => $value) { | ||
$fieldMapping = $tableMapping->getFieldMapping($fieldName); | ||
$updateExpressionParts[] = "$fieldName = :$fieldName"; | ||
|
||
$updateValues[':' . $fieldName] = $fieldMapping->dynamoDbQueryValue($value); | ||
} | ||
$updateExpression = 'set ' . implode(', ', $updateExpressionParts); | ||
|
||
$this->dynamoDb->updateItem([ | ||
'TableName' => $table, | ||
'Key' => $key, | ||
'UpdateExpression' => $updateExpression, | ||
'ExpressionAttributeValues' => $updateValues, | ||
]); | ||
} | ||
|
||
private function mapList(array $items, string $table): array | ||
{ | ||
return array_map(function (array $item) use ($table) { | ||
return $this->map($item, $table); | ||
}, $items); | ||
} | ||
|
||
private function map(array $item, string $table): object | ||
{ | ||
$tableMapping = $this->mappingObj->getTableMapping($table); | ||
|
||
$class = new \ReflectionClass($tableMapping->getClassName()); | ||
$object = $class->newInstanceWithoutConstructor(); | ||
|
||
foreach ($item as $fieldName => $fieldData) { | ||
if (!$tableMapping->hasFieldOrKey($fieldName)) { | ||
continue; | ||
} | ||
$fieldMapping = $tableMapping->getFieldMapping($fieldName); | ||
|
||
$property = $class->getProperty($fieldName); | ||
$property->setAccessible(true); | ||
$property->setValue($object, $fieldMapping->readFieldValue($item, $fieldName)); | ||
} | ||
|
||
return $object; | ||
} | ||
|
||
private function buildKeyQuery($key, TableMapping $tableMapping): array | ||
{ | ||
$keyQuery = []; | ||
if (! is_array($key)) { | ||
if ($tableMapping->isCompositeKey()) { | ||
throw new \Exception('The key is a composite key and only a single value was provided'); | ||
} | ||
foreach ($tableMapping->getKeyMapping() as $fieldMapping) { | ||
$keyQuery[$fieldMapping->name()] = $fieldMapping->dynamoDbQueryValue($key); | ||
} | ||
} else { | ||
foreach ($tableMapping->getKeyMapping() as $fieldMapping) { | ||
$fieldName = $fieldMapping->name(); | ||
if (! isset($key[$fieldName])) { | ||
throw new \Exception('The key is incomplete'); | ||
} | ||
$keyQuery[$fieldName] = $fieldMapping->dynamoDbQueryValue($key[$fieldName]); | ||
} | ||
} | ||
return $keyQuery; | ||
} | ||
} |
Oops, something went wrong.