diff --git a/app/controllers/api/databases.php b/app/controllers/api/databases.php index 353f207f37..2a1884e6b2 100644 --- a/app/controllers/api/databases.php +++ b/app/controllers/api/databases.php @@ -8,8 +8,10 @@ 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\Database\Validator\Queries\Indexes; use Appwrite\Utopia\Response; use MaxMind\Db\Reader; use Utopia\App; @@ -1618,9 +1620,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)); @@ -1634,11 +1637,33 @@ 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])); + + // 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]); + } + + $filterQueries = Query::groupByType($queries)['filters']; $response->dynamic(new Document([ - 'total' => \count($attributes), - 'attributes' => $attributes + 'total' => $dbForProject->count('attributes', $filterQueries, APP_LIMIT_COUNT), + 'attributes' => $dbForProject->find('attributes', $queries), ]), Response::MODEL_ATTRIBUTE_LIST); }); @@ -2421,26 +2446,50 @@ 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); + \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); }); diff --git a/app/controllers/shared/api.php b/app/controllers/shared/api.php index 1bca7d47dc..2bbac41811 100644 --- a/app/controllers/shared/api.php +++ b/app/controllers/shared/api.php @@ -256,10 +256,11 @@ App::init() $deletes->setProject($project); $database->setProject($project); + $calculateUsage = fn ($event, Document $document) => $databaseListener($event, $document, $project, $queueForUsage, $dbForProject); + $dbForProject - ->on(Database::EVENT_DOCUMENT_CREATE, fn ($event, $document) => $databaseListener($event, $document, $project, $queueForUsage, $dbForProject)) - ->on(Database::EVENT_DOCUMENT_DELETE, fn ($event, $document) => $databaseListener($event, $document, $project, $queueForUsage, $dbForProject)) - ; + ->on(Database::EVENT_DOCUMENT_CREATE, 'calculate-usage', $calculateUsage) + ->on(Database::EVENT_DOCUMENT_DELETE, 'calculate-usage', $calculateUsage); $useCache = $route->getLabel('cache', false); diff --git a/app/init.php b/app/init.php index 2e59869993..1b74b81bc4 100644 --- a/app/init.php +++ b/app/init.php @@ -947,7 +947,7 @@ App::setResource('user', function ($mode, $project, $console, $request, $respons if (APP_MODE_ADMIN !== $mode) { if ($project->isEmpty()) { - $user = new Document(['$id' => ID::custom(''), '$collection' => 'users']); + $user = new Document([]); } else { $user = $dbForProject->getDocument('users', Auth::$unique); } @@ -959,14 +959,14 @@ App::setResource('user', function ($mode, $project, $console, $request, $respons $user->isEmpty() // Check a document has been found in the DB || !Auth::sessionVerify($user->getAttribute('sessions', []), Auth::$secret, $authDuration) ) { // Validate user has valid login token - $user = new Document(['$id' => ID::custom(''), '$collection' => 'users']); + $user = new Document([]); } if (APP_MODE_ADMIN === $mode) { if ($user->find('teamId', $project->getAttribute('teamId'), 'memberships')) { Authorization::setDefaultStatus(false); // Cancel security segmentation for admin users. } else { - $user = new Document(['$id' => ID::custom(''), '$collection' => 'users']); + $user = new Document([]); } } @@ -989,7 +989,7 @@ App::setResource('user', function ($mode, $project, $console, $request, $respons } if (empty($user->find('$id', $jwtSessionId, 'sessions'))) { // Match JWT to active token - $user = new Document(['$id' => ID::custom(''), '$collection' => 'users']); + $user = new Document([]); } } diff --git a/composer.json b/composer.json index da8071baca..1edb6d0e1c 100644 --- a/composer.json +++ b/composer.json @@ -43,13 +43,13 @@ "ext-sockets": "*", "appwrite/php-clamav": "2.0.*", "appwrite/php-runtimes": "0.11.*", - "utopia-php/abuse": "0.27.*", + "utopia-php/abuse": "0.31.*", "utopia-php/analytics": "0.10.*", - "utopia-php/audit": "0.29.*", + "utopia-php/audit": "0.33.*", "utopia-php/cache": "0.8.*", "utopia-php/cli": "0.15.*", "utopia-php/config": "0.2.*", - "utopia-php/database": "0.38.*", + "utopia-php/database": "0.42.*", "utopia-php/domains": "1.1.*", "utopia-php/dsn": "0.1.*", "utopia-php/framework": "0.28.*", diff --git a/composer.lock b/composer.lock index 745633cb04..4a0862679d 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "d8f45e92243913c5898f4c6b483ed770", + "content-hash": "2098172fc4b71eb0d41dcdbfea2f5061", "packages": [ { "name": "adhocore/jwt", @@ -1225,23 +1225,23 @@ }, { "name": "utopia-php/abuse", - "version": "0.27.0", + "version": "0.31.0", "source": { "type": "git", "url": "https://github.com/utopia-php/abuse.git", - "reference": "d1115f5843e903ffaba9c23e450b33c0fe265ae0" + "reference": "d771c2c8d7d1237b1d04b5bf57b07a9b4736e627" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/abuse/zipball/d1115f5843e903ffaba9c23e450b33c0fe265ae0", - "reference": "d1115f5843e903ffaba9c23e450b33c0fe265ae0", + "url": "https://api.github.com/repos/utopia-php/abuse/zipball/d771c2c8d7d1237b1d04b5bf57b07a9b4736e627", + "reference": "d771c2c8d7d1237b1d04b5bf57b07a9b4736e627", "shasum": "" }, "require": { "ext-curl": "*", "ext-pdo": "*", "php": ">=8.0", - "utopia-php/database": "0.38.*" + "utopia-php/database": "0.42.*" }, "require-dev": { "laravel/pint": "1.5.*", @@ -1268,9 +1268,9 @@ ], "support": { "issues": "https://github.com/utopia-php/abuse/issues", - "source": "https://github.com/utopia-php/abuse/tree/0.27.0" + "source": "https://github.com/utopia-php/abuse/tree/0.31.0" }, - "time": "2023-07-15T00:53:50+00:00" + "time": "2023-08-11T01:17:15+00:00" }, { "name": "utopia-php/analytics", @@ -1320,21 +1320,21 @@ }, { "name": "utopia-php/audit", - "version": "0.29.0", + "version": "0.33.0", "source": { "type": "git", "url": "https://github.com/utopia-php/audit.git", - "reference": "5318538f457bf73623629345c98ea06371ca5dd4" + "reference": "6fb82331c58c66cbdb8a419314a687fcb18a1d22" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/audit/zipball/5318538f457bf73623629345c98ea06371ca5dd4", - "reference": "5318538f457bf73623629345c98ea06371ca5dd4", + "url": "https://api.github.com/repos/utopia-php/audit/zipball/6fb82331c58c66cbdb8a419314a687fcb18a1d22", + "reference": "6fb82331c58c66cbdb8a419314a687fcb18a1d22", "shasum": "" }, "require": { "php": ">=8.0", - "utopia-php/database": "0.38.*" + "utopia-php/database": "0.42.*" }, "require-dev": { "laravel/pint": "1.5.*", @@ -1361,9 +1361,9 @@ ], "support": { "issues": "https://github.com/utopia-php/audit/issues", - "source": "https://github.com/utopia-php/audit/tree/0.29.0" + "source": "https://github.com/utopia-php/audit/tree/0.33.0" }, - "time": "2023-07-15T00:51:10+00:00" + "time": "2023-08-11T01:17:28+00:00" }, { "name": "utopia-php/cache", @@ -1516,16 +1516,16 @@ }, { "name": "utopia-php/database", - "version": "0.38.0", + "version": "0.42.0", "source": { "type": "git", "url": "https://github.com/utopia-php/database.git", - "reference": "59e4684cf87e03c12dab9240158c1dfc6888e534" + "reference": "5631f151ce2b454517f11c1914e50a1fa70c7bd1" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/database/zipball/59e4684cf87e03c12dab9240158c1dfc6888e534", - "reference": "59e4684cf87e03c12dab9240158c1dfc6888e534", + "url": "https://api.github.com/repos/utopia-php/database/zipball/5631f151ce2b454517f11c1914e50a1fa70c7bd1", + "reference": "5631f151ce2b454517f11c1914e50a1fa70c7bd1", "shasum": "" }, "require": { @@ -1566,9 +1566,9 @@ ], "support": { "issues": "https://github.com/utopia-php/database/issues", - "source": "https://github.com/utopia-php/database/tree/0.38.0" + "source": "https://github.com/utopia-php/database/tree/0.42.0" }, - "time": "2023-07-14T07:49:38+00:00" + "time": "2023-08-10T23:18:38+00:00" }, { "name": "utopia-php/domains", diff --git a/src/Appwrite/GraphQL/Types/Mapper.php b/src/Appwrite/GraphQL/Types/Mapper.php index 9d2705baa7..0be60d93dc 100644 --- a/src/Appwrite/GraphQL/Types/Mapper.php +++ b/src/Appwrite/GraphQL/Types/Mapper.php @@ -252,6 +252,8 @@ class Mapper case 'Appwrite\Utopia\Database\Validator\Queries\Base': case 'Appwrite\Utopia\Database\Validator\Queries\Buckets': case 'Appwrite\Utopia\Database\Validator\Queries\Collections': + case 'Appwrite\Utopia\Database\Validator\Queries\Attributes': + case 'Appwrite\Utopia\Database\Validator\Queries\Indexes': case 'Appwrite\Utopia\Database\Validator\Queries\Databases': case 'Appwrite\Utopia\Database\Validator\Queries\Deployments': case 'Utopia\Database\Validator\Queries\Documents': diff --git a/src/Appwrite/Specification/Format/OpenAPI3.php b/src/Appwrite/Specification/Format/OpenAPI3.php index bf0293d5af..81999c06ad 100644 --- a/src/Appwrite/Specification/Format/OpenAPI3.php +++ b/src/Appwrite/Specification/Format/OpenAPI3.php @@ -343,6 +343,8 @@ class OpenAPI3 extends Format case 'Utopia\Validator\ArrayList': case 'Appwrite\Utopia\Database\Validator\Queries\Buckets': case 'Appwrite\Utopia\Database\Validator\Queries\Collections': + case 'Appwrite\Utopia\Database\Validator\Queries\Indexes': + case 'Appwrite\Utopia\Database\Validator\Queries\Attributes': case 'Appwrite\Utopia\Database\Validator\Queries\Databases': case 'Appwrite\Utopia\Database\Validator\Queries\Deployments': case 'Utopia\Database\Validator\Queries\Documents': diff --git a/src/Appwrite/Specification/Format/Swagger2.php b/src/Appwrite/Specification/Format/Swagger2.php index 60974bef85..ac489864eb 100644 --- a/src/Appwrite/Specification/Format/Swagger2.php +++ b/src/Appwrite/Specification/Format/Swagger2.php @@ -342,6 +342,8 @@ class Swagger2 extends Format case 'Utopia\Validator\ArrayList': case 'Appwrite\Utopia\Database\Validator\Queries\Buckets': case 'Appwrite\Utopia\Database\Validator\Queries\Collections': + case 'Appwrite\Utopia\Database\Validator\Queries\Indexes': + case 'Appwrite\Utopia\Database\Validator\Queries\Attributes': case 'Appwrite\Utopia\Database\Validator\Queries\Databases': case 'Appwrite\Utopia\Database\Validator\Queries\Deployments': case 'Utopia\Database\Validator\Queries\Documents': diff --git a/src/Appwrite/Utopia/Database/Validator/Queries/Attributes.php b/src/Appwrite/Utopia/Database/Validator/Queries/Attributes.php new file mode 100644 index 0000000000..1463316ad1 --- /dev/null +++ b/src/Appwrite/Utopia/Database/Validator/Queries/Attributes.php @@ -0,0 +1,27 @@ +getAttribute('options'); + if (!\is_null($options)) { + $document->setAttribute('relatedCollection', $options['relatedCollection']); + $document->setAttribute('relationType', $options['relationType']); + $document->setAttribute('twoWay', $options['twoWay']); + $document->setAttribute('twoWayKey', $options['twoWayKey']); + $document->setAttribute('side', $options['side']); + $document->setAttribute('onDelete', $options['onDelete']); + } + return $document; + } } diff --git a/tests/e2e/Services/Databases/DatabasesBase.php b/tests/e2e/Services/Databases/DatabasesBase.php index bd9443adec..c2c1c70bd6 100644 --- a/tests/e2e/Services/Databases/DatabasesBase.php +++ b/tests/e2e/Services/Databases/DatabasesBase.php @@ -5,10 +5,12 @@ namespace Tests\E2E\Services\Databases; use Appwrite\Extend\Exception; use Tests\E2E\Client; use Utopia\Database\Database; +use Utopia\Database\Document; use Utopia\Database\DateTime; use Utopia\Database\Helpers\ID; use Utopia\Database\Helpers\Permission; use Utopia\Database\Helpers\Role; +use Utopia\Database\Query; use Utopia\Database\Validator\Datetime as DatetimeValidator; trait DatabasesBase @@ -316,6 +318,32 @@ trait DatabasesBase return $data; } + /** + * @depends testCreateAttributes + */ + public function testListAttributes(array $data): void + { + $databaseId = $data['databaseId']; + $response = $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")', 'limit(2)', 'cursorAfter(title)'], + ]); + $this->assertEquals(200, $response['headers']['status-code']); + $this->assertEquals(2, \count($response['body']['attributes'])); + $response = $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' => ['select(["key"])'], + ]); + $this->assertEquals(Exception::GENERAL_ARGUMENT_INVALID, $response['body']['type']); + $this->assertEquals(400, $response['headers']['status-code']); + } + /** * @depends testCreateAttributes */ @@ -698,7 +726,6 @@ trait DatabasesBase $this->assertEquals(10, $attributes['body']['total']); $attributes = $attributes['body']['attributes']; - $this->assertIsArray($attributes); $this->assertCount(10, $attributes); @@ -1048,6 +1075,32 @@ trait DatabasesBase return $data; } + /** + * @depends testCreateIndexes + */ + public function testListIndexes(array $data): void + { + $databaseId = $data['databaseId']; + $response = $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")', 'limit(2)'], + ]); + $this->assertEquals(200, $response['headers']['status-code']); + $this->assertEquals(2, \count($response['body']['indexes'])); + $response = $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' => ['select(["key"])'], + ]); + $this->assertEquals(Exception::GENERAL_ARGUMENT_INVALID, $response['body']['type']); + $this->assertEquals(400, $response['headers']['status-code']); + } + /** * @depends testCreateIndexes */ @@ -1325,7 +1378,7 @@ trait DatabasesBase 'x-appwrite-project' => $this->getProject()['$id'], ], $this->getHeaders()), [ 'queries' => [ - 'select(["title", "releaseYear"])', + 'select(["title", "releaseYear", "$id"])', ], ]); @@ -4045,10 +4098,9 @@ trait DatabasesBase 'x-appwrite-project' => $this->getProject()['$id'], ], $this->getHeaders()), [ 'queries' => [ - 'select(["libraries.*"])', + 'select(["libraries.*", "$id"])', ], ]); - $document = $response['body']['documents'][0]; $this->assertEquals(200, $response['headers']['status-code']); $this->assertArrayHasKey('libraries', $document); @@ -4058,7 +4110,7 @@ trait DatabasesBase 'x-appwrite-project' => $this->getProject()['$id'], ], $this->getHeaders()), [ 'queries' => [ - 'select(["fullName"])' + 'select(["fullName", "$id"])' ], ]);