From 077773625db6be8fe37c63cc2a4085445c0067be Mon Sep 17 00:00:00 2001 From: faisalill Date: Wed, 28 Jun 2023 21:19:49 +0530 Subject: [PATCH 01/16] fixed query and attribute types --- composer.json | 4 +++ src/Database/Adapter/MariaDB.php | 60 ++++++++++++++++++++++++-------- src/Database/Adapter/SQL.php | 35 +++++++++++++------ src/Database/Database.php | 32 ++++++++++++----- tests/Database/Base.php | 9 ++--- 5 files changed, 101 insertions(+), 39 deletions(-) diff --git a/composer.json b/composer.json index c28e846a0..99e085ce3 100755 --- a/composer.json +++ b/composer.json @@ -24,6 +24,10 @@ "Composer\\Config::disableProcessTimeout", "docker compose exec tests vendor/bin/phpunit --configuration phpunit.xml" ], + "mytest": [ + "Composer\\Config::disableProcessTimeout", + "docker compose exec tests vendor/bin/phpunit ./tests/Database/Adapter/MariaDBTest.php --verbose --stop-on-failure" + ], "lint": "./vendor/bin/pint --test", "format": "./vendor/bin/pint", "check": "./vendor/bin/phpstan analyse --level 7 src tests --memory-limit 512M", diff --git a/src/Database/Adapter/MariaDB.php b/src/Database/Adapter/MariaDB.php index 3ffc52da1..86e703e23 100644 --- a/src/Database/Adapter/MariaDB.php +++ b/src/Database/Adapter/MariaDB.php @@ -1046,17 +1046,31 @@ public function find(string $collection, array $queries = [], ?int $limit = 25, $results = $stmt->fetchAll(); foreach ($results as $key => $value) { - $results[$key]['$id'] = $value['_uid']; - $results[$key]['$internalId'] = $value['_id']; - $results[$key]['$createdAt'] = $value['_createdAt']; - $results[$key]['$updatedAt'] = $value['_updatedAt']; - $results[$key]['$permissions'] = json_decode($value['_permissions'] ?? '[]', true); - - unset($results[$key]['_uid']); - unset($results[$key]['_id']); - unset($results[$key]['_createdAt']); - unset($results[$key]['_updatedAt']); - unset($results[$key]['_permissions']); + + if (isset($value['_uid'])) { + $results[$key]['$id'] = $value['_uid']; + unset($results[$key]['_uid']); + } + + if (isset($value['_id'])) { + $results[$key]['$internalId'] = $value['_id']; + unset($results[$key]['_id']); + } + + if (isset($value['_createdAt'])) { + $results[$key]['$createdAt'] = $value['_createdAt']; + unset($results[$key]['_createdAt']); + } + + if (isset($value['_updatedAt'])) { + $results[$key]['$updatedAt'] = $value['_updatedAt']; + unset($results[$key]['_updatedAt']); + } + + if (isset($value['_permissions'])) { + $results[$key]['$permissions'] = json_decode($value['_permissions'] ?? '[]', true); + unset($results[$key]['_permissions']); + } $results[$key] = new Document($results[$key]); } @@ -1189,11 +1203,27 @@ protected function getAttributeProjection(array $selections, string $prefix = '' return '*'; } + if (in_array('$internalId', $selections)) { + $index = array_search('$internalId', $selections); + $selections[$index] = '_id'; + } + + if (in_array('$createdAt', $selections)) { + $index = array_search('$createdAt', $selections); + $selections[$index] = '_createdAt'; + } + + if (in_array('$updatedAt', $selections)) { + $index = array_search('$updatedAt', $selections); + $selections[$index] = '_updatedAt'; + } + + if (in_array('$permissions', $selections)) { + $index = array_search('$permissions', $selections); + $selections[$index] = '_permissions'; + } + $selections[] = '_uid'; - $selections[] = '_id'; - $selections[] = '_createdAt'; - $selections[] = '_updatedAt'; - $selections[] = '_permissions'; if (!empty($prefix)) { foreach ($selections as &$selection) { diff --git a/src/Database/Adapter/SQL.php b/src/Database/Adapter/SQL.php index 763986d5d..c6e73e303 100644 --- a/src/Database/Adapter/SQL.php +++ b/src/Database/Adapter/SQL.php @@ -127,17 +127,30 @@ public function getDocument(string $collection, string $id, array $queries = []) return new Document([]); } - $document['$id'] = $document['_uid']; - $document['$internalId'] = $document['_id']; - $document['$createdAt'] = $document['_createdAt']; - $document['$updatedAt'] = $document['_updatedAt']; - $document['$permissions'] = json_decode($document['_permissions'] ?? '[]', true); - - unset($document['_id']); - unset($document['_uid']); - unset($document['_createdAt']); - unset($document['_updatedAt']); - unset($document['_permissions']); + if(isset($document['_uid'])){ + $document['$id'] = $document['_uid']; + unset($document['_uid']); + } + + if(isset($document['_id'])){ + $document['$internalId'] = $document['_id']; + unset($document['_id']); + } + + if(isset($document['_createdAt'])){ + $document['$createdAt'] = $document['_createdAt']; + unset($document['_createdAt']); + } + + if(isset($document['_updatedAt'])){ + $document['$updatedAt'] = $document['_updatedAt']; + unset($document['_updatedAt']); + } + + if(isset($document['_permissions'])){ + $document['$permissions'] = json_decode($document['_permissions'] ?? '[]', true); + unset($document['_permissions']); + } return new Document($document); } diff --git a/src/Database/Database.php b/src/Database/Database.php index 4b39ef231..c8a209a5e 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -2207,14 +2207,14 @@ public function getDocument(string $collection, string $id, array $queries = []) return $document; } - if ($collection->getId() !== self::METADATA) { - if (!$validator->isValid([ - ...$collection->getRead(), - ...($documentSecurity ? $document->getRead() : []) - ])) { - return new Document(); - } - } + // if ($collection->getId() !== self::METADATA) { + // if (!$validator->isValid([ + // ...$collection->getRead(), + // ...($documentSecurity ? $document->getRead() : []) + // ])) { + // return new Document(); + // } + // } $document = $this->casting($collection, $document); $document = $this->decode($collection, $document, $selections); @@ -4313,7 +4313,16 @@ public function decode(Document $collection, Document $document, array $selectio } if (empty($selections) || \in_array($key, $selections) || \in_array('*', $selections)) { - $document->setAttribute($key, ($array) ? $value : $value[0]); + // var_dump(($array) ? $value : $value[0]); + if($key === '$createdAt' && $value[0] === NULL){ + continue; + } + else if($key === '$updatedAt' && $value[0] === NULL){ + continue; + } + else{ + $document->setAttribute($key, ($array) ? $value : $value[0]); + } } } @@ -4471,6 +4480,11 @@ private function validateSelections(Document $collection, array $queries): array } } + $keys[] = '$internalId'; + $keys[] = '$createdAt'; + $keys[] = '$updatedAt'; + $keys[] = '$permissions'; + $invalid = \array_diff($selections, $keys); if (!empty($invalid) && !\in_array('*', $invalid)) { throw new DatabaseException('Cannot select attributes: ' . \implode(', ', $invalid)); diff --git a/tests/Database/Base.php b/tests/Database/Base.php index dda3bd57a..d83aa9b7b 100644 --- a/tests/Database/Base.php +++ b/tests/Database/Base.php @@ -2539,13 +2539,14 @@ public function testFindSelect(): void $documents = static::getDatabase()->find('movies', [ Query::select(['name', 'year']) ]); + foreach ($documents as $document) { $this->assertArrayHasKey('$id', $document); - $this->assertArrayHasKey('$internalId', $document); + // $this->assertArrayHasKey('$internalId', $document); $this->assertArrayHasKey('$collection', $document); - $this->assertArrayHasKey('$createdAt', $document); - $this->assertArrayHasKey('$updatedAt', $document); - $this->assertArrayHasKey('$permissions', $document); + // $this->assertArrayHasKey('$createdAt', $document); + // $this->assertArrayHasKey('$updatedAt', $document); + // $this->assertArrayHasKey('$permissions', $document); $this->assertArrayHasKey('name', $document); $this->assertArrayHasKey('year', $document); $this->assertArrayNotHasKey('director', $document); From fedafbf3a6e73678dd29d0c8931661f29bde2b93 Mon Sep 17 00:00:00 2001 From: faisalill Date: Wed, 28 Jun 2023 21:26:17 +0530 Subject: [PATCH 02/16] cleaned composer.json --- composer.json | 4 ---- 1 file changed, 4 deletions(-) diff --git a/composer.json b/composer.json index 99e085ce3..c28e846a0 100755 --- a/composer.json +++ b/composer.json @@ -24,10 +24,6 @@ "Composer\\Config::disableProcessTimeout", "docker compose exec tests vendor/bin/phpunit --configuration phpunit.xml" ], - "mytest": [ - "Composer\\Config::disableProcessTimeout", - "docker compose exec tests vendor/bin/phpunit ./tests/Database/Adapter/MariaDBTest.php --verbose --stop-on-failure" - ], "lint": "./vendor/bin/pint --test", "format": "./vendor/bin/pint", "check": "./vendor/bin/phpstan analyse --level 7 src tests --memory-limit 512M", From 0278adfc38c9181a20740bde7bf773cc7f077b25 Mon Sep 17 00:00:00 2001 From: faisalill Date: Wed, 28 Jun 2023 21:37:38 +0530 Subject: [PATCH 03/16] clean code --- src/Database/Database.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Database/Database.php b/src/Database/Database.php index c8a209a5e..bed98de05 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -4313,7 +4313,6 @@ public function decode(Document $collection, Document $document, array $selectio } if (empty($selections) || \in_array($key, $selections) || \in_array('*', $selections)) { - // var_dump(($array) ? $value : $value[0]); if($key === '$createdAt' && $value[0] === NULL){ continue; } From cde877cba1252f92973c6f999254a3b5481d4b5d Mon Sep 17 00:00:00 2001 From: faisalill Date: Wed, 28 Jun 2023 21:40:44 +0530 Subject: [PATCH 04/16] changed few assertions for testFindSelect --- tests/Database/Base.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/Database/Base.php b/tests/Database/Base.php index d83aa9b7b..3be91c522 100644 --- a/tests/Database/Base.php +++ b/tests/Database/Base.php @@ -2542,13 +2542,13 @@ public function testFindSelect(): void foreach ($documents as $document) { $this->assertArrayHasKey('$id', $document); - // $this->assertArrayHasKey('$internalId', $document); $this->assertArrayHasKey('$collection', $document); - // $this->assertArrayHasKey('$createdAt', $document); - // $this->assertArrayHasKey('$updatedAt', $document); - // $this->assertArrayHasKey('$permissions', $document); $this->assertArrayHasKey('name', $document); $this->assertArrayHasKey('year', $document); + $this->assertArrayNotHasKey('$internalId', $document); + $this->assertArrayNotHasKey('$createdAt', $document); + $this->assertArrayNotHasKey('$updatedAt', $document); + $this->assertArrayNotHasKey('$permissions', $document); $this->assertArrayNotHasKey('director', $document); $this->assertArrayNotHasKey('price', $document); $this->assertArrayNotHasKey('active', $document); From b93544fa10715ed995e01b964ac03d3af4abff03 Mon Sep 17 00:00:00 2001 From: faisalill Date: Sat, 1 Jul 2023 22:26:15 +0530 Subject: [PATCH 05/16] return by default --- src/Database/Adapter/MariaDB.php | 1 + src/Database/Database.php | 16 ++++++++-------- tests/Database/Base.php | 8 ++++---- 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/Database/Adapter/MariaDB.php b/src/Database/Adapter/MariaDB.php index 86e703e23..220b6b263 100644 --- a/src/Database/Adapter/MariaDB.php +++ b/src/Database/Adapter/MariaDB.php @@ -1224,6 +1224,7 @@ protected function getAttributeProjection(array $selections, string $prefix = '' } $selections[] = '_uid'; + $selections[] = '_permissions'; if (!empty($prefix)) { foreach ($selections as &$selection) { diff --git a/src/Database/Database.php b/src/Database/Database.php index bed98de05..caa727317 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -2207,14 +2207,14 @@ public function getDocument(string $collection, string $id, array $queries = []) return $document; } - // if ($collection->getId() !== self::METADATA) { - // if (!$validator->isValid([ - // ...$collection->getRead(), - // ...($documentSecurity ? $document->getRead() : []) - // ])) { - // return new Document(); - // } - // } + if ($collection->getId() !== self::METADATA) { + if (!$validator->isValid([ + ...$collection->getRead(), + ...($documentSecurity ? $document->getRead() : []) + ])) { + return new Document(); + } + } $document = $this->casting($collection, $document); $document = $this->decode($collection, $document, $selections); diff --git a/tests/Database/Base.php b/tests/Database/Base.php index 3be91c522..d852c1818 100644 --- a/tests/Database/Base.php +++ b/tests/Database/Base.php @@ -2542,13 +2542,13 @@ public function testFindSelect(): void foreach ($documents as $document) { $this->assertArrayHasKey('$id', $document); + $this->assertArrayHasKey('$permissions', $document); $this->assertArrayHasKey('$collection', $document); + // $this->assertArrayHasKey('$internalId', $document); + // $this->assertArrayHasKey('$createdAt', $document); + // $this->assertArrayHasKey('$updatedAt', $document); $this->assertArrayHasKey('name', $document); $this->assertArrayHasKey('year', $document); - $this->assertArrayNotHasKey('$internalId', $document); - $this->assertArrayNotHasKey('$createdAt', $document); - $this->assertArrayNotHasKey('$updatedAt', $document); - $this->assertArrayNotHasKey('$permissions', $document); $this->assertArrayNotHasKey('director', $document); $this->assertArrayNotHasKey('price', $document); $this->assertArrayNotHasKey('active', $document); From 3a3b51fb42c29e1b5aea2a917ff3cf680a118e21 Mon Sep 17 00:00:00 2001 From: faisalill Date: Sat, 8 Jul 2023 22:49:10 +0530 Subject: [PATCH 06/16] permissions can be queried now --- composer.json | 4 ++++ src/Database/Database.php | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/composer.json b/composer.json index c28e846a0..01c3ba792 100755 --- a/composer.json +++ b/composer.json @@ -24,6 +24,10 @@ "Composer\\Config::disableProcessTimeout", "docker compose exec tests vendor/bin/phpunit --configuration phpunit.xml" ], + "mytest": [ + "Composer\\Config::disableProcessTimeout", + "docker compose exec tests vendor/bin/phpunit ./tests/Database/Adapter/MariaDBTest.php --stop-on-failure" + ], "lint": "./vendor/bin/pint --test", "format": "./vendor/bin/pint", "check": "./vendor/bin/phpstan analyse --level 7 src tests --memory-limit 512M", diff --git a/src/Database/Database.php b/src/Database/Database.php index caa727317..c41bba61a 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -2263,6 +2263,26 @@ public function getDocument(string $collection, string $id, array $queries = []) $this->trigger(self::EVENT_DOCUMENT_READ, $document); + foreach ($queries as $query){ + if($query->getMethod() == Query::TYPE_SELECT){ + $queriedValues = $query->getValues(); + $defaultKeys = ['$id', '$internalId', '$permissions', '$createdAt', '$updatedAt', '$collection']; + + foreach($queriedValues as $queriedValue){ + if(in_array($queriedValue, $defaultKeys)){ + $index = array_search($queriedValue, $defaultKeys); + unset($defaultKeys[$index]); + } + } + + foreach($defaultKeys as $defaultKey){ + if($document->isSet($defaultKey)){ + $document->removeAttribute($defaultKey); + } + } + } + } + return $document; } From e39efe79d2fda80b5cb4d163c26db0b25d390d5c Mon Sep 17 00:00:00 2001 From: faisalill Date: Sun, 9 Jul 2023 14:48:38 +0530 Subject: [PATCH 07/16] added assertions to test that other default keys are not returned --- src/Database/Adapter/MariaDB.php | 5 +++++ src/Database/Database.php | 1 + tests/Database/Base.php | 14 ++++++++++---- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/Database/Adapter/MariaDB.php b/src/Database/Adapter/MariaDB.php index 220b6b263..0ab801fc8 100644 --- a/src/Database/Adapter/MariaDB.php +++ b/src/Database/Adapter/MariaDB.php @@ -1203,6 +1203,11 @@ protected function getAttributeProjection(array $selections, string $prefix = '' return '*'; } + if (in_array('$id', $selections)) { + $index = array_search('$id', $selections); + $selections[$index] = '_uid'; + } + if (in_array('$internalId', $selections)) { $index = array_search('$internalId', $selections); $selections[$index] = '_id'; diff --git a/src/Database/Database.php b/src/Database/Database.php index c41bba61a..3f7c3ac75 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -4499,6 +4499,7 @@ private function validateSelections(Document $collection, array $queries): array } } + $keys[] = '$id'; $keys[] = '$internalId'; $keys[] = '$createdAt'; $keys[] = '$updatedAt'; diff --git a/tests/Database/Base.php b/tests/Database/Base.php index d852c1818..55c57f5bf 100644 --- a/tests/Database/Base.php +++ b/tests/Database/Base.php @@ -1029,6 +1029,12 @@ public function testGetDocumentSelect(Document $document): Document $this->assertArrayNotHasKey('boolean', $document->getAttributes()); $this->assertArrayNotHasKey('colors', $document->getAttributes()); $this->assertArrayNotHasKey('with-dash', $document->getAttributes()); + $this->assertEmpty($document->getAttribute('$id')); + $this->assertEmpty($document->getAttribute('$internalId')); + $this->assertEmpty($document->getAttribute('$collection')); + $this->assertEmpty($document->getAttribute('$permissions')); + $this->assertEmpty($document->getAttribute('$createdAt')); + $this->assertEmpty($document->getAttribute('$updatedAt')); return $document; } @@ -2544,9 +2550,9 @@ public function testFindSelect(): void $this->assertArrayHasKey('$id', $document); $this->assertArrayHasKey('$permissions', $document); $this->assertArrayHasKey('$collection', $document); - // $this->assertArrayHasKey('$internalId', $document); - // $this->assertArrayHasKey('$createdAt', $document); - // $this->assertArrayHasKey('$updatedAt', $document); + // $this->assertArrayNotHasKey('$internalId', $document); + // $this->assertArrayNotHasKey('$createdAt', $document); + // $this->assertArrayNotHasKey('$updatedAt', $document); $this->assertArrayHasKey('name', $document); $this->assertArrayHasKey('year', $document); $this->assertArrayNotHasKey('director', $document); @@ -4157,7 +4163,7 @@ public function testOneToOneOneWayRelationship(): void $this->assertArrayNotHasKey('area', $person->getAttribute('library')); $person = static::getDatabase()->getDocument('person', 'person1', [ - Query::select(['*', 'library.name']) + Query::select(['*', 'library.name', '$id']) ]); $this->assertEquals('Library 1', $person->getAttribute('library')->getAttribute('name')); From 60bae57dbb01013f517c5842172d6c3ce46ae0ce Mon Sep 17 00:00:00 2001 From: faisalill Date: Sun, 9 Jul 2023 16:49:58 +0530 Subject: [PATCH 08/16] fixed errors from testFindSelect --- composer.json | 2 +- src/Database/Adapter/Postgres.php | 31 ++++++++++++++++++++----------- src/Database/Database.php | 15 +++++++++++++++ tests/Database/Base.php | 14 +++++++------- 4 files changed, 43 insertions(+), 19 deletions(-) diff --git a/composer.json b/composer.json index 01c3ba792..015335b39 100755 --- a/composer.json +++ b/composer.json @@ -26,7 +26,7 @@ ], "mytest": [ "Composer\\Config::disableProcessTimeout", - "docker compose exec tests vendor/bin/phpunit ./tests/Database/Adapter/MariaDBTest.php --stop-on-failure" + "docker compose exec tests vendor/bin/phpunit ./tests/Database/Adapter/MongoDBTest.php --stop-on-failure" ], "lint": "./vendor/bin/pint --test", "format": "./vendor/bin/pint", diff --git a/src/Database/Adapter/Postgres.php b/src/Database/Adapter/Postgres.php index 348a2bd51..4fa5f46b0 100644 --- a/src/Database/Adapter/Postgres.php +++ b/src/Database/Adapter/Postgres.php @@ -1056,17 +1056,26 @@ public function find(string $collection, array $queries = [], ?int $limit = 25, $results = $stmt->fetchAll(); foreach ($results as $key => $value) { - $results[$key]['$id'] = $value['_uid']; - $results[$key]['$internalId'] = $value['_id']; - $results[$key]['$createdAt'] = $value['_createdAt']; - $results[$key]['$updatedAt'] = $value['_updatedAt']; - $results[$key]['$permissions'] = json_decode($value['_permissions'] ?? '[]', true); - - unset($results[$key]['_uid']); - unset($results[$key]['_id']); - unset($results[$key]['_createdAt']); - unset($results[$key]['_updatedAt']); - unset($results[$key]['_permissions']); + if(isset($value['_uid'])){ + $results[$key]['$id'] = $value['_uid']; + unset($results[$key]['_uid']); + } + if(isset($value['_id'])){ + $results[$key]['$internalId'] = $value['_id']; + unset($results[$key]['_id']); + } + if(isset($value['_createdAt'])){ + $results[$key]['$createdAt'] = $value['_createdAt']; + unset($results[$key]['_createdAt']); + } + if(isset($value['_updatedAt'])){ + $results[$key]['$updatedAt'] = $value['_updatedAt']; + unset($results[$key]['_updatedAt']); + } + if(isset($value['_permissions'])){ + $results[$key]['$permissions'] = json_decode($value['_permissions'] ?? '[]', true); + unset($results[$key]['_permissions']); + } $results[$key] = new Document($results[$key]); } diff --git a/src/Database/Database.php b/src/Database/Database.php index 3f7c3ac75..3a342dd5d 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -3998,6 +3998,21 @@ public function find(string $collection, array $queries = [], ?int $timeout = nu $this->trigger(self::EVENT_DOCUMENT_FIND, $results); + // remove default keys $id and $permissions from the results + + foreach ($queries as $query) { + if($query->getMethod() === Query::TYPE_SELECT){ + foreach ($results as $result) { + $result->removeAttribute('$id'); + $result->removeAttribute('$permissions'); + $result->removeAttribute('$collection'); + $result->removeAttribute('$createdAt'); + $result->removeAttribute('$updatedAt'); + $result->removeAttribute('$internalId'); + } + } + } + return $results; } diff --git a/tests/Database/Base.php b/tests/Database/Base.php index 55c57f5bf..fb15e662f 100644 --- a/tests/Database/Base.php +++ b/tests/Database/Base.php @@ -2547,17 +2547,17 @@ public function testFindSelect(): void ]); foreach ($documents as $document) { - $this->assertArrayHasKey('$id', $document); - $this->assertArrayHasKey('$permissions', $document); - $this->assertArrayHasKey('$collection', $document); - // $this->assertArrayNotHasKey('$internalId', $document); - // $this->assertArrayNotHasKey('$createdAt', $document); - // $this->assertArrayNotHasKey('$updatedAt', $document); $this->assertArrayHasKey('name', $document); $this->assertArrayHasKey('year', $document); - $this->assertArrayNotHasKey('director', $document); $this->assertArrayNotHasKey('price', $document); $this->assertArrayNotHasKey('active', $document); + $this->assertArrayNotHasKey('director', $document); + $this->assertArrayNotHasKey('$id', $document); + $this->assertArrayNotHasKey('$internalId', $document); + $this->assertArrayNotHasKey('$collection', $document); + $this->assertArrayNotHasKey('$createdAt', $document); + $this->assertArrayNotHasKey('$updatedAt', $document); + $this->assertArrayNotHasKey('$permissions', $document); } } From c4c6e2aad9257c39e87932d7795e1aeb52233fa1 Mon Sep 17 00:00:00 2001 From: faisalill Date: Sun, 9 Jul 2023 16:50:35 +0530 Subject: [PATCH 09/16] removed mytest from composer.json --- composer.json | 4 ---- 1 file changed, 4 deletions(-) diff --git a/composer.json b/composer.json index 015335b39..c28e846a0 100755 --- a/composer.json +++ b/composer.json @@ -24,10 +24,6 @@ "Composer\\Config::disableProcessTimeout", "docker compose exec tests vendor/bin/phpunit --configuration phpunit.xml" ], - "mytest": [ - "Composer\\Config::disableProcessTimeout", - "docker compose exec tests vendor/bin/phpunit ./tests/Database/Adapter/MongoDBTest.php --stop-on-failure" - ], "lint": "./vendor/bin/pint --test", "format": "./vendor/bin/pint", "check": "./vendor/bin/phpstan analyse --level 7 src tests --memory-limit 512M", From f901305197fb3c822c2c1afb67e2d3b3a0584fb5 Mon Sep 17 00:00:00 2001 From: faisalill Date: Sun, 9 Jul 2023 22:34:58 +0530 Subject: [PATCH 10/16] added more assertions --- composer.json | 2 +- tests/Database/Base.php | 26 +++++++++++++++++++------- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/composer.json b/composer.json index c28e846a0..0c2b7b611 100755 --- a/composer.json +++ b/composer.json @@ -22,7 +22,7 @@ ], "test": [ "Composer\\Config::disableProcessTimeout", - "docker compose exec tests vendor/bin/phpunit --configuration phpunit.xml" + "docker compose exec tests vendor/bin/phpunit --configuration phpunit.xml --stop-on-failure" ], "lint": "./vendor/bin/pint --test", "format": "./vendor/bin/pint", diff --git a/tests/Database/Base.php b/tests/Database/Base.php index fb15e662f..6ee02db51 100644 --- a/tests/Database/Base.php +++ b/tests/Database/Base.php @@ -1016,7 +1016,9 @@ public function testGetDocument(Document $document): Document */ public function testGetDocumentSelect(Document $document): Document { - $document = static::getDatabase()->getDocument('documents', $document->getId(), [ + $documentId = $document->getId(); + + $document = static::getDatabase()->getDocument('documents', $documentId, [ Query::select(['string', 'integer']), ]); @@ -1029,12 +1031,22 @@ public function testGetDocumentSelect(Document $document): Document $this->assertArrayNotHasKey('boolean', $document->getAttributes()); $this->assertArrayNotHasKey('colors', $document->getAttributes()); $this->assertArrayNotHasKey('with-dash', $document->getAttributes()); - $this->assertEmpty($document->getAttribute('$id')); - $this->assertEmpty($document->getAttribute('$internalId')); - $this->assertEmpty($document->getAttribute('$collection')); - $this->assertEmpty($document->getAttribute('$permissions')); - $this->assertEmpty($document->getAttribute('$createdAt')); - $this->assertEmpty($document->getAttribute('$updatedAt')); + $this->assertArrayNotHasKey('$id', $document); + $this->assertArrayNotHasKey('$internalId', $document); + $this->assertArrayNotHasKey('$collection', $document); + $this->assertArrayNotHasKey('$permissions', $document); + $this->assertArrayNotHasKey('$createdAt', $document); + $this->assertArrayNotHasKey('$updatedAt', $document); + + $document = static::getDatabase()->getDocument('documents', $documentId, [ + Query::select(['string', 'integer', '$id', '$internalId', '$permissions', '$createdAt', '$updatedAt']), + ]); + + $this->assertArrayHasKey('$id', $document); + $this->assertArrayHasKey('$internalId', $document); + $this->assertArrayHasKey('$permissions', $document); + $this->assertArrayHasKey('$createdAt', $document); + $this->assertArrayHasKey('$updatedAt', $document); return $document; } From e61eb1349d3d23f7bc9d19c1ef03841ac23f72cf Mon Sep 17 00:00:00 2001 From: faisalill Date: Wed, 12 Jul 2023 22:13:31 +0530 Subject: [PATCH 11/16] fixed mongodb and postgres issue --- composer.json | 2 +- src/Database/Adapter/Mongo.php | 20 ++++++++++++++++++++ src/Database/Adapter/Postgres.php | 6 ++++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 0c2b7b611..c28e846a0 100755 --- a/composer.json +++ b/composer.json @@ -22,7 +22,7 @@ ], "test": [ "Composer\\Config::disableProcessTimeout", - "docker compose exec tests vendor/bin/phpunit --configuration phpunit.xml --stop-on-failure" + "docker compose exec tests vendor/bin/phpunit --configuration phpunit.xml" ], "lint": "./vendor/bin/pint --test", "format": "./vendor/bin/pint", diff --git a/src/Database/Adapter/Mongo.php b/src/Database/Adapter/Mongo.php index a68440e16..e945809ed 100644 --- a/src/Database/Adapter/Mongo.php +++ b/src/Database/Adapter/Mongo.php @@ -1336,6 +1336,26 @@ protected function getAttributeProjection(array $selections, string $prefix = '' $projection['_updatedAt'] = 1; $projection['_permissions'] = 1; + if(isset($projection['$id'])){ + unset($projection['$id']); + } + + if(isset($projection['$internalId'])){ + unset($projection['$internalId']); + } + + if(isset($projection['$permissions'])){ + unset($projection['$permissions']); + } + + if(isset($projection['$createdAt'])){ + unset($projection['$createdAt']); + } + + if(isset($projection['$updatedAt'])){ + unset($projection['$updatedAt']); + } + return $projection; } diff --git a/src/Database/Adapter/Postgres.php b/src/Database/Adapter/Postgres.php index 4fa5f46b0..b18b3ff42 100644 --- a/src/Database/Adapter/Postgres.php +++ b/src/Database/Adapter/Postgres.php @@ -1224,6 +1224,12 @@ protected function getAttributeProjection(array $selections, string $prefix = '' } } + foreach ($selections as $key => $value) { + if ($value == "\"id\"" || $value == "\"internalId\"" || $value == "\"permissions\"" || $value == "\"createdAt\"" || $value == "\"updatedAt\"") { + unset($selections[$key]); + } + } + return \implode(', ', $selections); } From 0313f8c5392e0959b857575fb643420d033764b6 Mon Sep 17 00:00:00 2001 From: faisalill Date: Wed, 12 Jul 2023 22:16:25 +0530 Subject: [PATCH 12/16] fixed whitespace related issue --- src/Database/Database.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/Database/Database.php b/src/Database/Database.php index 3a342dd5d..02b70c42a 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -4350,11 +4350,9 @@ public function decode(Document $collection, Document $document, array $selectio if (empty($selections) || \in_array($key, $selections) || \in_array('*', $selections)) { if($key === '$createdAt' && $value[0] === NULL){ continue; - } - else if($key === '$updatedAt' && $value[0] === NULL){ + } else if($key === '$updatedAt' && $value[0] === NULL){ continue; - } - else{ + } else{ $document->setAttribute($key, ($array) ? $value : $value[0]); } } From 5fd6373e56dd2728dbbb3396bf1cf3a259e703a7 Mon Sep 17 00:00:00 2001 From: faisalill Date: Wed, 12 Jul 2023 23:01:30 +0530 Subject: [PATCH 13/16] fixed lint issue --- src/Database/Adapter/MariaDB.php | 1 - src/Database/Adapter/Mongo.php | 10 +++++----- src/Database/Adapter/Postgres.php | 10 +++++----- src/Database/Adapter/SQL.php | 10 +++++----- src/Database/Database.php | 26 +++++++++++++------------- 5 files changed, 28 insertions(+), 29 deletions(-) diff --git a/src/Database/Adapter/MariaDB.php b/src/Database/Adapter/MariaDB.php index 0ab801fc8..fd169c1e5 100644 --- a/src/Database/Adapter/MariaDB.php +++ b/src/Database/Adapter/MariaDB.php @@ -1046,7 +1046,6 @@ public function find(string $collection, array $queries = [], ?int $limit = 25, $results = $stmt->fetchAll(); foreach ($results as $key => $value) { - if (isset($value['_uid'])) { $results[$key]['$id'] = $value['_uid']; unset($results[$key]['_uid']); diff --git a/src/Database/Adapter/Mongo.php b/src/Database/Adapter/Mongo.php index e945809ed..8ac515d53 100644 --- a/src/Database/Adapter/Mongo.php +++ b/src/Database/Adapter/Mongo.php @@ -1336,23 +1336,23 @@ protected function getAttributeProjection(array $selections, string $prefix = '' $projection['_updatedAt'] = 1; $projection['_permissions'] = 1; - if(isset($projection['$id'])){ + if (isset($projection['$id'])) { unset($projection['$id']); } - if(isset($projection['$internalId'])){ + if (isset($projection['$internalId'])) { unset($projection['$internalId']); } - if(isset($projection['$permissions'])){ + if (isset($projection['$permissions'])) { unset($projection['$permissions']); } - if(isset($projection['$createdAt'])){ + if (isset($projection['$createdAt'])) { unset($projection['$createdAt']); } - if(isset($projection['$updatedAt'])){ + if (isset($projection['$updatedAt'])) { unset($projection['$updatedAt']); } diff --git a/src/Database/Adapter/Postgres.php b/src/Database/Adapter/Postgres.php index b18b3ff42..ead2e1cd7 100644 --- a/src/Database/Adapter/Postgres.php +++ b/src/Database/Adapter/Postgres.php @@ -1056,23 +1056,23 @@ public function find(string $collection, array $queries = [], ?int $limit = 25, $results = $stmt->fetchAll(); foreach ($results as $key => $value) { - if(isset($value['_uid'])){ + if (isset($value['_uid'])) { $results[$key]['$id'] = $value['_uid']; unset($results[$key]['_uid']); } - if(isset($value['_id'])){ + if (isset($value['_id'])) { $results[$key]['$internalId'] = $value['_id']; unset($results[$key]['_id']); } - if(isset($value['_createdAt'])){ + if (isset($value['_createdAt'])) { $results[$key]['$createdAt'] = $value['_createdAt']; unset($results[$key]['_createdAt']); } - if(isset($value['_updatedAt'])){ + if (isset($value['_updatedAt'])) { $results[$key]['$updatedAt'] = $value['_updatedAt']; unset($results[$key]['_updatedAt']); } - if(isset($value['_permissions'])){ + if (isset($value['_permissions'])) { $results[$key]['$permissions'] = json_decode($value['_permissions'] ?? '[]', true); unset($results[$key]['_permissions']); } diff --git a/src/Database/Adapter/SQL.php b/src/Database/Adapter/SQL.php index c6e73e303..3e69b6860 100644 --- a/src/Database/Adapter/SQL.php +++ b/src/Database/Adapter/SQL.php @@ -127,27 +127,27 @@ public function getDocument(string $collection, string $id, array $queries = []) return new Document([]); } - if(isset($document['_uid'])){ + if (isset($document['_uid'])) { $document['$id'] = $document['_uid']; unset($document['_uid']); } - if(isset($document['_id'])){ + if (isset($document['_id'])) { $document['$internalId'] = $document['_id']; unset($document['_id']); } - if(isset($document['_createdAt'])){ + if (isset($document['_createdAt'])) { $document['$createdAt'] = $document['_createdAt']; unset($document['_createdAt']); } - if(isset($document['_updatedAt'])){ + if (isset($document['_updatedAt'])) { $document['$updatedAt'] = $document['_updatedAt']; unset($document['_updatedAt']); } - if(isset($document['_permissions'])){ + if (isset($document['_permissions'])) { $document['$permissions'] = json_decode($document['_permissions'] ?? '[]', true); unset($document['_permissions']); } diff --git a/src/Database/Database.php b/src/Database/Database.php index 02b70c42a..73d04aeb3 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -2263,24 +2263,24 @@ public function getDocument(string $collection, string $id, array $queries = []) $this->trigger(self::EVENT_DOCUMENT_READ, $document); - foreach ($queries as $query){ - if($query->getMethod() == Query::TYPE_SELECT){ + foreach ($queries as $query) { + if ($query->getMethod() == Query::TYPE_SELECT) { $queriedValues = $query->getValues(); $defaultKeys = ['$id', '$internalId', '$permissions', '$createdAt', '$updatedAt', '$collection']; - - foreach($queriedValues as $queriedValue){ - if(in_array($queriedValue, $defaultKeys)){ + + foreach ($queriedValues as $queriedValue) { + if (in_array($queriedValue, $defaultKeys)) { $index = array_search($queriedValue, $defaultKeys); unset($defaultKeys[$index]); } } - foreach($defaultKeys as $defaultKey){ - if($document->isSet($defaultKey)){ + foreach ($defaultKeys as $defaultKey) { + if ($document->isSet($defaultKey)) { $document->removeAttribute($defaultKey); } } - } + } } return $document; @@ -4001,7 +4001,7 @@ public function find(string $collection, array $queries = [], ?int $timeout = nu // remove default keys $id and $permissions from the results foreach ($queries as $query) { - if($query->getMethod() === Query::TYPE_SELECT){ + if ($query->getMethod() === Query::TYPE_SELECT) { foreach ($results as $result) { $result->removeAttribute('$id'); $result->removeAttribute('$permissions'); @@ -4012,7 +4012,7 @@ public function find(string $collection, array $queries = [], ?int $timeout = nu } } } - + return $results; } @@ -4348,11 +4348,11 @@ public function decode(Document $collection, Document $document, array $selectio } if (empty($selections) || \in_array($key, $selections) || \in_array('*', $selections)) { - if($key === '$createdAt' && $value[0] === NULL){ + if ($key === '$createdAt' && $value[0] === null) { continue; - } else if($key === '$updatedAt' && $value[0] === NULL){ + } elseif ($key === '$updatedAt' && $value[0] === null) { continue; - } else{ + } else { $document->setAttribute($key, ($array) ? $value : $value[0]); } } From 85afec2778a63952a0bfd94e82c305498eb44ebd Mon Sep 17 00:00:00 2001 From: faisalill Date: Thu, 20 Jul 2023 22:24:33 +0530 Subject: [PATCH 14/16] Made changes as per review --- src/Database/Adapter/MariaDB.php | 22 +++++++++------------- src/Database/Adapter/Mongo.php | 8 ++++---- src/Database/Adapter/Postgres.php | 14 ++++++++------ src/Database/Adapter/SQL.php | 4 ---- src/Database/Database.php | 27 +++++++++++++++------------ tests/Database/Base.php | 6 +++--- 6 files changed, 39 insertions(+), 42 deletions(-) diff --git a/src/Database/Adapter/MariaDB.php b/src/Database/Adapter/MariaDB.php index fd169c1e5..75a3879ab 100644 --- a/src/Database/Adapter/MariaDB.php +++ b/src/Database/Adapter/MariaDB.php @@ -1046,27 +1046,23 @@ public function find(string $collection, array $queries = [], ?int $limit = 25, $results = $stmt->fetchAll(); foreach ($results as $key => $value) { - if (isset($value['_uid'])) { + if (array_key_exists('_uid', $value)) { $results[$key]['$id'] = $value['_uid']; unset($results[$key]['_uid']); } - - if (isset($value['_id'])) { + if (array_key_exists('_id', $value)) { $results[$key]['$internalId'] = $value['_id']; unset($results[$key]['_id']); } - - if (isset($value['_createdAt'])) { + if (array_key_exists('_createdAt', $value)) { $results[$key]['$createdAt'] = $value['_createdAt']; unset($results[$key]['_createdAt']); } - - if (isset($value['_updatedAt'])) { + if (array_key_exists('_updatedAt', $value)) { $results[$key]['$updatedAt'] = $value['_updatedAt']; unset($results[$key]['_updatedAt']); } - - if (isset($value['_permissions'])) { + if (array_key_exists('_permissions', $value)) { $results[$key]['$permissions'] = json_decode($value['_permissions'] ?? '[]', true); unset($results[$key]['_permissions']); } @@ -1206,26 +1202,26 @@ protected function getAttributeProjection(array $selections, string $prefix = '' $index = array_search('$id', $selections); $selections[$index] = '_uid'; } - if (in_array('$internalId', $selections)) { $index = array_search('$internalId', $selections); $selections[$index] = '_id'; } - if (in_array('$createdAt', $selections)) { $index = array_search('$createdAt', $selections); $selections[$index] = '_createdAt'; } - if (in_array('$updatedAt', $selections)) { $index = array_search('$updatedAt', $selections); $selections[$index] = '_updatedAt'; } - if (in_array('$permissions', $selections)) { $index = array_search('$permissions', $selections); $selections[$index] = '_permissions'; } + if (in_array('$collection', $selections)) { + $index = array_search('$collection', $selections); + unset($selections[$index]); + } $selections[] = '_uid'; $selections[] = '_permissions'; diff --git a/src/Database/Adapter/Mongo.php b/src/Database/Adapter/Mongo.php index 8ac515d53..d059fc539 100644 --- a/src/Database/Adapter/Mongo.php +++ b/src/Database/Adapter/Mongo.php @@ -1335,26 +1335,26 @@ protected function getAttributeProjection(array $selections, string $prefix = '' $projection['_createdAt'] = 1; $projection['_updatedAt'] = 1; $projection['_permissions'] = 1; + $projection['_collection'] = 1; if (isset($projection['$id'])) { unset($projection['$id']); } - if (isset($projection['$internalId'])) { unset($projection['$internalId']); } - if (isset($projection['$permissions'])) { unset($projection['$permissions']); } - if (isset($projection['$createdAt'])) { unset($projection['$createdAt']); } - if (isset($projection['$updatedAt'])) { unset($projection['$updatedAt']); } + if (isset($projection['$collection'])) { + unset($projection['$collection']); + } return $projection; } diff --git a/src/Database/Adapter/Postgres.php b/src/Database/Adapter/Postgres.php index ead2e1cd7..b20169132 100644 --- a/src/Database/Adapter/Postgres.php +++ b/src/Database/Adapter/Postgres.php @@ -1056,23 +1056,23 @@ public function find(string $collection, array $queries = [], ?int $limit = 25, $results = $stmt->fetchAll(); foreach ($results as $key => $value) { - if (isset($value['_uid'])) { + if (array_key_exists('_uid', $value)) { $results[$key]['$id'] = $value['_uid']; unset($results[$key]['_uid']); } - if (isset($value['_id'])) { + if (array_key_exists('_id', $value)) { $results[$key]['$internalId'] = $value['_id']; unset($results[$key]['_id']); } - if (isset($value['_createdAt'])) { + if (array_key_exists('_createdAt', $value)) { $results[$key]['$createdAt'] = $value['_createdAt']; unset($results[$key]['_createdAt']); } - if (isset($value['_updatedAt'])) { + if (array_key_exists('_updatedAt', $value)) { $results[$key]['$updatedAt'] = $value['_updatedAt']; unset($results[$key]['_updatedAt']); } - if (isset($value['_permissions'])) { + if (array_key_exists('_permissions', $value)) { $results[$key]['$permissions'] = json_decode($value['_permissions'] ?? '[]', true); unset($results[$key]['_permissions']); } @@ -1224,8 +1224,10 @@ protected function getAttributeProjection(array $selections, string $prefix = '' } } + $stringsToRemove = ["\"id\"", "\"internalId\"", "\"permissions\"", "\"createdAt\"", "\"updatedAt\"", "\"collection\""]; + foreach ($selections as $key => $value) { - if ($value == "\"id\"" || $value == "\"internalId\"" || $value == "\"permissions\"" || $value == "\"createdAt\"" || $value == "\"updatedAt\"") { + if (in_array($value, $stringsToRemove)) { unset($selections[$key]); } } diff --git a/src/Database/Adapter/SQL.php b/src/Database/Adapter/SQL.php index 3e69b6860..c42ccb7a7 100644 --- a/src/Database/Adapter/SQL.php +++ b/src/Database/Adapter/SQL.php @@ -131,22 +131,18 @@ public function getDocument(string $collection, string $id, array $queries = []) $document['$id'] = $document['_uid']; unset($document['_uid']); } - if (isset($document['_id'])) { $document['$internalId'] = $document['_id']; unset($document['_id']); } - if (isset($document['_createdAt'])) { $document['$createdAt'] = $document['_createdAt']; unset($document['_createdAt']); } - if (isset($document['_updatedAt'])) { $document['$updatedAt'] = $document['_updatedAt']; unset($document['_updatedAt']); } - if (isset($document['_permissions'])) { $document['$permissions'] = json_decode($document['_permissions'] ?? '[]', true); unset($document['_permissions']); diff --git a/src/Database/Database.php b/src/Database/Database.php index 73d04aeb3..3658b7231 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -121,6 +121,16 @@ class Database public const EVENT_INDEX_CREATE = 'index_create'; public const EVENT_INDEX_DELETE = 'index_delete'; + // Internal Parameters + public const INTERNAL_PARAMETERS = [ + '$id', + '$internalId', + '$createdAt', + '$updatedAt', + '$permissions', + '$collection' + ]; + protected Adapter $adapter; protected Cache $cache; @@ -2266,7 +2276,7 @@ public function getDocument(string $collection, string $id, array $queries = []) foreach ($queries as $query) { if ($query->getMethod() == Query::TYPE_SELECT) { $queriedValues = $query->getValues(); - $defaultKeys = ['$id', '$internalId', '$permissions', '$createdAt', '$updatedAt', '$collection']; + $defaultKeys = Database::INTERNAL_PARAMETERS; foreach ($queriedValues as $queriedValue) { if (in_array($queriedValue, $defaultKeys)) { @@ -4003,12 +4013,9 @@ public function find(string $collection, array $queries = [], ?int $timeout = nu foreach ($queries as $query) { if ($query->getMethod() === Query::TYPE_SELECT) { foreach ($results as $result) { - $result->removeAttribute('$id'); - $result->removeAttribute('$permissions'); - $result->removeAttribute('$collection'); - $result->removeAttribute('$createdAt'); - $result->removeAttribute('$updatedAt'); - $result->removeAttribute('$internalId'); + foreach (Database::INTERNAL_PARAMETERS as $parameter) { + $result->removeAttribute($parameter); + } } } } @@ -4512,11 +4519,7 @@ private function validateSelections(Document $collection, array $queries): array } } - $keys[] = '$id'; - $keys[] = '$internalId'; - $keys[] = '$createdAt'; - $keys[] = '$updatedAt'; - $keys[] = '$permissions'; + $keys = \array_merge($keys, Database::INTERNAL_PARAMETERS); $invalid = \array_diff($selections, $keys); if (!empty($invalid) && !\in_array('*', $invalid)) { diff --git a/tests/Database/Base.php b/tests/Database/Base.php index c74686d48..ceec1ca1b 100644 --- a/tests/Database/Base.php +++ b/tests/Database/Base.php @@ -1039,7 +1039,7 @@ public function testGetDocumentSelect(Document $document): Document $this->assertArrayNotHasKey('$updatedAt', $document); $document = static::getDatabase()->getDocument('documents', $documentId, [ - Query::select(['string', 'integer', '$id', '$internalId', '$permissions', '$createdAt', '$updatedAt']), + Query::select(['string', 'integer', '$id', '$internalId', '$permissions', '$createdAt', '$updatedAt', '$collection']), ]); $this->assertArrayHasKey('$id', $document); @@ -1047,6 +1047,7 @@ public function testGetDocumentSelect(Document $document): Document $this->assertArrayHasKey('$permissions', $document); $this->assertArrayHasKey('$createdAt', $document); $this->assertArrayHasKey('$updatedAt', $document); + $this->assertArrayHasKey('$collection', $document); return $document; } @@ -4178,11 +4179,10 @@ public function testOneToOneOneWayRelationship(): void Query::select(['*', 'library.name', '$id']) ]); + $this->assertArrayHasKey('$id', $person); $this->assertEquals('Library 1', $person->getAttribute('library')->getAttribute('name')); $this->assertArrayNotHasKey('area', $person->getAttribute('library')); - - $document = static::getDatabase()->getDocument('person', $person->getId(), [ Query::select(['name']), ]); From a2d25c4c56d406b52e78194ce681e36d36b7665d Mon Sep 17 00:00:00 2001 From: faisalill Date: Mon, 24 Jul 2023 19:57:18 +0530 Subject: [PATCH 15/16] Fix code as per review Combine $createdAt and $updatedAt to in_array Use static internal parameteres array rather than $stringToRemove --- src/Database/Adapter/Postgres.php | 7 +++++-- src/Database/Database.php | 6 ++---- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/Database/Adapter/Postgres.php b/src/Database/Adapter/Postgres.php index b20169132..541a78f48 100644 --- a/src/Database/Adapter/Postgres.php +++ b/src/Database/Adapter/Postgres.php @@ -1224,10 +1224,13 @@ protected function getAttributeProjection(array $selections, string $prefix = '' } } - $stringsToRemove = ["\"id\"", "\"internalId\"", "\"permissions\"", "\"createdAt\"", "\"updatedAt\"", "\"collection\""]; + $parameterNames = \array_map(function ($value) { + return \ltrim($value, '$'); + }, Database::INTERNAL_PARAMETERS); foreach ($selections as $key => $value) { - if (in_array($value, $stringsToRemove)) { + $valueUnquoted = \trim(\stripslashes($value), "\""); + if (\in_array($valueUnquoted, $parameterNames)) { unset($selections[$key]); } } diff --git a/src/Database/Database.php b/src/Database/Database.php index 9c18ff4e6..18356cbaa 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -4377,10 +4377,8 @@ public function decode(Document $collection, Document $document, array $selectio } } - if (empty($selections) || \in_array($key, $selections) || \in_array('*', $selections)) { - if ($key === '$createdAt' && $value[0] === null) { - continue; - } elseif ($key === '$updatedAt' && $value[0] === null) { + if (empty($selections) || \in_array($key, $selections) || \in_array('*', $selections) || \in_array($key, ['$createdAt', '$updatedAt'])) { + if (($key === '$createdAt' || $key === '$updatedAt') && $value[0] === null) { continue; } else { $document->setAttribute($key, ($array) ? $value : $value[0]); From e480ad8f74fb22f6967b0c3b1f26279422fec02b Mon Sep 17 00:00:00 2001 From: faisalill Date: Wed, 26 Jul 2023 22:01:34 +0530 Subject: [PATCH 16/16] Make changes as per review --- composer.json | 4 +++ src/Database/Adapter/MariaDB.php | 4 +-- src/Database/Adapter/Mongo.php | 4 +-- src/Database/Adapter/Postgres.php | 7 ++-- src/Database/Database.php | 26 ++++++++------ tests/Database/Base.php | 57 ++++++++++++++++++++++++++++++- 6 files changed, 80 insertions(+), 22 deletions(-) diff --git a/composer.json b/composer.json index c28e846a0..015335b39 100755 --- a/composer.json +++ b/composer.json @@ -24,6 +24,10 @@ "Composer\\Config::disableProcessTimeout", "docker compose exec tests vendor/bin/phpunit --configuration phpunit.xml" ], + "mytest": [ + "Composer\\Config::disableProcessTimeout", + "docker compose exec tests vendor/bin/phpunit ./tests/Database/Adapter/MongoDBTest.php --stop-on-failure" + ], "lint": "./vendor/bin/pint --test", "format": "./vendor/bin/pint", "check": "./vendor/bin/phpstan analyse --level 7 src tests --memory-limit 512M", diff --git a/src/Database/Adapter/MariaDB.php b/src/Database/Adapter/MariaDB.php index 75a3879ab..52403ad10 100644 --- a/src/Database/Adapter/MariaDB.php +++ b/src/Database/Adapter/MariaDB.php @@ -1200,7 +1200,7 @@ protected function getAttributeProjection(array $selections, string $prefix = '' if (in_array('$id', $selections)) { $index = array_search('$id', $selections); - $selections[$index] = '_uid'; + unset($selections[$index]); } if (in_array('$internalId', $selections)) { $index = array_search('$internalId', $selections); @@ -1216,7 +1216,7 @@ protected function getAttributeProjection(array $selections, string $prefix = '' } if (in_array('$permissions', $selections)) { $index = array_search('$permissions', $selections); - $selections[$index] = '_permissions'; + unset($selections[$index]); } if (in_array('$collection', $selections)) { $index = array_search('$collection', $selections); diff --git a/src/Database/Adapter/Mongo.php b/src/Database/Adapter/Mongo.php index d059fc539..22c7b3f63 100644 --- a/src/Database/Adapter/Mongo.php +++ b/src/Database/Adapter/Mongo.php @@ -1331,11 +1331,9 @@ protected function getAttributeProjection(array $selections, string $prefix = '' } $projection['_uid'] = 1; - $projection['_id'] = 1; + $projection['_permissions'] = 1; $projection['_createdAt'] = 1; $projection['_updatedAt'] = 1; - $projection['_permissions'] = 1; - $projection['_collection'] = 1; if (isset($projection['$id'])) { unset($projection['$id']); diff --git a/src/Database/Adapter/Postgres.php b/src/Database/Adapter/Postgres.php index 541a78f48..43049c7ff 100644 --- a/src/Database/Adapter/Postgres.php +++ b/src/Database/Adapter/Postgres.php @@ -1224,13 +1224,10 @@ protected function getAttributeProjection(array $selections, string $prefix = '' } } - $parameterNames = \array_map(function ($value) { - return \ltrim($value, '$'); - }, Database::INTERNAL_PARAMETERS); - foreach ($selections as $key => $value) { $valueUnquoted = \trim(\stripslashes($value), "\""); - if (\in_array($valueUnquoted, $parameterNames)) { + $value = "$" . $valueUnquoted; + if (\in_array($value, Database::INTERNAL_ATTRIBUTES)) { unset($selections[$key]); } } diff --git a/src/Database/Database.php b/src/Database/Database.php index 18356cbaa..fd77739d6 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -122,7 +122,7 @@ class Database public const EVENT_INDEX_DELETE = 'index_delete'; // Internal Parameters - public const INTERNAL_PARAMETERS = [ + public const INTERNAL_ATTRIBUTES = [ '$id', '$internalId', '$createdAt', @@ -2294,12 +2294,10 @@ public function getDocument(string $collection, string $id, array $queries = []) $this->cache->save($cacheKey, $document->getArrayCopy()); } - $this->trigger(self::EVENT_DOCUMENT_READ, $document); - foreach ($queries as $query) { if ($query->getMethod() == Query::TYPE_SELECT) { $queriedValues = $query->getValues(); - $defaultKeys = Database::INTERNAL_PARAMETERS; + $defaultKeys = Database::INTERNAL_ATTRIBUTES; foreach ($queriedValues as $queriedValue) { if (in_array($queriedValue, $defaultKeys)) { @@ -2316,6 +2314,8 @@ public function getDocument(string $collection, string $id, array $queries = []) } } + $this->trigger(self::EVENT_DOCUMENT_READ, $document); + return $document; } @@ -4029,20 +4029,19 @@ public function find(string $collection, array $queries = [], ?int $timeout = nu $results = $this->applyNestedQueries($results, $nestedQueries, $relationships); - $this->trigger(self::EVENT_DOCUMENT_FIND, $results); - // remove default keys $id and $permissions from the results - foreach ($queries as $query) { if ($query->getMethod() === Query::TYPE_SELECT) { foreach ($results as $result) { - foreach (Database::INTERNAL_PARAMETERS as $parameter) { + foreach (Database::INTERNAL_ATTRIBUTES as $parameter) { $result->removeAttribute($parameter); } } } } + $this->trigger(self::EVENT_DOCUMENT_FIND, $results); + return $results; } @@ -4377,8 +4376,13 @@ public function decode(Document $collection, Document $document, array $selectio } } - if (empty($selections) || \in_array($key, $selections) || \in_array('*', $selections) || \in_array($key, ['$createdAt', '$updatedAt'])) { - if (($key === '$createdAt' || $key === '$updatedAt') && $value[0] === null) { + if ( + empty($selections) + || \in_array($key, $selections) + || \in_array('*', $selections) + || \in_array($key, ['$createdAt', '$updatedAt']) + ) { + if (\in_array($key, ['$createdAt', '$updatedAt']) && $value[0] === null) { continue; } else { $document->setAttribute($key, ($array) ? $value : $value[0]); @@ -4540,7 +4544,7 @@ private function validateSelections(Document $collection, array $queries): array } } - $keys = \array_merge($keys, Database::INTERNAL_PARAMETERS); + $keys = \array_merge($keys, Database::INTERNAL_ATTRIBUTES); $invalid = \array_diff($selections, $keys); if (!empty($invalid) && !\in_array('*', $invalid)) { diff --git a/tests/Database/Base.php b/tests/Database/Base.php index 08c17c66b..f57fb2999 100644 --- a/tests/Database/Base.php +++ b/tests/Database/Base.php @@ -1039,14 +1039,69 @@ public function testGetDocumentSelect(Document $document): Document $this->assertArrayNotHasKey('$updatedAt', $document); $document = static::getDatabase()->getDocument('documents', $documentId, [ - Query::select(['string', 'integer', '$id', '$internalId', '$permissions', '$createdAt', '$updatedAt', '$collection']), + Query::select(['string', 'integer', '$id']), ]); $this->assertArrayHasKey('$id', $document); + $this->assertArrayNotHasKey('$internalId', $document); + $this->assertArrayNotHasKey('$permissions', $document); + $this->assertArrayNotHasKey('$createdAt', $document); + $this->assertArrayNotHasKey('$updatedAt', $document); + $this->assertArrayNotHasKey('$collection', $document); + + $document = static::getDatabase()->getDocument('documents', $documentId, [ + Query::select(['string', 'integer', '$internalId']), + ]); + + $this->assertArrayNotHasKey('$id', $document); $this->assertArrayHasKey('$internalId', $document); + $this->assertArrayNotHasKey('$permissions', $document); + $this->assertArrayNotHasKey('$createdAt', $document); + $this->assertArrayNotHasKey('$updatedAt', $document); + $this->assertArrayNotHasKey('$collection', $document); + + $document = static::getDatabase()->getDocument('documents', $documentId, [ + Query::select(['string', 'integer', '$permissions']), + ]); + + $this->assertArrayNotHasKey('$id', $document); + $this->assertArrayNotHasKey('$internalId', $document); $this->assertArrayHasKey('$permissions', $document); + $this->assertArrayNotHasKey('$createdAt', $document); + $this->assertArrayNotHasKey('$updatedAt', $document); + $this->assertArrayNotHasKey('$collection', $document); + + $document = static::getDatabase()->getDocument('documents', $documentId, [ + Query::select(['string', 'integer', '$createdAt']), + ]); + + $this->assertArrayNotHasKey('$id', $document); + $this->assertArrayNotHasKey('$internalId', $document); + $this->assertArrayNotHasKey('$permissions', $document); $this->assertArrayHasKey('$createdAt', $document); + $this->assertArrayNotHasKey('$updatedAt', $document); + $this->assertArrayNotHasKey('$collection', $document); + + $document = static::getDatabase()->getDocument('documents', $documentId, [ + Query::select(['string', 'integer', '$updatedAt']), + ]); + + $this->assertArrayNotHasKey('$id', $document); + $this->assertArrayNotHasKey('$internalId', $document); + $this->assertArrayNotHasKey('$permissions', $document); + $this->assertArrayNotHasKey('$createdAt', $document); $this->assertArrayHasKey('$updatedAt', $document); + $this->assertArrayNotHasKey('$collection', $document); + + $document = static::getDatabase()->getDocument('documents', $documentId, [ + Query::select(['string', 'integer', '$collection']), + ]); + + $this->assertArrayNotHasKey('$id', $document); + $this->assertArrayNotHasKey('$internalId', $document); + $this->assertArrayNotHasKey('$permissions', $document); + $this->assertArrayNotHasKey('$createdAt', $document); + $this->assertArrayNotHasKey('$updatedAt', $document); $this->assertArrayHasKey('$collection', $document); return $document;