Skip to content

Commit

Permalink
Update Database find methods to only use queries
Browse files Browse the repository at this point in the history
  • Loading branch information
stnguyen90 committed Aug 10, 2022
1 parent 97754fb commit 39e3e89
Show file tree
Hide file tree
Showing 9 changed files with 573 additions and 168 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
6 changes: 3 additions & 3 deletions src/Database/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ abstract public function deleteDocument(string $collection, string $id): bool;
* @param int $offset
* @param array $orderAttributes
* @param array $orderTypes
* @param array $cursor Array copy of document used for before/after pagination
* @param array $cursor
* @param string $cursorDirection
*
* @return Document[]
Expand Down Expand Up @@ -459,11 +459,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
2 changes: 2 additions & 0 deletions src/Database/Adapter/MariaDB.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Utopia\Database\Document;
use Utopia\Database\Exception\Duplicate;
use Utopia\Database\Query;
use Utopia\Database\Validator\Queries;
use Utopia\Database\Validator\Authorization;

class MariaDB extends Adapter
Expand Down Expand Up @@ -776,6 +777,7 @@ public function deleteDocument(string $collection, string $id): bool
* @param array $orderTypes
* @param array $cursor
* @param string $cursorDirection
*
* @return Document[]
* @throws Exception
* @throws PDOException
Expand Down
Loading

0 comments on commit 39e3e89

Please sign in to comment.