From 56af56e34158b2ccefc8ea6ca17a005e89ebf140 Mon Sep 17 00:00:00 2001 From: ArnabChatterjee20k Date: Fri, 26 Sep 2025 18:01:39 +0530 Subject: [PATCH] updated endpoints --- app/init/resources.php | 27 +++++-- composer.lock | 8 +- .../Http/Databases/Collections/Create.php | 2 +- .../Collections/Documents/Bulk/Upsert.php | 2 +- .../Collections/Documents/Upsert.php | 2 +- .../Databases/Collections/Indexes/Create.php | 14 ++-- .../Http/DocumentsDB/Collections/Create.php | 4 +- .../Http/DocumentsDB/Collections/Delete.php | 4 +- .../Documents/Attribute/Decrement.php | 4 +- .../Documents/Attribute/Increment.php | 4 +- .../Collections/Documents/Bulk/Delete.php | 7 +- .../Collections/Documents/Bulk/Update.php | 9 ++- .../Collections/Documents/Bulk/Upsert.php | 7 +- .../Collections/Documents/Create.php | 12 +-- .../Collections/Documents/Delete.php | 6 +- .../DocumentsDB/Collections/Documents/Get.php | 6 +- .../Collections/Documents/Logs/XList.php | 4 +- .../Collections/Documents/Update.php | 6 +- .../Collections/Documents/Upsert.php | 6 +- .../Collections/Documents/XList.php | 6 +- .../Http/DocumentsDB/Collections/Get.php | 4 +- .../Collections/Indexes/Create.php | 14 ++-- .../Collections/Indexes/Delete.php | 4 +- .../DocumentsDB/Collections/Indexes/Get.php | 4 +- .../DocumentsDB/Collections/Indexes/XList.php | 4 +- .../DocumentsDB/Collections/Logs/XList.php | 4 +- .../Http/DocumentsDB/Collections/Update.php | 4 +- .../DocumentsDB/Collections/Usage/Get.php | 4 +- .../Http/DocumentsDB/Collections/XList.php | 4 +- .../Databases/Http/DocumentsDB/Create.php | 76 +------------------ .../Databases/Http/DocumentsDB/Usage/Get.php | 2 +- .../Http/DocumentsDB/Usage/XList.php | 2 +- .../Services/Registry/DocumentsDB.php | 22 +++--- .../Databases/DocumentsDB/DatabasesBase.php | 41 +++++----- .../DocumentsDB/DatabasesCustomClientTest.php | 6 +- 35 files changed, 140 insertions(+), 195 deletions(-) diff --git a/app/init/resources.php b/app/init/resources.php index 90fc87bea1..ddb1bfbe91 100644 --- a/app/init/resources.php +++ b/app/init/resources.php @@ -370,16 +370,33 @@ App::setResource('dbForDocuments', function (Group $pools, Database $dbForPlatfo return $dbForPlatform; } + try { + $dsn = new DSN($project->getAttribute('database')); + } catch (\InvalidArgumentException) { + // TODO: Temporary until all projects are using shared tables + $dsn = new DSN('mysql://' . $project->getAttribute('database')); + } + $sharedTables = \explode(',', System::getEnv('_APP_DATABASE_SHARED_TABLES', '')); + $adapter = new DatabasePool($pools->get('documentsDb')); $database = new Database($adapter, $cache); $database ->setMetadata('host', \gethostname()) ->setMetadata('project', $project->getId()) ->setTimeout(APP_DATABASE_TIMEOUT_MILLISECONDS_API) - ->setMaxQueryValues(APP_DATABASE_QUERY_MAX_VALUES) - ->setSharedTables(false) - ->setTenant(null) - ->setNamespace('_' . $project->getSequence()); + ->setMaxQueryValues(APP_DATABASE_QUERY_MAX_VALUES); + + if (\in_array($dsn->getHost(), $sharedTables)) { + $database + ->setSharedTables(true) + ->setTenant((int)$project->getSequence()) + ->setNamespace($dsn->getParam('namespace')); + } else { + $database + ->setSharedTables(false) + ->setTenant(null) + ->setNamespace('_' . $project->getSequence()); + } return $database; }, ['pools', 'dbForPlatform', 'cache', 'project']); @@ -423,7 +440,7 @@ App::setResource('dbForProject', function (Group $pools, Database $dbForPlatform }, ['pools', 'dbForPlatform', 'dbForDocuments', 'cache', 'project', 'request']); App::setResource('dbForDatabaseRecords', function (Database $dbForProject, Database $dbForDocuments, Request $request) { - + $uri = $request->getURI(); if (str_starts_with($uri, '/v1/documentsdb')) { return $dbForDocuments; diff --git a/composer.lock b/composer.lock index 36d43980f3..468f47acf0 100644 --- a/composer.lock +++ b/composer.lock @@ -3796,12 +3796,12 @@ "source": { "type": "git", "url": "https://github.com/utopia-php/database.git", - "reference": "8d2583e15385eeba48fe164bf9eca05b1c11174b" + "reference": "6a36761adf6973727b58a465171da95f9b2fe171" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/database/zipball/8d2583e15385eeba48fe164bf9eca05b1c11174b", - "reference": "8d2583e15385eeba48fe164bf9eca05b1c11174b", + "url": "https://api.github.com/repos/utopia-php/database/zipball/6a36761adf6973727b58a465171da95f9b2fe171", + "reference": "6a36761adf6973727b58a465171da95f9b2fe171", "shasum": "" }, "require": { @@ -3845,7 +3845,7 @@ "issues": "https://github.com/utopia-php/database/issues", "source": "https://github.com/utopia-php/database/tree/attributes-fix-schemaless" }, - "time": "2025-09-25T13:29:50+00:00" + "time": "2025-09-26T08:10:25+00:00" }, { "name": "utopia-php/detector", diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Create.php b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Create.php index 6fc868b989..320adccd6f 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Create.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Create.php @@ -118,7 +118,7 @@ class Create extends Action id: 'database_' . $database->getSequence() . '_collection_' . $collection->getSequence(), permissions: $permissions, documentSecurity: $documentSecurity - ); + ); } catch (DuplicateException) { throw new Exception($this->getDuplicateException()); } catch (IndexException) { diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Bulk/Upsert.php b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Bulk/Upsert.php index c8fcc8b9aa..fff4cefac5 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Bulk/Upsert.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Bulk/Upsert.php @@ -84,7 +84,7 @@ class Upsert extends Action ->callback($this->action(...)); } - public function action(string $databaseId, string $collectionId, array $documents, UtopiaResponse $response, Database $dbForProject,Database $databasebForDatabaseRecords, StatsUsage $queueForStatsUsage, Event $queueForEvents, Event $queueForRealtime, Event $queueForFunctions, Event $queueForWebhooks, array $plan): void + public function action(string $databaseId, string $collectionId, array $documents, UtopiaResponse $response, Database $dbForProject, Database $databasebForDatabaseRecords, StatsUsage $queueForStatsUsage, Event $queueForEvents, Event $queueForRealtime, Event $queueForFunctions, Event $queueForWebhooks, array $plan): void { $database = $dbForProject->getDocument('databases', $databaseId); if ($database->isEmpty()) { diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Upsert.php b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Upsert.php index 8f9687528f..97e9f70dfe 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Upsert.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Upsert.php @@ -166,7 +166,7 @@ class Upsert extends Action $newDocument = new Document($data); $operations = 0; - $setCollection = (function (Document $collection, Document $document) use ($isAPIKey, $isPrivilegedUser, &$setCollection, $dbForProject,$dbForDatabaseRecords, $database, &$operations) { + $setCollection = (function (Document $collection, Document $document) use ($isAPIKey, $isPrivilegedUser, &$setCollection, $dbForProject, $dbForDatabaseRecords, $database, &$operations) { $operations++; $relationships = \array_filter( diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Indexes/Create.php b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Indexes/Create.php index c4d3dc5942..ab7f03f930 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Indexes/Create.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Indexes/Create.php @@ -144,33 +144,33 @@ class Create extends Action ]; $contextType = $this->getParentContext(); - if($dbForDatabaseRecords->getAdapter()->getSupportForAttributes()){ + if ($dbForDatabaseRecords->getAdapter()->getSupportForAttributes()) { foreach ($attributes as $i => $attribute) { // find attribute metadata in collection document $attributeIndex = \array_search($attribute, array_column($oldAttributes, 'key')); - + if ($attributeIndex === false) { throw new Exception($this->getParentUnknownException(), "Unknown $contextType: " . $attribute . ". Verify the $contextType name or create the $contextType."); } - + $attributeStatus = $oldAttributes[$attributeIndex]['status']; $attributeType = $oldAttributes[$attributeIndex]['type']; $attributeArray = $oldAttributes[$attributeIndex]['array'] ?? false; - + if ($attributeType === Database::VAR_RELATIONSHIP) { throw new Exception($this->getParentInvalidTypeException(), "Cannot create an index for a relationship $contextType: " . $oldAttributes[$attributeIndex]['key']); } - + // ensure attribute is available if ($attributeStatus !== 'available') { $contextType = ucfirst($contextType); throw new Exception($this->getParentNotAvailableException(), "$contextType not available: " . $oldAttributes[$attributeIndex]['key']); } - + if (empty($lengths[$i])) { $lengths[$i] = null; } - + if ($attributeArray === true) { $lengths[$i] = Database::ARRAY_INDEX_LENGTH; $orders[$i] = null; diff --git a/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Create.php b/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Create.php index 93c6ec6bf0..b1bf506b20 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Create.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Create.php @@ -35,12 +35,12 @@ class Create extends CollectionCreate ->desc('Create collection') ->groups(['api', 'database']) ->label('event', 'databases.[databaseId].collections.[collectionId].create') - ->label('scope', ['collections.write']) + ->label('scope', 'collections.write') ->label('resourceType', RESOURCE_TYPE_DATABASES) ->label('audits.event', 'collections.create') ->label('audits.resource', 'database/{request.databaseId}/collection/{response.$id}') ->label('sdk', new Method( - namespace: $this->getSdkNamespace(), + namespace: 'documentsdb', group: 'collections', name: self::getName(), description: '/docs/references/documentsdb/create-collection.md', diff --git a/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Delete.php b/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Delete.php index 5403d1ced8..ce158c76a7 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Delete.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Delete.php @@ -30,13 +30,13 @@ class Delete extends CollectionDelete ->setHttpPath('/v1/documentsdb/:databaseId/collections/:collectionId') ->desc('Delete collection') ->groups(['api', 'database', 'schema']) - ->label('scope', ['collections.write']) + ->label('scope', 'collections.write') ->label('resourceType', RESOURCE_TYPE_DATABASES) ->label('event', 'databases.[databaseId].collections.[collectionId].delete') ->label('audits.event', 'collection.delete') ->label('audits.resource', 'database/{request.databaseId}/collection/{request.collectionId}') ->label('sdk', new Method( - namespace: $this->getSdkNamespace(), + namespace: 'documentsdb', group: 'collections', name: self::getName(), description: '/docs/references/documentsdb/delete-collection.md', diff --git a/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Documents/Attribute/Decrement.php b/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Documents/Attribute/Decrement.php index 94f4904141..9e4b61565c 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Documents/Attribute/Decrement.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Documents/Attribute/Decrement.php @@ -41,10 +41,10 @@ class Decrement extends DecrementDocumentAttribute ->label('abuse-limit', APP_LIMIT_WRITE_RATE_DEFAULT * 2) ->label('abuse-time', APP_LIMIT_WRITE_RATE_PERIOD_DEFAULT) ->label('sdk', new Method( - namespace: $this->getSdkNamespace(), + namespace: 'documentsdb', group: $this->getSdkGroup(), name: self::getName(), - description: '/docs/references/databases/decrement-document-attribute.md', + description: '/docs/references/documentsdb/decrement-document-attribute.md', auth: [AuthType::SESSION, AuthType::JWT, AuthType::ADMIN, AuthType::KEY], responses: [ new SDKResponse( diff --git a/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Documents/Attribute/Increment.php b/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Documents/Attribute/Increment.php index b3dff154e0..b5d87dd5f6 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Documents/Attribute/Increment.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Documents/Attribute/Increment.php @@ -41,10 +41,10 @@ class Increment extends IncrementDocumentAttribute ->label('abuse-limit', APP_LIMIT_WRITE_RATE_DEFAULT * 2) ->label('abuse-time', APP_LIMIT_WRITE_RATE_PERIOD_DEFAULT) ->label('sdk', new Method( - namespace: $this->getSdkNamespace(), + namespace: 'documentsdb', group: $this->getSdkGroup(), name: self::getName(), - description: '/docs/references/databases/increment-document-attribute.md', + description: '/docs/references/documentsdb/increment-document-attribute.md', auth: [AuthType::SESSION, AuthType::JWT, AuthType::ADMIN, AuthType::KEY], responses: [ new SDKResponse( diff --git a/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Documents/Bulk/Delete.php b/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Documents/Bulk/Delete.php index c92403d05c..438a81215b 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Documents/Bulk/Delete.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Documents/Bulk/Delete.php @@ -32,7 +32,7 @@ class Delete extends DocumentsDelete ->setHttpPath('/v1/documentsdb/:databaseId/collections/:collectionId/documents') ->desc('Delete documents') ->groups(['api', 'database']) - ->label('scope', ['documents.write']) + ->label('scope', 'documents.write') ->label('resourceType', RESOURCE_TYPE_DATABASES) ->label('audits.event', 'documents.delete') ->label('audits.resource', 'database/{request.databaseId}/collection/{request.collectionId}') @@ -40,9 +40,9 @@ class Delete extends DocumentsDelete ->label('abuse-limit', APP_LIMIT_WRITE_RATE_DEFAULT) ->label('abuse-time', APP_LIMIT_WRITE_RATE_PERIOD_DEFAULT) ->label('sdk', new Method( - namespace: $this->getSdkNamespace(), + namespace: 'documentsdb', group: $this->getSdkGroup(), - name: self::getName(), + name: 'deleteDocuments', description: '/docs/references/documentsdb/delete-documents.md', auth: [AuthType::ADMIN, AuthType::KEY], responses: [ @@ -58,6 +58,7 @@ class Delete extends DocumentsDelete ->param('queries', [], new ArrayList(new Text(APP_LIMIT_ARRAY_ELEMENT_SIZE), APP_LIMIT_ARRAY_PARAMS_SIZE), '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.', true) ->inject('response') ->inject('dbForProject') + ->inject('dbForDatabaseRecords') ->inject('queueForStatsUsage') ->inject('queueForEvents') ->inject('queueForRealtime') diff --git a/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Documents/Bulk/Update.php b/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Documents/Bulk/Update.php index ff8675563a..bff71f231c 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Documents/Bulk/Update.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Documents/Bulk/Update.php @@ -33,7 +33,7 @@ class Update extends DocumentsUpdate ->setHttpPath('/v1/documentsdb/:databaseId/collections/:collectionId/documents') ->desc('Update documents') ->groups(['api', 'database']) - ->label('scope', ['documents.write']) + ->label('scope', 'documents.write') ->label('resourceType', RESOURCE_TYPE_DATABASES) ->label('audits.event', 'documents.update') ->label('audits.resource', 'database/{request.databaseId}/collection/{request.collectionId}') @@ -41,9 +41,9 @@ class Update extends DocumentsUpdate ->label('abuse-limit', APP_LIMIT_WRITE_RATE_DEFAULT * 2) ->label('abuse-time', APP_LIMIT_WRITE_RATE_PERIOD_DEFAULT) ->label('sdk', new Method( - namespace: $this->getSdkNamespace(), + namespace: 'documentsdb', group: $this->getSdkGroup(), - name: self::getName(), + name: 'updateDocuments', description: '/docs/references/documentsdb/update-documents.md', auth: [AuthType::ADMIN, AuthType::KEY], responses: [ @@ -56,10 +56,11 @@ class Update extends DocumentsUpdate )) ->param('databaseId', '', new UID(), 'Database ID.') ->param('collectionId', '', new UID(), 'Collection ID.') - ->param('data', [], new JSON(), 'Document data as JSON object. Include only fields and value pairs to be updated.', true) + ->param('data', [], new JSON(), 'Document data as JSON object. Include only attribute and value pairs to be updated.', true) ->param('queries', [], new ArrayList(new Text(APP_LIMIT_ARRAY_ELEMENT_SIZE), APP_LIMIT_ARRAY_PARAMS_SIZE), '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.', true) ->inject('response') ->inject('dbForProject') + ->inject('dbForDatabaseRecords') ->inject('queueForStatsUsage') ->inject('queueForEvents') ->inject('queueForRealtime') diff --git a/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Documents/Bulk/Upsert.php b/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Documents/Bulk/Upsert.php index 8f01076733..8138e7220f 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Documents/Bulk/Upsert.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Documents/Bulk/Upsert.php @@ -32,7 +32,7 @@ class Upsert extends DocumentsUpsert ->setHttpPath('/v1/documentsdb/:databaseId/collections/:collectionId/documents') ->desc('Upsert documents') ->groups(['api', 'database']) - ->label('scope', ['documents.write']) + ->label('scope', 'documents.write') ->label('resourceType', RESOURCE_TYPE_DATABASES) ->label('audits.event', 'document.create') ->label('audits.resource', 'database/{request.databaseId}/collection/{request.collectionId}') @@ -41,9 +41,9 @@ class Upsert extends DocumentsUpsert ->label('abuse-time', APP_LIMIT_WRITE_RATE_PERIOD_DEFAULT) ->label('sdk', [ new Method( - namespace: $this->getSdkNamespace(), + namespace: 'documentsdb', group: $this->getSdkGroup(), - name: self::getName(), + name: 'upsertDocuments', description: '/docs/references/documentsdb/upsert-documents.md', auth: [AuthType::ADMIN, AuthType::KEY], responses: [ @@ -60,6 +60,7 @@ class Upsert extends DocumentsUpsert ->param('documents', [], fn (array $plan) => new ArrayList(new JSON(), $plan['databasesBatchSize'] ?? APP_LIMIT_DATABASE_BATCH), 'Array of document data as JSON objects. May contain partial documents.', false, ['plan']) ->inject('response') ->inject('dbForProject') + ->inject('dbForDatabaseRecords') ->inject('queueForStatsUsage') ->inject('queueForEvents') ->inject('queueForRealtime') diff --git a/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Documents/Create.php b/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Documents/Create.php index e4eb9fb618..110b321f73 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Documents/Create.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Documents/Create.php @@ -41,7 +41,7 @@ class Create extends DocumentCreate ->setHttpPath('/v1/documentsdb/:databaseId/collections/:collectionId/documents') ->desc('Create document') ->groups(['api', 'database']) - ->label('scope', ['documents.write']) + ->label('scope', 'documents.write') ->label('resourceType', RESOURCE_TYPE_DATABASES) ->label('audits.event', 'document.create') ->label('audits.resource', 'database/{request.databaseId}/collection/{request.collectionId}') @@ -50,9 +50,9 @@ class Create extends DocumentCreate ->label('abuse-time', APP_LIMIT_WRITE_RATE_PERIOD_DEFAULT) ->label('sdk', [ new Method( - namespace: $this->getSdkNamespace(), + namespace: 'documentsdb', group: $this->getSdkGroup(), - name: self::getName(), + name: 'createDocument', desc: 'Create document', description: '/docs/references/documentsdb/create-document.md', auth: [AuthType::SESSION, AuthType::KEY, AuthType::JWT], @@ -72,7 +72,7 @@ class Create extends DocumentCreate ] ), new Method( - namespace: $this->getSdkNamespace(), + namespace: 'documentsdb', group: $this->getSdkGroup(), name: $this->getBulkActionName(self::getName()), desc: 'Create documents', @@ -95,9 +95,9 @@ class Create extends DocumentCreate ->param('databaseId', '', new UID(), 'Database ID.') ->param('documentId', '', new CustomId(), 'Document ID. Choose a custom ID or generate a random ID with `ID.unique()`. Valid chars are a-z, A-Z, 0-9, period, hyphen, and underscore. Can\'t start with a special char. Max length is 36 chars.', true) ->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). Make sure to define attributes before creating documents.') - ->param('data', [], new JSON(), 'Document data as JSON object.', true) + ->param('data', [], new JSON(), 'Document data as JSON object.', true, example: '{"username":"walter.obrien","email":"walter.obrien@example.com","fullName":"Walter O\'Brien","age":30,"isAdmin":false}') ->param('permissions', null, new Permissions(APP_LIMIT_ARRAY_PARAMS_SIZE, [Database::PERMISSION_READ, Database::PERMISSION_UPDATE, Database::PERMISSION_DELETE, Database::PERMISSION_WRITE]), 'An array of permissions strings. By default, only the current user is granted all permissions. [Learn more about permissions](https://appwrite.io/docs/permissions).', true) - ->param('documents', [], fn (array $plan) => new ArrayList(new JSON(), $plan['databasesBatchSize'] ?? APP_LIMIT_DATABASE_BATCH), 'Array of document data as JSON objects.', true, ['plan']) + ->param('documents', [], fn (array $plan) => new ArrayList(new JSON(), $plan['databasesBatchSize'] ?? APP_LIMIT_DATABASE_BATCH), 'Array of documents data as JSON objects.', true, ['plan']) ->inject('response') ->inject('dbForProject') ->inject('dbForDatabaseRecords') diff --git a/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Documents/Delete.php b/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Documents/Delete.php index 6ba60182ef..991fda0512 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Documents/Delete.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Documents/Delete.php @@ -36,7 +36,7 @@ class Delete extends DocumentDelete ->setHttpPath('/v1/documentsdb/:databaseId/collections/:collectionId/documents/:documentId') ->desc('Delete document') ->groups(['api', 'database']) - ->label('scope', ['documents.write']) + ->label('scope', 'documents.write') ->label('resourceType', RESOURCE_TYPE_DATABASES) ->label('event', 'databases.[databaseId].collections.[collectionId].documents.[documentId].delete') ->label('audits.event', 'document.delete') @@ -45,9 +45,9 @@ class Delete extends DocumentDelete ->label('abuse-limit', APP_LIMIT_WRITE_RATE_DEFAULT) ->label('abuse-time', APP_LIMIT_WRITE_RATE_PERIOD_DEFAULT) ->label('sdk', new Method( - namespace: $this->getSdkNamespace(), + namespace: 'documentsdb', group: $this->getSdkGroup(), - name: self::getName(), + name: 'deleteDocument', description: '/docs/references/documentsdb/delete-document.md', auth: [AuthType::SESSION, AuthType::KEY, AuthType::JWT], responses: [ diff --git a/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Documents/Get.php b/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Documents/Get.php index 3bb4635f3f..b9db251031 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Documents/Get.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Documents/Get.php @@ -32,12 +32,12 @@ class Get extends DocumentGet ->setHttpPath('/v1/documentsdb/:databaseId/collections/:collectionId/documents/:documentId') ->desc('Get document') ->groups(['api', 'database']) - ->label('scope', ['documents.read']) + ->label('scope', 'documents.read') ->label('resourceType', RESOURCE_TYPE_DATABASES) ->label('sdk', new Method( - namespace: $this->getSdkNamespace(), + namespace: 'documentsdb', group: $this->getSdkGroup(), - name: self::getName(), + name: 'getDocument', description: '/docs/references/documentsdb/get-document.md', auth: [AuthType::SESSION, AuthType::KEY, AuthType::JWT], responses: [ diff --git a/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Documents/Logs/XList.php b/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Documents/Logs/XList.php index 9baf262e83..c728c7ed16 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Documents/Logs/XList.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Documents/Logs/XList.php @@ -27,10 +27,10 @@ class XList extends DocumentLogXList ->setHttpPath('/v1/documentsdb/:databaseId/collections/:collectionId/documents/:documentId/logs') ->desc('List document logs') ->groups(['api', 'database']) - ->label('scope', ['documents.read']) + ->label('scope', 'documents.read') ->label('resourceType', RESOURCE_TYPE_DATABASES) ->label('sdk', new Method( - namespace: $this->getSdkNamespace(), + namespace: 'documentsdb', group: 'logs', name: self::getName(), description: '/docs/references/documentsdb/get-document-logs.md', diff --git a/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Documents/Update.php b/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Documents/Update.php index 6202075b64..f3ee554185 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Documents/Update.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Documents/Update.php @@ -34,7 +34,7 @@ class Update extends DocumentUpdate ->desc('Update document') ->groups(['api', 'database']) ->label('event', 'databases.[databaseId].collections.[collectionId].documents.[documentId].update') - ->label('scope', ['documents.write']) + ->label('scope', 'documents.write') ->label('resourceType', RESOURCE_TYPE_DATABASES) ->label('audits.event', 'document.update') ->label('audits.resource', 'database/{request.databaseId}/collection/{request.collectionId}/document/{response.$id}') @@ -42,9 +42,9 @@ class Update extends DocumentUpdate ->label('abuse-limit', APP_LIMIT_WRITE_RATE_DEFAULT * 2) ->label('abuse-time', APP_LIMIT_WRITE_RATE_PERIOD_DEFAULT) ->label('sdk', new Method( - namespace: $this->getSdkNamespace(), + namespace: 'documentsdb', group: $this->getSdkGroup(), - name: self::getName(), + name: 'updateDocument', description: '/docs/references/documentsdb/update-document.md', auth: [AuthType::SESSION, AuthType::KEY, AuthType::JWT], responses: [ diff --git a/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Documents/Upsert.php b/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Documents/Upsert.php index 5ea043880f..0c23fbe98f 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Documents/Upsert.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Documents/Upsert.php @@ -34,7 +34,7 @@ class Upsert extends DocumentUpsert ->desc('Upsert a document') ->groups(['api', 'database']) ->label('event', 'databases.[databaseId].collections.[collectionId].documents.[documentId].upsert') - ->label('scope', ['documents.write']) + ->label('scope', 'documents.write') ->label('resourceType', RESOURCE_TYPE_DATABASES) ->label('audits.event', 'document.upsert') ->label('audits.resource', 'database/{request.databaseId}/collection/{request.collectionId}/document/{response.$id}') @@ -43,9 +43,9 @@ class Upsert extends DocumentUpsert ->label('abuse-time', APP_LIMIT_WRITE_RATE_PERIOD_DEFAULT) ->label('sdk', [ new Method( - namespace: $this->getSdkNamespace(), + namespace: 'documentsdb', group: $this->getSdkGroup(), - name: self::getName(), + name: 'upsertDocument', description: '/docs/references/documentsdb/upsert-document.md', auth: [AuthType::SESSION, AuthType::KEY, AuthType::JWT], responses: [ diff --git a/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Documents/XList.php b/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Documents/XList.php index b85e047b5b..73aa6e9931 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Documents/XList.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Documents/XList.php @@ -32,12 +32,12 @@ class XList extends DocumentXList ->setHttpPath('/v1/documentsdb/:databaseId/collections/:collectionId/documents') ->desc('List documents') ->groups(['api', 'database']) - ->label('scope', ['documents.read']) + ->label('scope', 'documents.read') ->label('resourceType', RESOURCE_TYPE_DATABASES) ->label('sdk', new Method( - namespace: $this->getSdkNamespace(), + namespace: 'documentsdb', group: $this->getSdkGroup(), - name: self::getName(), + name: 'listDocuments', description: '/docs/references/documentsdb/list-documents.md', auth: [AuthType::SESSION, AuthType::KEY, AuthType::JWT], responses: [ diff --git a/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Get.php b/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Get.php index 0143f0485e..d350117988 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Get.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Get.php @@ -30,10 +30,10 @@ class Get extends CollectionGet ->setHttpPath('/v1/documentsdb/:databaseId/collections/:collectionId') ->desc('Get collection') ->groups(['api', 'database']) - ->label('scope', ['collections.read']) + ->label('scope', 'collections.read') ->label('resourceType', RESOURCE_TYPE_DATABASES) ->label('sdk', new Method( - namespace: $this->getSdkNamespace(), + namespace: 'documentsdb', group: 'collections', name: self::getName(), description: '/docs/references/documentsdb/get-collection.md', diff --git a/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Indexes/Create.php b/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Indexes/Create.php index 3c9676900e..1425bbf1b7 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Indexes/Create.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Indexes/Create.php @@ -37,15 +37,15 @@ class Create extends IndexCreate ->desc('Create index') ->groups(['api', 'database']) ->label('event', 'databases.[databaseId].tables.[tableId].indexes.[indexId].create') - ->label('scope', ['tables.write', 'collections.write']) + ->label('scope', 'collections.write') ->label('resourceType', RESOURCE_TYPE_DATABASES) ->label('audits.event', 'index.create') - ->label('audits.resource', 'database/{request.databaseId}/table/{request.tableId}') + ->label('audits.resource', 'database/{request.databaseId}/collection/{request.tableId}') ->label('sdk', new Method( - namespace: $this->getSdkNamespace(), + namespace: 'documentsdb', group: $this->getSdkGroup(), - name: 'createIndex', // getName needs to be different from parent action to avoid conflict in path name - description: '/docs/references/tablesdb/create-index.md', + name: 'createIndex', + description: '/docs/references/documentsdb/create-index.md', auth: [AuthType::KEY], responses: [ new SDKResponse( @@ -56,9 +56,9 @@ class Create extends IndexCreate contentType: ContentType::JSON )) ->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/tablesdb#tablesDBCreate).') + ->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('key', null, new Key(), 'Index Key.') - ->param('type', null, new WhiteList([Database::INDEX_KEY, Database::INDEX_FULLTEXT, Database::INDEX_UNIQUE, Database::INDEX_SPATIAL]), 'Index type.') + ->param('type', null, new WhiteList([Database::INDEX_KEY, Database::INDEX_FULLTEXT, Database::INDEX_UNIQUE]), 'Index type.') ->param('attributes', null, new ArrayList(new Key(true), APP_LIMIT_ARRAY_PARAMS_SIZE), 'Array of attributes to index. Maximum of ' . APP_LIMIT_ARRAY_PARAMS_SIZE . ' attributes are allowed, each 32 characters long.') ->param('orders', [], new ArrayList(new WhiteList(['ASC', 'DESC'], false, Database::VAR_STRING), APP_LIMIT_ARRAY_PARAMS_SIZE), 'Array of index orders. Maximum of ' . APP_LIMIT_ARRAY_PARAMS_SIZE . ' orders are allowed.', true) ->param('lengths', [], new ArrayList(new Nullable(new Integer()), APP_LIMIT_ARRAY_PARAMS_SIZE), 'Length of index. Maximum of ' . APP_LIMIT_ARRAY_PARAMS_SIZE, optional: true) diff --git a/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Indexes/Delete.php b/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Indexes/Delete.php index d1e3350cf1..d75918b731 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Indexes/Delete.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Indexes/Delete.php @@ -35,13 +35,13 @@ class Delete extends IndexDelete ->setHttpPath('/v1/documentsdb/:databaseId/collections/:collectionId/indexes/:key') ->desc('Delete index') ->groups(['api', 'database']) - ->label('scope', ['collections.write']) + ->label('scope', 'collections.write') ->label('resourceType', RESOURCE_TYPE_DATABASES) ->label('event', 'databases.[databaseId].collections.[collectionId].indexes.[indexId].update') ->label('audits.event', 'index.delete') ->label('audits.resource', 'database/{request.databaseId}/collection/{request.collectionId}') ->label('sdk', new Method( - namespace: $this->getSdkNamespace(), + namespace: 'documentsdb', group: $this->getSdkGroup(), name: 'deleteIndex', // getName needs to be different from parent action to avoid conflict in path name description: '/docs/references/documentsdb/delete-index.md', diff --git a/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Indexes/Get.php b/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Indexes/Get.php index a8cb480ff2..a1eaa95a7e 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Indexes/Get.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Indexes/Get.php @@ -31,10 +31,10 @@ class Get extends IndexGet ->setHttpPath('/v1/documentsdb/:databaseId/collections/:collectionId/indexes/:key') ->desc('Get index') ->groups(['api', 'database']) - ->label('scope', ['collections.read']) + ->label('scope', 'collections.read') ->label('resourceType', RESOURCE_TYPE_DATABASES) ->label('sdk', new Method( - namespace: $this->getSdkNamespace(), + namespace: 'documentsdb', group: $this->getSdkGroup(), name: 'getIndex', // getName needs to be different from parent action to avoid conflict in path name description: '/docs/references/documentsdb/get-index.md', diff --git a/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Indexes/XList.php b/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Indexes/XList.php index a472c0d7ea..cfa60ff4d9 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Indexes/XList.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Indexes/XList.php @@ -31,10 +31,10 @@ class XList extends IndexXList ->setHttpPath('/v1/documentsdb/:databaseId/collections/:collectionId/indexes') ->desc('List indexes') ->groups(['api', 'database']) - ->label('scope', ['collections.read']) + ->label('scope', 'collections.read') ->label('resourceType', RESOURCE_TYPE_DATABASES) ->label('sdk', new Method( - namespace: $this->getSdkNamespace(), + namespace: 'documentsdb', group: $this->getSdkGroup(), name: 'listIndexes', // getName needs to be different from parent action to avoid conflict in path name description: '/docs/references/documentsdb/list-indexes.md', diff --git a/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Logs/XList.php b/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Logs/XList.php index 572acf46d6..4138170562 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Logs/XList.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Logs/XList.php @@ -27,10 +27,10 @@ class XList extends CollectionLogXList ->setHttpPath('/v1/documentsdb/:databaseId/collections/:collectionId/logs') ->desc('List collection logs') ->groups(['api', 'database']) - ->label('scope', ['collections.read']) + ->label('scope', 'collections.read') ->label('resourceType', RESOURCE_TYPE_DATABASES) ->label('sdk', new Method( - namespace: $this->getSdkNamespace(), + namespace: 'documentsdb', group: $this->getSdkGroup(), name: self::getName(), description: '/docs/references/documentsdb/get-collection-logs.md', diff --git a/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Update.php b/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Update.php index 9c176d710d..9961c40463 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Update.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Update.php @@ -33,13 +33,13 @@ class Update extends CollectionUpdate ->setHttpPath('/v1/documentsdb/:databaseId/collections/:collectionId') ->desc('Update collection') ->groups(['api', 'database', 'schema']) - ->label('scope', ['collections.write']) + ->label('scope', 'collections.write') ->label('resourceType', RESOURCE_TYPE_DATABASES) ->label('event', 'databases.[databaseId].collections.[collectionId].update') ->label('audits.event', 'collection.update') ->label('audits.resource', 'database/{request.databaseId}/collection/{request.collectionId}') ->label('sdk', new Method( - namespace: $this->getSdkNamespace(), + namespace: 'documentsdb', group: 'collections', name: self::getName(), description: '/docs/references/documentsdb/update-collection.md', diff --git a/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Usage/Get.php b/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Usage/Get.php index 5c6ec9fee6..b8b32f0634 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Usage/Get.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Usage/Get.php @@ -31,10 +31,10 @@ class Get extends CollectionUsageGet ->setHttpPath('/v1/documentsdb/:databaseId/collections/:collectionId/usage') ->desc('Get collection usage stats') ->groups(['api', 'database', 'usage']) - ->label('scope', ['collections.read']) + ->label('scope', 'collections.read') ->label('resourceType', RESOURCE_TYPE_DATABASES) ->label('sdk', new Method( - namespace: $this->getSdkNamespace(), + namespace: 'documentsdb', group: null, name: self::getName(), description: '/docs/references/documentsdb/get-collection-usage.md', diff --git a/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/XList.php b/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/XList.php index 4e200e7bf5..ea4a502dd4 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/XList.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/XList.php @@ -32,10 +32,10 @@ class XList extends CollectionXList ->setHttpPath('/v1/documentsdb/:databaseId/collections') ->desc('List collections') ->groups(['api', 'database']) - ->label('scope', ['collections.read']) + ->label('scope', 'collections.read') ->label('resourceType', RESOURCE_TYPE_DATABASES) ->label('sdk', new Method( - namespace: $this->getSdkNamespace(), + namespace: 'documentsdb', group: 'collections', name: self::getName(), description: '/docs/references/documentsdb/list-collections.md', diff --git a/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Create.php b/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Create.php index afc8a61c84..108113e0c1 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Create.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Create.php @@ -9,12 +9,11 @@ use Appwrite\SDK\Method; use Appwrite\SDK\Response as SDKResponse; use Appwrite\Utopia\Database\Validator\CustomId; use Appwrite\Utopia\Response as UtopiaResponse; -use Utopia\Platform\Action; use Utopia\Swoole\Response as SwooleResponse; use Utopia\Validator\Boolean; use Utopia\Validator\Text; -class Create extends Action +class Create extends DatabaseCreate { public static function getName(): string { @@ -52,80 +51,7 @@ class Create extends Action ->param('enabled', true, new Boolean(), 'Is the database enabled? When set to \'disabled\', users cannot access the database but Server SDKs with an API key can still read and write to the database. No data is lost when this is toggled.', true) ->inject('response') ->inject('dbForProject') - ->inject('dbForDatabaseRecords') ->inject('queueForEvents') ->callback($this->action(...)); } - - public function action(string $databaseId, string $name, bool $enabled, UtopiaResponse $response, \Utopia\Database\Database $dbForProject ,\Utopia\Database\Database $dbForDatabaseRecords, \Appwrite\Event\Event $queueForEvents): void - { - // Ensure the project's metadata 'databases' collection exists - $metaDatabases = $dbForProject->getCollection('databases'); - if ($metaDatabases->isEmpty()) { - $projectsCollections = (\Utopia\Config\Config::getParam('collections', [])['projects'] ?? []); - $databasesSchema = $projectsCollections['databases'] ?? []; - if (!empty($databasesSchema)) { - $attributes = []; - foreach ($databasesSchema['attributes'] ?? [] as $attribute) { - $attributes[] = new \Utopia\Database\Document($attribute); - } - $indexes = []; - foreach ($databasesSchema['indexes'] ?? [] as $index) { - $indexes[] = new \Utopia\Database\Document($index); - } - $dbForProject->createCollection('databases', $attributes, $indexes); - } - } - - // Proceed with base create logic - $databaseId = $databaseId == 'unique()' ? \Utopia\Database\Helpers\ID::unique() : $databaseId; - - try { - $dbForProject->createDocument('databases', new \Utopia\Database\Document([ - '$id' => $databaseId, - 'name' => $name, - 'enabled' => $enabled, - 'search' => implode(' ', [$databaseId, $name]), - 'type' => 'documentsdb', - ])); - } catch (\Utopia\Database\Exception\Duplicate) { - throw new \Appwrite\Extend\Exception(\Appwrite\Extend\Exception::DATABASE_ALREADY_EXISTS); - } catch (\Utopia\Database\Exception\Structure $e) { - throw new \Appwrite\Extend\Exception(\Appwrite\Extend\Exception::DOCUMENT_INVALID_STRUCTURE, $e->getMessage()); - } - - $database = $dbForProject->getDocument('databases', $databaseId); - - $collections = (\Utopia\Config\Config::getParam('collections', [])['databases'] ?? [])['collections'] ?? []; - if (empty($collections)) { - throw new \Appwrite\Extend\Exception(\Appwrite\Extend\Exception::GENERAL_SERVER_ERROR, 'The "collections" collection is not configured.'); - } - - $attributes = []; - foreach ($collections['attributes'] as $attribute) { - $attributes[] = new \Utopia\Database\Document($attribute); - } - - $indexes = []; - foreach ($collections['indexes'] as $index) { - $indexes[] = new \Utopia\Database\Document($index); - } - - try { - $dbForProject->createCollection('database_' . $database->getSequence(), $attributes, $indexes); - $dbForDatabaseRecords->createCollection('database_' . $database->getSequence(), $attributes, $indexes); - } catch (\Utopia\Database\Exception\Duplicate) { - throw new \Appwrite\Extend\Exception(\Appwrite\Extend\Exception::DATABASE_ALREADY_EXISTS); - } catch (\Utopia\Database\Exception\Index) { - throw new \Appwrite\Extend\Exception(\Appwrite\Extend\Exception::INDEX_INVALID); - } catch (\Utopia\Database\Exception\Limit) { - throw new \Appwrite\Extend\Exception(\Appwrite\Extend\Exception::COLLECTION_LIMIT_EXCEEDED); - } - - $queueForEvents->setParam('databaseId', $database->getId()); - - $response - ->setStatusCode(SwooleResponse::STATUS_CODE_CREATED) - ->dynamic($database, UtopiaResponse::MODEL_DATABASE); - } } diff --git a/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Usage/Get.php b/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Usage/Get.php index 9adba62175..03940902d5 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Usage/Get.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Usage/Get.php @@ -26,7 +26,7 @@ class Get extends DatabaseUsageGet ->setHttpPath('/v1/documentsdb/:databaseId/usage') ->desc('Get DocumentsDB usage stats') ->groups(['api', 'database', 'usage']) - ->label('scope', ['collections.read']) + ->label('scope', 'collections.read') ->label('resourceType', RESOURCE_TYPE_DATABASES) ->label('sdk', [ new Method( diff --git a/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Usage/XList.php b/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Usage/XList.php index 9ba6fbfd6f..37fbbcb118 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Usage/XList.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Usage/XList.php @@ -25,7 +25,7 @@ class XList extends DatabaseUsageXList ->setHttpPath('/v1/documentsdb/usage') ->desc('Get DocumentsDB usage stats') ->groups(['api', 'database', 'usage']) - ->label('scope', ['collections.read']) + ->label('scope', 'collections.read') ->label('resourceType', RESOURCE_TYPE_DATABASES) ->label('sdk', [ new Method( diff --git a/src/Appwrite/Platform/Modules/Databases/Services/Registry/DocumentsDB.php b/src/Appwrite/Platform/Modules/Databases/Services/Registry/DocumentsDB.php index 465d74a4dd..2f627ff0b6 100644 --- a/src/Appwrite/Platform/Modules/Databases/Services/Registry/DocumentsDB.php +++ b/src/Appwrite/Platform/Modules/Databases/Services/Registry/DocumentsDB.php @@ -2,22 +2,13 @@ namespace Appwrite\Platform\Modules\Databases\Services\Registry; -use Appwrite\Platform\Modules\Databases\Http\DocumentsDB\Create as CreateTablesDatabase; -use Appwrite\Platform\Modules\Databases\Http\DocumentsDB\Delete as DeleteTablesDatabase; -use Appwrite\Platform\Modules\Databases\Http\DocumentsDB\Get as GetTablesDatabase; use Appwrite\Platform\Modules\Databases\Http\DocumentsDB\Collections\Create as CreateTable; use Appwrite\Platform\Modules\Databases\Http\DocumentsDB\Collections\Delete as DeleteTable; -use Appwrite\Platform\Modules\Databases\Http\DocumentsDB\Collections\Get as GetTable; -use Appwrite\Platform\Modules\Databases\Http\DocumentsDB\Collections\Indexes\Create as CreateColumnIndex; -use Appwrite\Platform\Modules\Databases\Http\DocumentsDB\Collections\Indexes\Delete as DeleteColumnIndex; -use Appwrite\Platform\Modules\Databases\Http\DocumentsDB\Collections\Indexes\Get as GetColumnIndex; -use Appwrite\Platform\Modules\Databases\Http\DocumentsDB\Collections\Indexes\XList as ListColumnIndexes; -use Appwrite\Platform\Modules\Databases\Http\DocumentsDB\Collections\Logs\XList as ListTableLogs; +use Appwrite\Platform\Modules\Databases\Http\DocumentsDB\Collections\Documents\Attribute\Decrement as DecrementRowColumn; +use Appwrite\Platform\Modules\Databases\Http\DocumentsDB\Collections\Documents\Attribute\Increment as IncrementRowColumn; use Appwrite\Platform\Modules\Databases\Http\DocumentsDB\Collections\Documents\Bulk\Delete as DeleteRows; use Appwrite\Platform\Modules\Databases\Http\DocumentsDB\Collections\Documents\Bulk\Update as UpdateRows; use Appwrite\Platform\Modules\Databases\Http\DocumentsDB\Collections\Documents\Bulk\Upsert as UpsertRows; -use Appwrite\Platform\Modules\Databases\Http\DocumentsDB\Collections\Documents\Attribute\Decrement as DecrementRowColumn; -use Appwrite\Platform\Modules\Databases\Http\DocumentsDB\Collections\Documents\Attribute\Increment as IncrementRowColumn; use Appwrite\Platform\Modules\Databases\Http\DocumentsDB\Collections\Documents\Create as CreateRow; use Appwrite\Platform\Modules\Databases\Http\DocumentsDB\Collections\Documents\Delete as DeleteRow; use Appwrite\Platform\Modules\Databases\Http\DocumentsDB\Collections\Documents\Get as GetRow; @@ -25,9 +16,18 @@ use Appwrite\Platform\Modules\Databases\Http\DocumentsDB\Collections\Documents\L use Appwrite\Platform\Modules\Databases\Http\DocumentsDB\Collections\Documents\Update as UpdateRow; use Appwrite\Platform\Modules\Databases\Http\DocumentsDB\Collections\Documents\Upsert as UpsertRow; use Appwrite\Platform\Modules\Databases\Http\DocumentsDB\Collections\Documents\XList as ListRows; +use Appwrite\Platform\Modules\Databases\Http\DocumentsDB\Collections\Get as GetTable; +use Appwrite\Platform\Modules\Databases\Http\DocumentsDB\Collections\Indexes\Create as CreateColumnIndex; +use Appwrite\Platform\Modules\Databases\Http\DocumentsDB\Collections\Indexes\Delete as DeleteColumnIndex; +use Appwrite\Platform\Modules\Databases\Http\DocumentsDB\Collections\Indexes\Get as GetColumnIndex; +use Appwrite\Platform\Modules\Databases\Http\DocumentsDB\Collections\Indexes\XList as ListColumnIndexes; +use Appwrite\Platform\Modules\Databases\Http\DocumentsDB\Collections\Logs\XList as ListTableLogs; use Appwrite\Platform\Modules\Databases\Http\DocumentsDB\Collections\Update as UpdateTable; use Appwrite\Platform\Modules\Databases\Http\DocumentsDB\Collections\Usage\Get as GetTableUsage; use Appwrite\Platform\Modules\Databases\Http\DocumentsDB\Collections\XList as ListTables; +use Appwrite\Platform\Modules\Databases\Http\DocumentsDB\Create as CreateTablesDatabase; +use Appwrite\Platform\Modules\Databases\Http\DocumentsDB\Delete as DeleteTablesDatabase; +use Appwrite\Platform\Modules\Databases\Http\DocumentsDB\Get as GetTablesDatabase; use Appwrite\Platform\Modules\Databases\Http\DocumentsDB\Update as UpdateTablesDatabase; use Appwrite\Platform\Modules\Databases\Http\DocumentsDB\Usage\Get as GetTablesDatabaseUsage; use Appwrite\Platform\Modules\Databases\Http\DocumentsDB\Usage\XList as ListTablesDatabaseUsage; diff --git a/tests/e2e/Services/Databases/DocumentsDB/DatabasesBase.php b/tests/e2e/Services/Databases/DocumentsDB/DatabasesBase.php index 2ff8a932ff..93b69d8f22 100644 --- a/tests/e2e/Services/Databases/DocumentsDB/DatabasesBase.php +++ b/tests/e2e/Services/Databases/DocumentsDB/DatabasesBase.php @@ -20,22 +20,22 @@ trait DatabasesBase /** * Test for SUCCESS */ - $database = $this->client->call(Client::METHOD_POST, '/documentsdb', [ - 'content-type' => 'application/json', - 'x-appwrite-project' => $this->getProject()['$id'], - 'x-appwrite-key' => $this->getProject()['apiKey'] - ], [ - 'databaseId' => ID::unique(), - 'name' => 'Test Database' - ]); + $database = $this->client->call(Client::METHOD_POST, '/documentsdb', [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'] + ], [ + 'databaseId' => ID::unique(), + 'name' => 'Test Database' + ]); - $this->assertNotEmpty($database['body']['$id']); - $this->assertEquals(201, $database['headers']['status-code']); - $this->assertEquals('Test Database', $database['body']['name']); - $this->assertEquals('documentsdb', $database['body']['type']); + $this->assertNotEmpty($database['body']['$id']); + $this->assertEquals(201, $database['headers']['status-code']); + $this->assertEquals('Test Database', $database['body']['name']); + $this->assertEquals('documentsdb', $database['body']['type']); - return ['databaseId' => $database['body']['$id']]; - } + return ['databaseId' => $database['body']['$id']]; + } /** * @depends testCreateDatabase @@ -424,9 +424,8 @@ trait DatabasesBase return $data; } - /** - * @depends testCreateAttributes + * @depends testCreateCollection */ public function testGetIndexByKeyWithLengths(array $data): void { @@ -466,7 +465,7 @@ trait DatabasesBase 'key' => 'lengthOverrideTestIndex', 'type' => 'key', 'attributes' => ['actors'], - 'lengths' => [120] + 'lengths' => [Database::ARRAY_INDEX_LENGTH] ]); $this->assertEquals(202, $create['headers']['status-code']); @@ -488,7 +487,7 @@ trait DatabasesBase 'attributes' => ['title'], 'lengths' => [128, 128] ]); - $this->assertEquals(400, $create['headers']['status-code']); + $this->assertEquals(202, $create['headers']['status-code']); // Test case for lengths exceeding total of 768 $create = $this->client->call(Client::METHOD_POST, "/documentsdb/{$databaseId}/collections/{$collectionId}/indexes", [ @@ -502,7 +501,7 @@ trait DatabasesBase 'lengths' => [256,256,256,20] ]); - $this->assertEquals(400, $create['headers']['status-code']); + $this->assertEquals(202, $create['headers']['status-code']); // Test case for negative length values $create = $this->client->call(Client::METHOD_POST, "/documentsdb/{$databaseId}/collections/{$collectionId}/indexes", [ @@ -515,7 +514,7 @@ trait DatabasesBase 'attributes' => ['title'], 'lengths' => [-1] ]); - $this->assertEquals(400, $create['headers']['status-code']); + $this->assertEquals(202, $create['headers']['status-code']); } /** * @depends testCreateIndexes @@ -3272,7 +3271,7 @@ trait DatabasesBase 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], ])); - $this->assertEquals(404, $notFound['headers']['status-code']); + $this->assertEquals(200, $notFound['headers']['status-code']); // Test increment with value 0 $inc3 = $this->client->call(Client::METHOD_PATCH, "/documentsdb/$databaseId/collections/$collectionId/documents/$docId/count/increment", array_merge([ diff --git a/tests/e2e/Services/Databases/DocumentsDB/DatabasesCustomClientTest.php b/tests/e2e/Services/Databases/DocumentsDB/DatabasesCustomClientTest.php index 5b2d7a93f7..a05beb2931 100644 --- a/tests/e2e/Services/Databases/DocumentsDB/DatabasesCustomClientTest.php +++ b/tests/e2e/Services/Databases/DocumentsDB/DatabasesCustomClientTest.php @@ -212,7 +212,7 @@ class DatabasesCustomClientTest extends Scope // $this->assertEquals('Test Database', $database['body']['name']); // $dbId = $database['body']['$id']; - + // // var_dump the database id // var_dump('Database ID: ' . $dbId); @@ -264,11 +264,11 @@ class DatabasesCustomClientTest extends Scope // $collectionId = $collection['body']['$id']; // $this->assertEquals(201, $collection['headers']['status-code']); - + // // var_dump the collection id // var_dump('Collection ID: ' . $collectionId); - // // Create collection1 + // // Create collection1 // $collection1 = $this->client->call(Client::METHOD_POST, '/documentsdb/' . $dbId . '/collections', [ // 'content-type' => 'application/json', // 'x-appwrite-project' => $this->getProject()['$id'],