Add support for queries in List attributes endpoint
This commit is contained in:
parent
3d62652e1e
commit
ead77d6cad
3 changed files with 84 additions and 7 deletions
|
@ -8,6 +8,7 @@ use Appwrite\Event\Event;
|
|||
use Appwrite\Extend\Exception;
|
||||
use Appwrite\Network\Validator\Email;
|
||||
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\Response;
|
||||
|
@ -1657,9 +1658,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(', ', Collections::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,12 +1675,48 @@ App::get('/v1/databases/:databaseId/collections/:collectionId/attributes')
|
|||
throw new Exception(Exception::COLLECTION_NOT_FOUND);
|
||||
}
|
||||
|
||||
$attributes = $collection->getAttribute('attributes');
|
||||
$queries = Query::parseQueries($queries);
|
||||
\array_push($queries, Query::equal('collectionId', [$collectionId]), Query::equal('databaseId', [$databaseId]));
|
||||
|
||||
$response->dynamic(new Document([
|
||||
'total' => \count($attributes),
|
||||
'attributes' => $attributes
|
||||
]), Response::MODEL_ATTRIBUTE_LIST);
|
||||
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]);
|
||||
$cursor = reset($cursor);
|
||||
|
||||
if ($cursor) {
|
||||
$attributeId = $cursor->getValue();
|
||||
|
||||
$cursorDocument = Authorization::skip(fn() => $dbForProject->getDocument('attributes', $database->getInternalId() . '_' . $collection->getInternalId() . '_' . $attributeId));
|
||||
|
||||
if ($cursorDocument->isEmpty()) {
|
||||
throw new Exception(Exception::GENERAL_CURSOR_NOT_FOUND, "Attribute '{$attributeId}' for the 'cursor' value not found.");
|
||||
}
|
||||
|
||||
$cursor->setValue($cursorDocument);
|
||||
}
|
||||
|
||||
$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' => $dbForProject->count('attributes', $filterQueries, APP_LIMIT_COUNT),
|
||||
'attributes' => $attributes,
|
||||
]), Response::MODEL_ATTRIBUTE_LIST);
|
||||
});
|
||||
|
||||
App::get('/v1/databases/:databaseId/collections/:collectionId/attributes/:key')
|
||||
|
|
|
@ -0,0 +1,23 @@
|
|||
<?php
|
||||
|
||||
namespace Appwrite\Utopia\Database\Validator\Queries;
|
||||
|
||||
class Attributes extends Base
|
||||
{
|
||||
public const ALLOWED_ATTRIBUTES = [
|
||||
'key',
|
||||
'type',
|
||||
'size',
|
||||
'required',
|
||||
'array'
|
||||
];
|
||||
|
||||
/**
|
||||
* Expression constructor
|
||||
*
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct('attributes', self::ALLOWED_ATTRIBUTES);
|
||||
}
|
||||
}
|
|
@ -316,6 +316,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
|
||||
*/
|
||||
|
@ -698,7 +715,6 @@ trait DatabasesBase
|
|||
$this->assertEquals(10, $attributes['body']['total']);
|
||||
|
||||
$attributes = $attributes['body']['attributes'];
|
||||
|
||||
$this->assertIsArray($attributes);
|
||||
$this->assertCount(10, $attributes);
|
||||
|
||||
|
|
Loading…
Reference in a new issue