diff --git a/src/Appwrite/Databases/TransactionState.php b/src/Appwrite/Databases/TransactionState.php index 6d51356f89..f37366f72c 100644 --- a/src/Appwrite/Databases/TransactionState.php +++ b/src/Appwrite/Databases/TransactionState.php @@ -99,15 +99,15 @@ class TransactionState switch ($action) { case 'create': - if ($documentId) { - $state[$collectionId][$documentId] = [ + $docId = $documentId ?? ($data['$id'] ?? null); + if ($docId) { + $state[$collectionId][$docId] = [ 'action' => 'create', 'document' => new Document($data), 'exists' => true ]; } break; - case 'update': if (isset($state[$collectionId][$documentId])) { // Update existing document in transaction state @@ -133,7 +133,9 @@ class TransactionState break; case 'upsert': - $state[$collectionId][$documentId] = [ + $docId = $documentId ?? ($data['$id'] ?? null); + if (!$docId) { break; } + $state[$collectionId][$docId] = [ 'action' => 'upsert', 'document' => new Document($data), 'exists' => true 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 6123454a8b..2c8d26020d 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 @@ -124,7 +124,7 @@ class Upsert extends Action if (($existing + 1) > $maxBatch) { throw new Exception( Exception::TRANSACTION_LIMIT_EXCEEDED, - 'Transaction already has ' . $existing . ' operations, adding ' . \count($documents) . ' would exceed the maximum of ' . $maxBatch + 'Transaction already has ' . $existing . ' operations, adding 1 would exceed the maximum of ' . $maxBatch ); } @@ -144,7 +144,7 @@ class Upsert extends Action 'transactions', $transactionId, 'operations', - \count($staged) + 1 ); }); 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 67a54068b8..902a3585ba 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 @@ -381,7 +381,7 @@ class Create extends Action if (($existing + 1) > $maxBatch) { throw new Exception( Exception::TRANSACTION_LIMIT_EXCEEDED, - 'Transaction already has ' . $existing . ' operations, adding ' . \count($documents) . ' would exceed the maximum of ' . $maxBatch + 'Transaction already has ' . $existing . ' operations, adding 1 would exceed the maximum of ' . $maxBatch ); } diff --git a/src/Appwrite/Utopia/Database/Validator/Operation.php b/src/Appwrite/Utopia/Database/Validator/Operation.php index a8de32de79..5cc3ca2135 100644 --- a/src/Appwrite/Utopia/Database/Validator/Operation.php +++ b/src/Appwrite/Utopia/Database/Validator/Operation.php @@ -127,14 +127,25 @@ class Operation extends Validator } // If action requires documentId, it must be present - if ( - isset($this->requiresDocumentId[$value['action']]) && - !\array_key_exists($this->documentIdName, $value) - ) { +- if ( +- isset($this->requiresDocumentId[$value['action']]) && +- !\array_key_exists($this->documentIdName, $value) +- ) { +- $this->description = "Key '$this->documentIdName' is required for action '{$value['action']}'"; +- return false; + $actionRequiresDocumentId = ($this->requiresDocumentId[$value['action']] ?? false) === true; + if ($actionRequiresDocumentId && !\array_key_exists($this->documentIdName, $value)) { $this->description = "Key '$this->documentIdName' is required for action '{$value['action']}'"; return false; } + if (\array_key_exists($this->documentIdName, $value)) { + if (!\is_string($value[$this->documentIdName]) || \trim($value[$this->documentIdName]) === '') { + $this->description = "Key '$this->documentIdName' must be a non-empty string"; + return false; + } + } + // Data validation - only required for certain actions if (isset($this->requiresData[$value['action']]) && $this->requiresData[$value['action']]) { // Data is required for this action @@ -146,7 +157,7 @@ class Operation extends Validator $this->description = "Key 'data' must be an array"; return false; } - } elseif (\array_key_exists('data', $value)) { + } else if (\array_key_exists('data', $value)) { // Data is optional but if provided, must be an array if (!\is_array($value['data'])) { $this->description = "Key 'data' must be an array";