Add support for select queries

This commit is contained in:
Matej Bačo 2025-08-26 15:15:10 +02:00
parent 34658aac9b
commit 43f75d082e
2 changed files with 49 additions and 23 deletions

View file

@ -11,6 +11,7 @@ use Utopia\Database\Validator\Query\Filter;
use Utopia\Database\Validator\Query\Limit; use Utopia\Database\Validator\Query\Limit;
use Utopia\Database\Validator\Query\Offset; use Utopia\Database\Validator\Query\Offset;
use Utopia\Database\Validator\Query\Order; use Utopia\Database\Validator\Query\Order;
use Utopia\Database\Validator\Query\Select;
class Base extends Queries class Base extends Queries
{ {
@ -40,6 +41,7 @@ class Base extends Queries
$allowedAttributesLookup[$attribute] = true; $allowedAttributesLookup[$attribute] = true;
} }
$allAttributes = [];
$attributes = []; $attributes = [];
foreach ($collection['attributes'] as $attribute) { foreach ($collection['attributes'] as $attribute) {
$key = $attribute['$id']; $key = $attribute['$id'];
@ -47,34 +49,44 @@ class Base extends Queries
if (!isset($allowedAttributesLookup[$key])) { if (!isset($allowedAttributesLookup[$key])) {
continue; continue;
} }
$attributes[] = new Document([ $attributeDocument = new Document([
'key' => $key, 'key' => $key,
'type' => $attribute['type'], 'type' => $attribute['type'],
'array' => $attribute['array'], 'array' => $attribute['array'],
]); ]);
}
$attributes[] = new Document([ $attributes[] = $attributeDocument;
'key' => '$id', $allAttributes[] = $attributeDocument;
'type' => Database::VAR_STRING, }
'array' => false,
]); $internalAttributes = [
$attributes[] = new Document([ new Document([
'key' => '$createdAt', 'key' => '$id',
'type' => Database::VAR_DATETIME, 'type' => Database::VAR_STRING,
'array' => false, 'array' => false,
]); ]),
$attributes[] = new Document([ new Document([
'key' => '$updatedAt', 'key' => '$createdAt',
'type' => Database::VAR_DATETIME, 'type' => Database::VAR_DATETIME,
'array' => false, 'array' => false,
]); ]),
$attributes[] = new Document([ new Document([
'key' => '$sequence', 'key' => '$updatedAt',
'type' => Database::VAR_INTEGER, 'type' => Database::VAR_DATETIME,
'array' => false, 'array' => false,
]); ]),
new Document([
'key' => '$sequence',
'type' => Database::VAR_INTEGER,
'array' => false,
])
];
foreach($internalAttributes as $attribute) {
$attributes[] = $attribute;
$allAttributes[] = $attribute;
}
$validators = [ $validators = [
new Limit(), new Limit(),
@ -83,7 +95,16 @@ class Base extends Queries
new Filter($attributes, APP_DATABASE_QUERY_MAX_VALUES), new Filter($attributes, APP_DATABASE_QUERY_MAX_VALUES),
new Order($attributes), new Order($attributes),
]; ];
if($this->isSelectQueryAllowed()) {
$validators[] = new Select($allAttributes);
}
parent::__construct($validators); parent::__construct($validators);
} }
public function isSelectQueryAllowed(): bool
{
return true;
}
} }

View file

@ -22,4 +22,9 @@ class Deployments extends Base
{ {
parent::__construct('deployments', self::ALLOWED_ATTRIBUTES); parent::__construct('deployments', self::ALLOWED_ATTRIBUTES);
} }
public function isSelectQueryAllowed(): bool
{
return true;
}
} }