address comments.

This commit is contained in:
Darshan 2025-05-03 09:59:04 +05:30
parent a3db080fcf
commit 29647a9dc8
2 changed files with 47 additions and 28 deletions

View file

@ -7,7 +7,7 @@ use Utopia\Route;
abstract class Filter abstract class Filter
{ {
private ?Route $route; protected ?Route $route;
private ?Database $dbForProject; private ?Database $dbForProject;
public function __construct(Database $dbForProject = null, Route $route = null) public function __construct(Database $dbForProject = null, Route $route = null)
@ -26,16 +26,6 @@ abstract class Filter
*/ */
abstract public function parse(array $content, string $model): array; abstract public function parse(array $content, string $model): array;
/**
* Get the current route context.
*
* @return null|Route
*/
public function getRoute(): ?Route
{
return $this->route;
}
/** /**
* Get the database for the current project. * Get the database for the current project.
* *
@ -45,4 +35,23 @@ abstract class Filter
{ {
return $this->dbForProject; return $this->dbForProject;
} }
/**
* Returns the value of the given route param key, or a default if not found or on error.
*
* @param string $key
* @param mixed $default
*
* @return mixed
*/
public function getParamValue(string $key, mixed $default = ''): mixed
{
try {
$value = $this->route?->getParamValue($key) ?? $default;
} catch (\Exception $e) {
$value = $default;
}
return $value;
}
} }

View file

@ -2,10 +2,9 @@
namespace Appwrite\Utopia\Request\Filters; namespace Appwrite\Utopia\Request\Filters;
use Appwrite\Query as AppwriteQuery;
use Appwrite\Utopia\Request\Filter; use Appwrite\Utopia\Request\Filter;
use Utopia\Database\Database; use Utopia\Database\Database;
use Utopia\Database\Query as UtopiaQuery; use Utopia\Database\Query;
use Utopia\Database\Validator\Authorization; use Utopia\Database\Validator\Authorization;
class V19 extends Filter class V19 extends Filter
@ -18,6 +17,14 @@ class V19 extends Filter
return $content; return $content;
} }
/**
* From 1.7.x onward, related documents are no longer returned by default to improve performance.
*
* Use `Query::select(['related.*'])` for full documents or `Query::select(['related.key'])` for specific fields.
*
* This filter preserves 1.6.x behavior by including all related documents for backward compatibility with
* `listDocuments` and `getDocument` calls.
*/
protected function manageSelectQueries(array $content, string $model): array protected function manageSelectQueries(array $content, string $model): array
{ {
$isDatabaseModel = \str_starts_with($model, 'databases.'); $isDatabaseModel = \str_starts_with($model, 'databases.');
@ -30,16 +37,20 @@ class V19 extends Filter
$hasWildcard = false; $hasWildcard = false;
if (! isset($content['queries'])) { if (! isset($content['queries'])) {
$hasWildcard = true; $hasWildcard = true;
$content['queries'] = [AppwriteQuery::select(['*'])]; $content['queries'] = [Query::select(['*'])];
} }
$parsed = UtopiaQuery::parseQueries($content['queries']); $parsed = Query::parseQueries($content['queries']);
$selections = UtopiaQuery::groupByType($parsed)['selections'] ?? []; $selections = Query::groupByType($parsed)['selections'] ?? [];
if (! $hasWildcard) { if (! $hasWildcard) {
// check if any select includes a wildcard as we added one above // check if any select includes a wildcard as we added one above
$hasWildcard = \array_reduce($selections, fn (bool $carry, $select) => foreach ($selections as $select) {
$carry || \in_array('*', $select->getValues(), true), false); if (\in_array('*', $select->getValues(), true)) {
$hasWildcard = true;
break;
}
}
} }
if ($hasWildcard && $model === 'databases.listDocuments') { if ($hasWildcard && $model === 'databases.listDocuments') {
@ -51,12 +62,11 @@ class V19 extends Filter
// remove previous select queries // remove previous select queries
$parsed = \array_filter( $parsed = \array_filter(
$parsed, $parsed,
fn ($query) => fn ($query) => $query->getMethod() !== Query::TYPE_SELECT
$query->getMethod() !== UtopiaQuery::TYPE_SELECT
); );
// add wildcard + relationship(s) selects // add wildcard + relationship(s) selects
$parsed[] = AppwriteQuery::select($selects); $parsed[] = Query::select($selects);
} }
} }
@ -65,18 +75,19 @@ class V19 extends Filter
return $content; return $content;
} }
/**
* Returns all relationship attribute keys in `key.*` format for use with `Query::select`.
*/
private function getRelatedCollectionKeys(): array private function getRelatedCollectionKeys(): array
{ {
$route = $this->getRoute();
$dbForProject = $this->getDbForProject(); $dbForProject = $this->getDbForProject();
if ($dbForProject === null || $route === null) { if ($dbForProject === null) {
return []; return [];
} }
$params = $route->getParamsValues(); $databaseId = $this->getParamValue('databaseId');
$databaseId = $params['databaseId'] ?? ''; $collectionId = $this->getParamValue('collectionId');
$collectionId = $params['collectionId'] ?? '';
if (empty($databaseId) || empty($collectionId)) { if (empty($databaseId) || empty($collectionId)) {
return []; return [];
@ -95,8 +106,7 @@ class V19 extends Filter
fn ($attr) => $attr['key'] . '.*', fn ($attr) => $attr['key'] . '.*',
\array_filter( \array_filter(
$attributes, $attributes,
fn ($attr) => fn ($attr) => ($attr['type'] ?? null) === Database::VAR_RELATIONSHIP
($attr['type'] ?? null) === Database::VAR_RELATIONSHIP
) )
)); ));
} }