Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Adds DynamoDB Adapter #333

Draft
wants to merge 20 commits into
base: main
Choose a base branch
from
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add create and exists function
  • Loading branch information
ajwad-shaikh committed Oct 2, 2023
commit 6469588d85b343b41c374b7e0ea7380eebab3152
199 changes: 198 additions & 1 deletion src/Database/Adapter/DynamoDB.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,33 @@
use Utopia\Database\Database;
use Utopia\Database\Query;
use Utopia\Database\Exception as DatabaseException;
use Aws\DynamoDb\Exception\DynamoDbException;
use Aws\DynamoDb\DynamoDbClient as Client;

class DynamoDB extends Adapter
{
// Data types
public const VAR_STRING = 'S';
public const VAR_NUMBER = 'N';
public const VAR_BINARY = 'B';

// Index types
public const SIMPLE_INDEX = 'SIMPLE';
public const COMPOSITE_INDEX = 'COMPOSITE';

protected Client $client;

/**
* @return Client
*
* @throws Exception
*/
protected function getClient(): Client
{
return $this->client;
}



public function __construct(Client $client)
{
Expand All @@ -23,7 +43,8 @@ public function __construct(Client $client)

/**
* Ping Database
*
*
* DynamoDB is a managed database - If the DynamoDB client is successfully initialized, you can assume the service is accessible.
* @return bool
*/
public function ping(): bool
Expand All @@ -36,6 +57,7 @@ public function ping(): bool
*
* @param string $name
*
* No concept of Database schemas in DynamoDb.
* @return bool
*/
public function create(string $name): bool
Expand All @@ -54,12 +76,23 @@ public function create(string $name): bool
*/
public function exists(string $database, ?string $collection): bool
{
if (!\is_null($collection)) {
$collection = $this->filter($collection);
try {
$this->client->describeTable([
'TableName' => "{$this->getNamespace()}_{$collection}",
]);
} catch (DynamoDbException $e) {
return false;
}
}
return true;
}

/**
* List Databases
*
* No concept of Database schemas in DynamoDb.
* @return array<Document>
*/
public function list(): array
Expand All @@ -72,13 +105,33 @@ public function list(): array
*
* @param string $name
*
* No concept of Database schemas in DynamoDb.
* @return bool
*/
public function delete(string $name): bool
{
return true;
}

/**
* Get DynamoDb Type
*
* @param string $type
* @param int $size in chars
*
* @return string
*/
protected function getDynamoDbType(string $type): string
{
if (in_array($type, array(Database::VAR_STRING, Database::VAR_RELATIONSHIP))) {
return DynamoDB::VAR_STRING;
} else if (in_array($type, array(Database::VAR_INTEGER, Database::VAR_FLOAT, Database::VAR_BOOLEAN, Database::VAR_DATETIME))) {
return DynamoDB::VAR_NUMBER;
} else {
throw new DatabaseException('Unknown Type: ' . $type);
}
}

/**
* Create Collection
*
Expand All @@ -89,6 +142,150 @@ public function delete(string $name): bool
*/
public function createCollection(string $name, array $attributes = [], array $indexes = []): bool
{
$tableName = "{$this->getNamespace()}_{$this->filter($name)}";

$attributeDefinitions = [
[
'AttributeName' => '_id',
'AttributeType' => DynamoDB::VAR_NUMBER,
],
[
'AttributeName' => '_uid',
'AttributeType' => DynamoDB::VAR_STRING,
],
[
'AttributeName' => '_createdAt',
'AttributeType' => DynamoDB::VAR_NUMBER,
],
[
'AttributeName' => '_updatedAt',
'AttributeType' => DynamoDB::VAR_NUMBER,
],
[
'AttributeName' => '_permissions',
'AttributeType' => DynamoDB::VAR_STRING,
]
];

$permsAttributeDefinitions = [
[
'AttributeName' => '_id',
'AttributeType' => DynamoDB::VAR_NUMBER,
],
[
'AttributeName' => '_type',
'AttributeType' => DynamoDB::VAR_STRING,
],
[
'AttributeName' => '_permission',
'AttributeType' => DynamoDB::VAR_STRING,
],
[
'AttributeName' => '_document',
'AttributeType' => DynamoDB::VAR_STRING,
]
];

$globalIndexes = [
[
'IndexName' => '_uid',
'KeySchema' => [
[
'AttributeName' => '_uid',
'KeyType' => 'HASH',
],
],
'Projection' => [
'ProjectionType' => 'ALL',
],
],
];

$permsGlobalIndexes = [
[
'IndexName' => "index_{$tableName}_ukey",
'KeySchema' => [
[
'AttributeName' => '_document',
'KeyType' => 'HASH',
],
[
'AttributeName' => '_permission',
'KeyType' => 'RANGE'
]
],
'Projection' => [
'ProjectionType' => 'ALL',
],
],
];

foreach ($attributes as $attribute) {
$attrId = $this->filter($attribute->getId());
$attrType = $this->getDynamoDbType($attribute->getAttribute('type'));

$attributeDef = [
'AttributeName' => $attrId,
'AttributeType' => $attrType,
];

array_push($attributeDefinitions, $attributeDef);
}

foreach ($indexes as $index) {
$indexId = $this->filter($index->getId());
$indexType = $index->getAttribute('type');
$indexAttributes = $index->getAttribute('attributes');

$globalIndex = [
'IndexName' => $indexId,
'Projection' => [
'ProjectionType' => 'ALL',
],
];

$globalIndex['KeySchema'] = [
[
'AttributeName' => $indexAttributes[0],
'KeyType' => 'HASH',
]
];
if ($indexType == DynamoDB::COMPOSITE_INDEX) {
array_push($globalIndex['KeySchema'], [
'AttributeName' => $indexAttributes[1],
'KeyType' => 'RANGE',
]);
}
array_push($globalIndexes, $globalIndex);
}

$params = [
'TableName' => $tableName,
'AttributeDefinitions' => $attributeDefinitions,
'KeySchema' => [
[
'AttributeName' => '_id',
'KeyType' => 'HASH',
],
],
'GlobalSecondaryIndexes' => $globalIndexes,
];

$permsParams = [
'TableName' => "{$tableName}_perms",
'AttributeDefinitions' => $permsAttributeDefinitions,
'KeySchema' => [
[
'AttributeName' => '_id',
'KeyType' => 'HASH',
],
],
'GlobalSecondaryIndexes' => $permsGlobalIndexes,
];

$this->client->createTable($params);
$this->client->createTable($permsParams);

return true;
}

Expand Down