mirror of
https://github.com/appwrite/appwrite.git
synced 2026-05-26 13:51:13 +00:00
Add support for queries in List attributes endpoint
This commit is contained in:
@@ -48,6 +48,7 @@ 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 Utopia\Config\Config;
|
||||
use MaxMind\Db\Reader;
|
||||
use Utopia\Validator\Nullable;
|
||||
@@ -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')
|
||||
|
||||
Reference in New Issue
Block a user