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

77 lines
2.2 KiB
PHP
Raw Normal View History

2022-08-22 14:14:40 +00:00
<?php
namespace Appwrite\Utopia\Database\Validator\Queries;
use Appwrite\Extend\Exception;
2023-04-25 11:35:49 +00:00
use Utopia\Database\Validator\Queries;
use Utopia\Database\Validator\Query\Limit;
use Utopia\Database\Validator\Query\Offset;
use Utopia\Database\Validator\Query\Cursor;
use Utopia\Database\Validator\Query\Filter;
use Utopia\Database\Validator\Query\Order;
2022-08-22 14:14:40 +00:00
use Utopia\Config\Config;
use Utopia\Database\Database;
use Utopia\Database\Document;
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['console'], $config['projects'], $config['buckets'], $config['databases']);
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'];
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(),
new Filter($attributes),
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
}