From fcdddf7bf0511e696b3906012d72916a34a1c616 Mon Sep 17 00:00:00 2001 From: Darshan Date: Wed, 11 Jun 2025 12:21:48 +0530 Subject: [PATCH] address comments. --- app/config/errors.php | 13 +++-- app/config/events.php | 2 +- app/init/constants.php | 12 ----- src/Appwrite/Event/Event.php | 48 ++++++++++++++++- src/Appwrite/Event/Realtime.php | 51 ++----------------- src/Appwrite/Extend/Exception.php | 45 ++++++++-------- src/Appwrite/Messaging/Adapter/Realtime.php | 2 - .../Platform/Modules/Databases/Constants.php | 23 +++++++++ 8 files changed, 106 insertions(+), 90 deletions(-) create mode 100644 src/Appwrite/Platform/Modules/Databases/Constants.php diff --git a/app/config/errors.php b/app/config/errors.php index de26b077fc..f7e8f194ab 100644 --- a/app/config/errors.php +++ b/app/config/errors.php @@ -69,9 +69,14 @@ return [ 'description' => 'The request contains one or more invalid arguments. Please refer to the endpoint documentation.', 'code' => 400, ], - Exception::GENERAL_QUERY_LIMIT_EXCEEDED => [ - 'name' => Exception::GENERAL_QUERY_LIMIT_EXCEEDED, - 'description' => 'Query limit exceeded for the current attribute/column. Usage of more than 100 query values on a single attribute/column is prohibited.', + Exception::GENERAL_ATTRIBUTE_QUERY_LIMIT_EXCEEDED => [ + 'name' => Exception::GENERAL_ATTRIBUTE_QUERY_LIMIT_EXCEEDED, + 'description' => 'Query limit exceeded for the current attribute.', + 'code' => 400, + ], + Exception::GENERAL_COLUMN_QUERY_LIMIT_EXCEEDED => [ + 'name' => Exception::GENERAL_COLUMN_QUERY_LIMIT_EXCEEDED, + 'description' => 'Query limit exceeded for the current column.', 'code' => 400, ], Exception::GENERAL_QUERY_INVALID => [ @@ -813,7 +818,7 @@ return [ ], Exception::ATTRIBUTE_INVALID_RESIZE => [ 'name' => Exception::ATTRIBUTE_INVALID_RESIZE, - 'description' => "Existing data is too large for new size, truncate your existing data then try again.", + 'description' => 'Existing data is too large for new size, truncate your existing data then try again.', 'code' => 400, ], diff --git a/app/config/events.php b/app/config/events.php index b0e6b9f5fc..8e759aaf56 100644 --- a/app/config/events.php +++ b/app/config/events.php @@ -129,7 +129,7 @@ return [ '$resource' => true, '$description' => 'This event triggers on any columns event.', 'create' => [ - '$description' => 'This event triggers when an column is created.', + '$description' => 'This event triggers when a column is created.', ], 'delete' => [ '$description' => 'This event triggers when an column is deleted.' diff --git a/app/init/constants.php b/app/init/constants.php index c3b6d4a058..143bba29bd 100644 --- a/app/init/constants.php +++ b/app/init/constants.php @@ -265,15 +265,3 @@ const TOKENS_RESOURCE_TYPE_FILES = 'files'; const TOKENS_RESOURCE_TYPE_SITES = 'sites'; const TOKENS_RESOURCE_TYPE_FUNCTIONS = 'functions'; const TOKENS_RESOURCE_TYPE_DATABASES = 'databases'; - -// Context constants for database - -const DATABASE_ROWS_CONTEXT = 'row'; -const DATABASE_TABLES_CONTEXT = 'table'; -const DATABASE_COLUMNS_CONTEXT = 'column'; -const DATABASE_INDEX_CONTEXT = 'index'; - -const DATABASE_DOCUMENTS_CONTEXT = 'document'; -const DATABASE_ATTRIBUTES_CONTEXT = 'attribute'; -const DATABASE_COLLECTIONS_CONTEXT = 'collection'; -const DATABASE_COLUMN_INDEX_CONTEXT = 'columnIndex'; diff --git a/src/Appwrite/Event/Event.php b/src/Appwrite/Event/Event.php index d699a45417..73ca8126a0 100644 --- a/src/Appwrite/Event/Event.php +++ b/src/Appwrite/Event/Event.php @@ -576,7 +576,12 @@ class Event /** * Force a non-assoc array. */ - return \array_values($events); + $eventValues = \array_values($events); + + /** + * Return a combined list of table, collection events. + */ + return Event::mirrorCollectionEvents($pattern, $eventValues[0], $eventValues); } /** @@ -597,4 +602,45 @@ class Event $this->context = $event->context; return $this; } + + /** + * Adds `table` events for `collection` events. + * + * Example: + * + * `databases.*.collections.*.documents.*.update` →\ + * `[databases.*.collections.*.documents.*.update, databases.*.tables.*.rows.*.update]` + */ + private static function mirrorCollectionEvents(string $pattern, string $firstEvent, array $events): array + { + $tableEventMap = [ + 'documents' => 'rows', + 'collections' => 'tables', + 'attributes' => 'columns', + ]; + + if ( + str_contains($pattern, 'databases.') && + str_contains($firstEvent, 'collections') + ) { + $pairedEvents = []; + + foreach ($events as $event) { + $pairedEvents[] = $event; + + if (str_contains($event, 'collections')) { + $tableSideEvent = str_replace( + array_keys($tableEventMap), + array_values($tableEventMap), + $event + ); + $pairedEvents[] = $tableSideEvent; + } + } + + $events = $pairedEvents; + } + + return $events; + } } diff --git a/src/Appwrite/Event/Realtime.php b/src/Appwrite/Event/Realtime.php index 016006ae73..6cb51ffa14 100644 --- a/src/Appwrite/Event/Realtime.php +++ b/src/Appwrite/Event/Realtime.php @@ -72,11 +72,7 @@ class Realtime extends Event return false; } - $events = Event::generateEvents($this->getEvent(), $this->getParams()); - $firstEvent = $events[0]; // most verbose event pattern - - // generate and merge all collection and tables api events. - $events = $this->mirrorCollectionEvents($firstEvent, $events); + $allEvents = Event::generateEvents($this->getEvent(), $this->getParams()); $payload = new Document($this->getPayload()); @@ -87,7 +83,7 @@ class Realtime extends Event $tableOrCollection = $this->getContext('table') ?? $this->getContext('collection'); $target = RealtimeAdapter::fromPayload( - event: $firstEvent, + event: $allEvents[0], payload: $payload, project: $this->getProject(), database: $db, @@ -103,7 +99,7 @@ class Realtime extends Event $this->realtime->send( projectId: $projectId, payload: $this->getRealtimePayload(), - events: $events, + events: $allEvents, channels: $target['channels'], roles: $target['roles'], options: [ @@ -115,45 +111,4 @@ class Realtime extends Event return true; } - - /** - * Adds `table` events for `collection` events. - * - * Example: - * - * `databases.*.collections.*.documents.*.update` →\ - * `[databases.*.collections.*.documents.*.update, databases.*.tables.*.rows.*.update]` - */ - private function mirrorCollectionEvents(string $firstEvent, array $events): array - { - $tableEventMap = [ - 'documents' => 'rows', - 'collections' => 'tables', - 'attributes' => 'columns', - ]; - - if ( - str_contains($this->getEvent(), 'databases.') && - str_contains($firstEvent, 'collections') - ) { - $pairedEvents = []; - - foreach ($events as $event) { - $pairedEvents[] = $event; - - if (str_contains($event, 'collections')) { - $tableSideEvent = str_replace( - array_keys($tableEventMap), - array_values($tableEventMap), - $event - ); - $pairedEvents[] = $tableSideEvent; - } - } - - $events = $pairedEvents; - } - - return $events; - } } diff --git a/src/Appwrite/Extend/Exception.php b/src/Appwrite/Extend/Exception.php index 38a562571c..29880a03b1 100644 --- a/src/Appwrite/Extend/Exception.php +++ b/src/Appwrite/Extend/Exception.php @@ -48,7 +48,8 @@ class Exception extends \Exception public const GENERAL_SMTP_DISABLED = 'general_smtp_disabled'; public const GENERAL_PHONE_DISABLED = 'general_phone_disabled'; public const GENERAL_ARGUMENT_INVALID = 'general_argument_invalid'; - public const GENERAL_QUERY_LIMIT_EXCEEDED = 'general_query_limit_exceeded'; + public const GENERAL_COLUMN_QUERY_LIMIT_EXCEEDED = 'general_column_query_limit_exceeded'; + public const GENERAL_ATTRIBUTE_QUERY_LIMIT_EXCEEDED = 'general_attribute_query_limit_exceeded'; public const GENERAL_QUERY_INVALID = 'general_query_invalid'; public const GENERAL_ROUTE_NOT_FOUND = 'general_route_not_found'; public const GENERAL_CURSOR_NOT_FOUND = 'general_cursor_not_found'; @@ -197,9 +198,9 @@ class Exception extends \Exception public const COLLECTION_LIMIT_EXCEEDED = 'collection_limit_exceeded'; /** Tables */ - public const TABLE_NOT_FOUND = 'table_not_found'; - public const TABLE_ALREADY_EXISTS = 'table_already_exists'; - public const TABLE_LIMIT_EXCEEDED = 'table_limit_exceeded'; + public const TABLE_NOT_FOUND = 'table_not_found'; + public const TABLE_ALREADY_EXISTS = 'table_already_exists'; + public const TABLE_LIMIT_EXCEEDED = 'table_limit_exceeded'; /** Documents */ public const DOCUMENT_NOT_FOUND = 'document_not_found'; @@ -211,13 +212,13 @@ class Exception extends \Exception public const DOCUMENT_DELETE_RESTRICTED = 'document_delete_restricted'; /** Rows */ - public const ROW_NOT_FOUND = 'row_not_found'; - public const ROW_INVALID_STRUCTURE = 'row_invalid_structure'; - public const ROW_MISSING_DATA = 'row_missing_data'; - public const ROW_MISSING_PAYLOAD = 'row_missing_payload'; - public const ROW_ALREADY_EXISTS = 'row_already_exists'; - public const ROW_UPDATE_CONFLICT = 'row_update_conflict'; - public const ROW_DELETE_RESTRICTED = 'row_delete_restricted'; + public const ROW_NOT_FOUND = 'row_not_found'; + public const ROW_INVALID_STRUCTURE = 'row_invalid_structure'; + public const ROW_MISSING_DATA = 'row_missing_data'; + public const ROW_MISSING_PAYLOAD = 'row_missing_payload'; + public const ROW_ALREADY_EXISTS = 'row_already_exists'; + public const ROW_UPDATE_CONFLICT = 'row_update_conflict'; + public const ROW_DELETE_RESTRICTED = 'row_delete_restricted'; /** Attributes */ public const ATTRIBUTE_NOT_FOUND = 'attribute_not_found'; @@ -232,16 +233,16 @@ class Exception extends \Exception public const ATTRIBUTE_INVALID_RESIZE = 'attribute_invalid_resize'; /** Columns */ - public const COLUMN_NOT_FOUND = 'column_not_found'; - public const COLUMN_UNKNOWN = 'column_unknown'; - public const COLUMN_NOT_AVAILABLE = 'column_not_available'; - public const COLUMN_FORMAT_UNSUPPORTED = 'column_format_unsupported'; - public const COLUMN_DEFAULT_UNSUPPORTED = 'column_default_unsupported'; - public const COLUMN_ALREADY_EXISTS = 'column_already_exists'; - public const COLUMN_LIMIT_EXCEEDED = 'column_limit_exceeded'; - public const COLUMN_VALUE_INVALID = 'column_value_invalid'; - public const COLUMN_TYPE_INVALID = 'column_type_invalid'; - public const COLUMN_INVALID_RESIZE = 'column_invalid_resize'; + public const COLUMN_NOT_FOUND = 'column_not_found'; + public const COLUMN_UNKNOWN = 'column_unknown'; + public const COLUMN_NOT_AVAILABLE = 'column_not_available'; + public const COLUMN_FORMAT_UNSUPPORTED = 'column_format_unsupported'; + public const COLUMN_DEFAULT_UNSUPPORTED = 'column_default_unsupported'; + public const COLUMN_ALREADY_EXISTS = 'column_already_exists'; + public const COLUMN_LIMIT_EXCEEDED = 'column_limit_exceeded'; + public const COLUMN_VALUE_INVALID = 'column_value_invalid'; + public const COLUMN_TYPE_INVALID = 'column_type_invalid'; + public const COLUMN_INVALID_RESIZE = 'column_invalid_resize'; /** Relationship */ public const RELATIONSHIP_VALUE_INVALID = 'relationship_value_invalid'; @@ -347,7 +348,7 @@ class Exception extends \Exception public const MESSAGE_MISSING_SCHEDULE = 'message_missing_schedule'; /** Targets */ - public const TARGET_PROVIDER_INVALID_TYPE = 'target_provider_invalid_type'; + public const TARGET_PROVIDER_INVALID_TYPE = 'target_provider_invalid_type'; /** Schedules */ public const SCHEDULE_NOT_FOUND = 'schedule_not_found'; diff --git a/src/Appwrite/Messaging/Adapter/Realtime.php b/src/Appwrite/Messaging/Adapter/Realtime.php index bfe87c49db..aff4e400ce 100644 --- a/src/Appwrite/Messaging/Adapter/Realtime.php +++ b/src/Appwrite/Messaging/Adapter/Realtime.php @@ -312,12 +312,10 @@ class Realtime extends Adapter throw new \Exception('Collection or the Table needs to be passed to Realtime for Document/Row events in the Database.'); } - // 1.7.x - Tables API $channels[] = 'rows'; $channels[] = 'databases.' . $database->getId() . '.tables.' . $payload->getAttribute('$tableId') . '.rows'; $channels[] = 'databases.' . $database->getId() . '.tables.' . $payload->getAttribute('$tableId') . '.rows.' . $payload->getId(); - // 1.6.x - Collections API $channels[] = 'documents'; $channels[] = 'databases.' . $database->getId() . '.collections.' . $payload->getAttribute('$collectionId') . '.documents'; $channels[] = 'databases.' . $database->getId() . '.collections.' . $payload->getAttribute('$collectionId') . '.documents.' . $payload->getId(); diff --git a/src/Appwrite/Platform/Modules/Databases/Constants.php b/src/Appwrite/Platform/Modules/Databases/Constants.php new file mode 100644 index 0000000000..b17248bf4d --- /dev/null +++ b/src/Appwrite/Platform/Modules/Databases/Constants.php @@ -0,0 +1,23 @@ +