appwrite/src/Appwrite/Utopia/Database/Validator/Queries/Base.php

85 lines
2.3 KiB
PHP
Raw Normal View History

2022-08-22 14:14:40 +00:00
<?php
namespace Appwrite\Utopia\Database\Validator\Queries;
2024-03-06 17:34:21 +00:00
use Utopia\Config\Config;
use Utopia\Database\Database;
use Utopia\Database\Document;
2023-04-25 11:35:49 +00:00
use Utopia\Database\Validator\Queries;
use Utopia\Database\Validator\Query\Cursor;
use Utopia\Database\Validator\Query\Filter;
2024-03-06 17:34:21 +00:00
use Utopia\Database\Validator\Query\Limit;
use Utopia\Database\Validator\Query\Offset;
2023-04-25 11:35:49 +00:00
use Utopia\Database\Validator\Query\Order;
2022-08-22 14:14:40 +00:00
2022-08-24 18:23:34 +00:00
class Base extends Queries
2022-08-22 14:14:40 +00:00
{
/**
* Expression constructor
*
* @param string $collection
* @param string[] $allowedAttributes
* @throws \Exception
2022-08-22 14:14:40 +00:00
*/
public function __construct(string $collection, array $allowedAttributes)
2022-08-22 14:14:40 +00:00
{
$config = Config::getParam('collections', []);
$collections = array_merge(
$config['projects'],
$config['buckets'],
$config['databases'],
$config['console'],
2025-02-25 03:58:30 +00:00
$config['logs']
);
2023-06-15 01:23:46 +00:00
$collection = $collections[$collection];
2022-08-22 14:14:40 +00:00
// array for constant lookup time
$allowedAttributesLookup = [];
foreach ($allowedAttributes as $attribute) {
$allowedAttributesLookup[$attribute] = true;
}
$attributes = [];
foreach ($collection['attributes'] as $attribute) {
$key = $attribute['$id'];
2022-08-22 14:14:40 +00:00
if (!isset($allowedAttributesLookup[$key])) {
continue;
}
$attributes[] = new Document([
'key' => $key,
'type' => $attribute['type'],
'array' => $attribute['array'],
]);
}
2022-08-22 19:12:30 +00:00
$attributes[] = new Document([
'key' => '$id',
'type' => Database::VAR_STRING,
'array' => false,
]);
$attributes[] = new Document([
2022-08-23 15:01:57 +00:00
'key' => '$createdAt',
2022-08-22 19:12:30 +00:00
'type' => Database::VAR_DATETIME,
'array' => false,
]);
$attributes[] = new Document([
2022-08-23 15:01:57 +00:00
'key' => '$updatedAt',
2022-08-22 19:12:30 +00:00
'type' => Database::VAR_DATETIME,
'array' => false,
]);
$validators = [
new Limit(),
new Offset(),
new Cursor(),
2024-10-16 08:35:32 +00:00
new Filter($attributes, APP_DATABASE_QUERY_MAX_VALUES),
2022-08-22 19:12:30 +00:00
new Order($attributes),
];
2023-05-16 12:56:20 +00:00
parent::__construct($validators);
2022-08-22 14:14:40 +00:00
}
2022-08-22 19:12:30 +00:00
}