Skip to content

Commit

Permalink
Update Database and Adapter find methods to only use queries
Browse files Browse the repository at this point in the history
  • Loading branch information
stnguyen90 committed Aug 9, 2022
1 parent 97754fb commit db04fbe
Show file tree
Hide file tree
Showing 6 changed files with 459 additions and 170 deletions.
24 changes: 15 additions & 9 deletions bin/tasks/query.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
/**
* @var CLI
*/ global $cli;

use Faker\Factory;
use MongoDB\Client;
use Utopia\Cache\Cache;
Expand All @@ -17,19 +18,21 @@
use Utopia\Database\Validator\Authorization;
use Utopia\Validator\Numeric;
use Utopia\Validator\Text;

$cli
->task('query')
->desc('Query mock data')
->param('adapter', '', new Text(0), 'Database adapter', false)
->param('name', '', new Text(0), 'Name of created database.', false)
->param('limit', 25, new Numeric(), 'Limit on queried documents', true)
->action(function ($adapter, $name, $limit) {
->action(function (string $adapter, string $name, int $limit) {
$database = null;

switch ($adapter) {
case 'mongodb':
$options = ["typeMap" => ['root' => 'array', 'document' => 'array', 'array' => 'array']];
$client = new Client('mongodb://mongo/',
$client = new Client(
'mongodb://mongo/',
[
'username' => 'root',
'password' => 'example',
Expand Down Expand Up @@ -130,7 +133,8 @@
fclose($f);
});

function runQueries($database, $limit) {
function runQueries(Database $database, int $limit)
{
$results = [];
// Recent travel blogs
$query = ["created.greater(1262322000)", "genre.equal('travel')"];
Expand All @@ -151,21 +155,23 @@ function runQueries($database, $limit) {
return $results;
}

function addRoles($faker, $count) {
for ($i=0; $i < $count; $i++) {
function addRoles($faker, $count)
{
for ($i = 0; $i < $count; $i++) {
Authorization::setRole($faker->numerify('user####'));
}
return count(Authorization::getRoles());
}

function runQuery($query, $database, $limit) {
Console::log('Running query: ['.implode(', ', $query).']');
$query = array_map(function($q) {
function runQuery(array $query, Database $database, int $limit)
{
Console::log('Running query: [' . implode(', ', $query) . ']');
$query = array_map(function ($q) {
return Query::parse($q);
}, $query);

$start = microtime(true);
$documents = $database->find('articles', $query, $limit);
$database->find('articles', array_merge($query, [Query::limit($limit)]));
$time = microtime(true) - $start;
Console::success("{$time} s");
return $time;
Expand Down
12 changes: 3 additions & 9 deletions src/Database/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -314,16 +314,10 @@ abstract public function deleteDocument(string $collection, string $id): bool;
*
* @param string $collection
* @param \Utopia\Database\Query[] $queries
* @param int $limit
* @param int $offset
* @param array $orderAttributes
* @param array $orderTypes
* @param array $cursor Array copy of document used for before/after pagination
* @param string $cursorDirection
*
* @return Document[]
*/
abstract public function find(string $collection, array $queries = [], int $limit = 25, int $offset = 0, array $orderAttributes = [], array $orderTypes = [], array $cursor = [], string $cursorDirection = Database::CURSOR_AFTER): array;
abstract public function find(string $collection, array $queries = []): array;

/**
* Sum an attribute
Expand Down Expand Up @@ -459,11 +453,11 @@ abstract public function getSupportForCasting(): bool;
* @throws Exception
* @return string
*/
public function filter(string $value):string
public function filter(string $value): string
{
$value = preg_replace("/[^A-Za-z0-9\_\-]/", '', $value);

if(\is_null($value)) {
if (\is_null($value)) {
throw new Exception('Failed to filter key');
}

Expand Down
Loading

0 comments on commit db04fbe

Please sign in to comment.