1
0
Fork 0
mirror of synced 2024-10-01 17:58:02 +13:00

add support of queries in list indexes

This commit is contained in:
Prateek Banga 2023-07-21 16:40:44 +05:30 committed by prateek banga
parent 165268cb71
commit a6abc7c4e8
3 changed files with 69 additions and 16 deletions

View file

@ -11,6 +11,7 @@ use Appwrite\Utopia\Database\Validator\CustomId;
use Appwrite\Utopia\Database\Validator\Queries\Attributes;
use Appwrite\Utopia\Database\Validator\Queries\Collections;
use Appwrite\Utopia\Database\Validator\Queries\Databases;
use Appwrite\Utopia\Database\Validator\Queries\Indexes;
use Appwrite\Utopia\Response;
use MaxMind\Db\Reader;
use Utopia\App;
@ -1664,11 +1665,13 @@ App::get('/v1/databases/:databaseId/collections/:collectionId/attributes')
->action(function (string $databaseId, string $collectionId, array $queries, Response $response, Database $dbForProject) {
$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]));
if (!empty($search)) {
$queries[] = Query::search('search', $search);
}
// Get cursor document if there was a cursor query
$cursor = Query::getByType($queries, [Query::TYPE_CURSORAFTER, Query::TYPE_CURSORBEFORE]);
@ -2514,26 +2517,38 @@ 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);
$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.");
}
}
$collection = $dbForProject->getDocument('database_' . $database->getInternalId(), $collectionId);
\array_push($queries, Query::equal('collectionId', [$collectionId]), Query::equal('databaseId', [$databaseId]));
if ($collection->isEmpty()) {
throw new Exception(Exception::COLLECTION_NOT_FOUND);
// 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]);
}
$indexes = $collection->getAttribute('indexes');
$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);
});

View file

@ -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);
}
}

View file

@ -1064,6 +1064,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
*/