diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Action.php b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Action.php index d5e7d84695..52dfc62f50 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Action.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Action.php @@ -200,39 +200,31 @@ abstract class Action extends UtopiaAction /** * Resolves relationships in a document and attaches metadata. */ - final protected function resolveDocumentRelations(Document $document, Document $collection, array &$context): bool - { - /* @type Document $database */ - $database = $context['database']; + final protected function processDocument( + /* database */ + Document $database, + Document $collection, + Document $document, + Database $dbForProject, - /* @type Database $dbForProject */ - $dbForProject = $context['dbForProject']; + /* options */ + array &$collectionsCache, + ?int &$operations = null, + ): bool { - /* remove `$collection` if needed */ - $removeCollection = $context['removeCollection'] ?? false; - - /* count operations inside loop */ - $trackOperations = array_key_exists('trackOperations', $context); - - if (!$trackOperations) { - $context['operations'] ??= 0; - } elseif ($document->isEmpty()) { + if ($operations !== null && $document->isEmpty()) { return false; } - $operations = &$context['operations']; - $collectionsCache = &$context['collectionsCache']; + if ($operations !== null) { + $operations++; + } - $operations++; $collectionId = $collection->getId(); - + $document->removeAttribute('$collection'); $document->setAttribute('$databaseId', $database->getId()); $document->setAttribute('$collectionId', $collectionId); - if ($removeCollection) { - $document->removeAttribute('$collection'); - } - $relationships = $collectionsCache[$collectionId] ??= \array_filter( $collection->getAttribute('attributes', []), fn ($attr) => $attr->getAttribute('type') === Database::VAR_RELATIONSHIP @@ -243,7 +235,7 @@ abstract class Action extends UtopiaAction $related = $document->getAttribute($key); if (empty($related)) { - if (\in_array(\gettype($related), ['array', 'object'])) { + if (\in_array(\gettype($related), ['array', 'object']) && $operations !== null) { $operations++; } continue; @@ -266,14 +258,21 @@ abstract class Action extends UtopiaAction ); } - foreach ($relations as $index => $relation) { + foreach ($relations as $relation) { if ($relation instanceof Document) { $relatedCollection = new Document([ '$id' => $relatedCollectionId, 'attributes' => $collectionsCache[$relatedCollectionId], ]); - $this->resolveDocumentRelations(document: $relation, collection: $relatedCollection, context: $context); + $this->processDocument( + database: $database, + collection: $relatedCollection, + document: $relation, + dbForProject: $dbForProject, + collectionsCache: $collectionsCache, + operations: $operations + ); } } diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Create.php b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Create.php index 47c6acf0b7..ebab4a7ece 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Create.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Create.php @@ -374,12 +374,14 @@ class Create extends Action ->setContext($this->getCollectionsEventsContext(), $collection); $collectionsCache = []; - $removeCollection = true; - $context = compact('database', 'dbForProject', 'collectionsCache', 'removeCollection'); - foreach ($documents as $document) { - // Add $collectionId and $databaseId for all documents - $this->resolveDocumentRelations(document: $document, collection: $collection, context: $context); + $this->processDocument( + database: $database, + collection: $collection, + document: $document, + dbForProject: $dbForProject, + collectionsCache: $collectionsCache, + ); } $queueForStatsUsage diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Delete.php b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Delete.php index 9f1f27b1f8..d8ec82910c 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Delete.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Delete.php @@ -115,10 +115,13 @@ class Delete extends Action } $collectionsCache = []; - $context = compact('database', 'dbForProject', 'collectionsCache'); - - // Add $collection and $databaseId for all documents - $this->resolveDocumentRelations(document: $document, collection: $collection, context: $context); + $this->processDocument( + database: $database, + collection: $collection, + document: $document, + dbForProject: $dbForProject, + collectionsCache: $collectionsCache, + ); $queueForStatsUsage ->addMetric(METRIC_DATABASES_OPERATIONS_WRITES, 1) diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Get.php b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Get.php index bf1acb8086..fe40a2412f 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Get.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Get.php @@ -101,14 +101,14 @@ class Get extends Action $operations = 0; $collectionsCache = []; - $trackOperations = true; - $context = compact('database', 'dbForProject', 'operations', 'trackOperations', 'collectionsCache'); - - // Add $collectionId and $databaseId for all documents - $this->resolveDocumentRelations(document: $document, collection: $collection, context: $context); - - // get updated from the context - $operations = $context['operations']; + $this->processDocument( + database: $database, + collection: $collection, + document: $document, + dbForProject: $dbForProject, + collectionsCache: $collectionsCache, + operations: $operations + ); $queueForStatsUsage ->addMetric(METRIC_DATABASES_OPERATIONS_READS, max($operations, 1)) diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Update.php b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Update.php index b946731c7a..c39eeea707 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Update.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Update.php @@ -248,10 +248,13 @@ class Update extends Action } $collectionsCache = []; - $context = compact('database', 'dbForProject', 'collectionsCache'); - - // Add $collectionId and $databaseId for all documents - $this->resolveDocumentRelations(document: $document, collection: $collection, context: $context); + $this->processDocument( + database: $database, + collection: $collection, + document: $document, + dbForProject: $dbForProject, + collectionsCache: $collectionsCache, + ); $response->dynamic($document, $this->getResponseModel()); 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 d27ff632b8..fa632c7b2b 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 @@ -237,10 +237,13 @@ class Upsert extends Action $collectionsCache = []; $document = $upserted[0]; - $context = compact('database', 'dbForProject', 'collectionsCache'); - - // Add $collectionId and $databaseId for all documents - $this->resolveDocumentRelations(document: $document, collection: $collection, context: $context); + $this->processDocument( + database: $database, + collection: $collection, + document: $document, + dbForProject: $dbForProject, + collectionsCache: $collectionsCache, + ); $relationships = \array_map( fn ($document) => $document->getAttribute('key'), diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/XList.php b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/XList.php index b87efe42de..9aad8f4b77 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/XList.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/XList.php @@ -131,18 +131,17 @@ class XList extends Action $operations = 0; $collectionsCache = []; - $trackOperations = true; - $removeCollection = true; - $context = compact('database', 'dbForProject', 'operations', 'collectionsCache', 'removeCollection', 'trackOperations'); - - // Add $collectionId and $databaseId for all documents foreach ($documents as $document) { - $this->resolveDocumentRelations(document: $document, collection: $collection, context: $context); + $this->processDocument( + database: $database, + collection: $collection, + document: $document, + dbForProject: $dbForProject, + collectionsCache: $collectionsCache, + operations: $operations, + ); } - // get updated from the context - $operations = $context['operations']; - $queueForStatsUsage ->addMetric(METRIC_DATABASES_OPERATIONS_READS, max($operations, 1)) ->addMetric(str_replace('{databaseInternalId}', $database->getSequence(), METRIC_DATABASE_ID_OPERATIONS_READS), $operations);