Compare commits

...
4 changed files with 162 additions and 9 deletions
+83 -8
View File
@@ -48,6 +48,8 @@ use Appwrite\Utopia\Database\Validator\Queries\Collections;
use Appwrite\Utopia\Database\Validator\Queries\Databases;
use Appwrite\Utopia\Database\Validator\Queries\Document as DocumentValidator;
use Appwrite\Utopia\Database\Validator\Queries\Documents;
use Appwrite\Utopia\Database\Validator\Queries\Attributes;
use Appwrite\Utopia\Database\Validator\Queries\Indexes;
use Utopia\Config\Config;
use MaxMind\Db\Reader;
use Utopia\Validator\Nullable;
@@ -1657,9 +1659,10 @@ App::get('/v1/databases/:databaseId/collections/:collectionId/attributes')
->label('sdk.response.model', Response::MODEL_ATTRIBUTE_LIST)
->param('databaseId', '', new UID(), 'Database ID.')
->param('collectionId', '', new UID(), 'Collection ID. You can create a new collection using the Database service [server integration](https://appwrite.io/docs/server/databases#databasesCreateCollection).')
->param('queries', [], new Attributes(), 'Array of query strings generated using the Query class provided by the SDK. [Learn more about queries](https://appwrite.io/docs/queries). Maximum of ' . APP_LIMIT_ARRAY_PARAMS_SIZE . ' queries are allowed, each ' . APP_LIMIT_ARRAY_ELEMENT_SIZE . ' characters long. You may filter on the following attributes: ' . implode(', ', Attributes::ALLOWED_ATTRIBUTES), true)
->inject('response')
->inject('dbForProject')
->action(function (string $databaseId, string $collectionId, Response $response, Database $dbForProject) {
->action(function (string $databaseId, string $collectionId, array $queries, Response $response, Database $dbForProject) {
$database = Authorization::skip(fn() => $dbForProject->getDocument('databases', $databaseId));
@@ -1673,11 +1676,54 @@ App::get('/v1/databases/:databaseId/collections/:collectionId/attributes')
throw new Exception(Exception::COLLECTION_NOT_FOUND);
}
$attributes = $collection->getAttribute('attributes');
$queries = Query::parseQueries($queries);
foreach ($queries as $query) {
if ($query->getMethod() === Query::TYPE_SELECT) {
throw new Exception(Exception::GENERAL_QUERY_INVALID, 'Select queries are not valid.');
}
}
\array_push($queries, Query::equal('collectionId', [$collectionId]), Query::equal('databaseId', [$databaseId]));
// Get cursor document if there was a cursor query
$cursor = Query::getByType($queries, Query::TYPE_CURSORAFTER, Query::TYPE_CURSORBEFORE);
$cursor = reset($cursor);
if ($cursor) {
$attributeId = $cursor->getValue();
$cursorDocument = Authorization::skip(fn() => $dbForProject->find('attributes', [
Query::equal('collectionId', [$collectionId]),
Query::equal('databaseId', [$databaseId]),
Query::equal('key', [$attributeId]),
Query::limit(1),
]));
if (empty($cursorDocument) || $cursorDocument[0]->isEmpty()) {
throw new Exception(Exception::GENERAL_CURSOR_NOT_FOUND, "Attribute '{$attributeId}' for the 'cursor' value not found.");
}
$cursor->setValue($cursorDocument[0]);
}
$attributes = $dbForProject->find('attributes', $queries);
//Add relationship data from options to attributes as it loses options during response setup
foreach ($attributes as $attribute) {
if ($attribute->getAttribute('type') === Database::VAR_RELATIONSHIP) {
$options = $attribute->getAttribute('options');
$attribute->setAttribute('relatedCollection', $options['relatedCollection']);
$attribute->setAttribute('relationType', $options['relationType']);
$attribute->setAttribute('twoWay', $options['twoWay']);
$attribute->setAttribute('twoWayKey', $options['twoWayKey']);
$attribute->setAttribute('side', $options['side']);
$attribute->setAttribute('onDelete', $options['onDelete']);
}
}
$filterQueries = Query::groupByType($queries)['filters'];
$response->dynamic(new Document([
'total' => \count($attributes),
'attributes' => $attributes
'total' => $dbForProject->count('attributes', $filterQueries, APP_LIMIT_COUNT),
'attributes' => $attributes,
]), Response::MODEL_ATTRIBUTE_LIST);
});
@@ -2480,26 +2526,55 @@ App::get('/v1/databases/:databaseId/collections/:collectionId/indexes')
->label('sdk.response.model', Response::MODEL_INDEX_LIST)
->param('databaseId', '', new UID(), 'Database ID.')
->param('collectionId', '', new UID(), 'Collection ID. You can create a new collection using the Database service [server integration](https://appwrite.io/docs/server/databases#databasesCreateCollection).')
->param('queries', [], new Indexes(), 'Array of query strings generated using the Query class provided by the SDK. [Learn more about queries](https://appwrite.io/docs/queries). Maximum of ' . APP_LIMIT_ARRAY_PARAMS_SIZE . ' queries are allowed, each ' . APP_LIMIT_ARRAY_ELEMENT_SIZE . ' characters long. You may filter on the following attributes: ' . implode(', ', Indexes::ALLOWED_ATTRIBUTES), true)
->inject('response')
->inject('dbForProject')
->action(function (string $databaseId, string $collectionId, Response $response, Database $dbForProject) {
->action(function (string $databaseId, string $collectionId, array $queries, Response $response, Database $dbForProject) {
$database = Authorization::skip(fn() => $dbForProject->getDocument('databases', $databaseId));
if ($database->isEmpty()) {
throw new Exception(Exception::DATABASE_NOT_FOUND);
}
$collection = $dbForProject->getDocument('database_' . $database->getInternalId(), $collectionId);
if ($collection->isEmpty()) {
throw new Exception(Exception::COLLECTION_NOT_FOUND);
}
$indexes = $collection->getAttribute('indexes');
$queries = Query::parseQueries($queries);
foreach ($queries as $query) {
if ($query->getMethod() === Query::TYPE_SEARCH) {
throw new Exception(Exception::GENERAL_QUERY_INVALID, 'Select queries are not valid.');
}
}
\array_push($queries, Query::equal('collectionId', [$collectionId]), Query::equal('databaseId', [$databaseId]));
// Get cursor document if there was a cursor query
$cursor = Query::getByType($queries, Query::TYPE_CURSORAFTER, Query::TYPE_CURSORBEFORE);
$cursor = reset($cursor);
if ($cursor) {
$indexId = $cursor->getValue();
$cursorDocument = Authorization::skip(fn() => $dbForProject->find('indexes', [
Query::equal('collectionId', [$collectionId]),
Query::equal('databaseId', [$databaseId]),
Query::equal('key', [$indexId]),
Query::limit(1)
]));
if (empty($cursorDocument) || $cursorDocument[0]->isEmpty()) {
throw new Exception(Exception::GENERAL_CURSOR_NOT_FOUND, "Index '{$indexId}' for the 'cursor' value not found.");
}
$cursor->setValue($cursorDocument[0]);
}
$filterQueries = Query::groupByType($queries)['filters'];
$response->dynamic(new Document([
'total' => \count($indexes),
'indexes' => $indexes,
'total' => $dbForProject->count('indexes', $filterQueries, APP_LIMIT_COUNT),
'indexes' => $dbForProject->find('indexes', $queries),
]), Response::MODEL_INDEX_LIST);
});
@@ -0,0 +1,24 @@
<?php
namespace Appwrite\Utopia\Database\Validator\Queries;
class Attributes extends Base
{
public const ALLOWED_ATTRIBUTES = [
'key',
'type',
'size',
'required',
'array',
'status'
];
/**
* Expression constructor
*
*/
public function __construct()
{
parent::__construct('attributes', self::ALLOWED_ATTRIBUTES);
}
}
@@ -0,0 +1,21 @@
<?php
namespace Appwrite\Utopia\Database\Validator\Queries;
class Indexes extends Base
{
public const ALLOWED_ATTRIBUTES = [
'key',
'type',
'status',
];
/**
* Expression constructor
*
*/
public function __construct()
{
parent::__construct('attributes', self::ALLOWED_ATTRIBUTES);
}
}
+34 -1
View File
@@ -297,6 +297,23 @@ trait DatabasesBase
return $data;
}
/**
* @depends testCreateAttributes
*/
public function testListAttributes(array $data): void
{
$databaseId = $data['databaseId'];
$attributes = $this->client->call(Client::METHOD_GET, '/databases/' . $databaseId . '/collections/' . $data['moviesId'] . '/attributes', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
]), [
'queries' => ['equal("type", "string")'],
]);
$this->assertEquals(200, $attributes['headers']['status-code']);
$this->assertEquals(3, $attributes['body']['total']);
}
/**
* @depends testCreateAttributes
*/
@@ -679,7 +696,6 @@ trait DatabasesBase
$this->assertEquals(10, $attributes['body']['total']);
$attributes = $attributes['body']['attributes'];
$this->assertIsArray($attributes);
$this->assertCount(10, $attributes);
@@ -976,6 +992,23 @@ trait DatabasesBase
return $data;
}
/**
* @depends testCreateIndexes
*/
public function testListIndexes(array $data): void
{
$databaseId = $data['databaseId'];
$indexes = $this->client->call(Client::METHOD_GET, '/databases/' . $databaseId . '/collections/' . $data['moviesId'] . '/indexes', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
]), [
'queries' => ['equal("type", "key")'],
]);
$this->assertEquals(200, $indexes['headers']['status-code']);
$this->assertEquals(3, $indexes['body']['total']);
}
/**
* @depends testCreateIndexes
*/