Skip to content

Commit

Permalink
refact: improve queryBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
JoaoFerrazfs committed Aug 25, 2023
1 parent 2d86b5c commit d13b7a0
Show file tree
Hide file tree
Showing 7 changed files with 212 additions and 114 deletions.
28 changes: 21 additions & 7 deletions src/DataMapper/DataMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use InvalidArgumentException;
use MongoDB\Collection;
use Mongolid\Connection\Connection;
use Mongolid\Container\Container;
use Mongolid\Cursor\CursorInterface;
use Mongolid\Cursor\EagerLoadedCursor;
Expand All @@ -12,10 +13,9 @@
use Mongolid\Event\EventTriggerService;
use Mongolid\Model\Exception\ModelNotFoundException;
use Mongolid\Model\ModelInterface;
use Mongolid\Query\Resolver;
use Mongolid\Schema\HasSchemaInterface;
use Mongolid\Schema\Schema;
use Mongolid\Connection\Connection;
Use Mongolid\Util\QueryBuilder;

/**
* The DataMapper class will abstract how an Entity is persisted and retrieved
Expand All @@ -25,7 +25,7 @@
*/
class DataMapper implements HasSchemaInterface
{
public bool $withTrashed = false;
private bool $ignoreSoftDelete = false;

/**
* Name of the schema class to be used.
Expand Down Expand Up @@ -261,9 +261,11 @@ public function where(

$model = new $this->schema->entityClass;

$query = $this->withTrashed ?
QueryBuilder::prepareValueForQueryCompatibility($query) :
QueryBuilder::prepareValueForSoftDeleteCompatibility($query, $model);
$query = Resolver::resolveQuery(
$query,
$model,
$this->ignoreSoftDelete
);

return new $cursorClass(
$this->schema,
Expand Down Expand Up @@ -309,8 +311,13 @@ public function first(

$model = new $this->schema->entityClass;

$query = Resolver::resolveQuery(
$query,
$model,
);

$document = $this->getCollection()->findOne(
QueryBuilder::prepareValueForSoftDeleteCompatibility($query, $model),
$query,
['projection' => $this->prepareProjection($projection)]
);

Expand Down Expand Up @@ -347,6 +354,13 @@ public function firstOrFail(
throw (new ModelNotFoundException())->setModel($this->schema->entityClass);
}

public function withoutSoftDelete(): self
{
$this->ignoreSoftDelete = true;

return $this;
}

/**
* Parses an object with SchemaMapper and the given Schema.
*
Expand Down
29 changes: 10 additions & 19 deletions src/Model/SoftDeleteTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@
use DateTime;
use MongoDB\BSON\UTCDateTime;
use Mongolid\Cursor\CursorInterface;
use Mongolid\Util\QueryBuilder;
use Mongolid\Query\Resolver;

trait SoftDeleteTrait
{
public bool $isSoftDeleteEnabled = true;

public function isTrashed(): bool
{
return !is_null($this->{QueryBuilder::getDeletedAtColumn($this)});
return !is_null($this->{Resolver::getDeletedAtColumn($this)});
}

public function restore(): bool
{
$collumn = QueryBuilder::getDeletedAtColumn($this);
$collumn = Resolver::getDeletedAtColumn($this);

if (!$this->isTrashed()) {
return false;
Expand All @@ -36,7 +36,7 @@ public function forceDelete(): bool

public function executeSoftDelete(): bool
{
$deletedAtColumn = QueryBuilder::getDeletedAtColumn($this);
$deletedAtColumn = Resolver::getDeletedAtColumn($this);
$this->$deletedAtColumn = new UTCDateTime(new DateTime('now'));

return $this->update();
Expand All @@ -52,25 +52,16 @@ public static function withTrashed(

private static function searchWithDataMapper(mixed $query, array $projection, bool $useCache): CursorInterface
{
$mapper = self::getDataMapperInstance();

$mapper->withTrashed = true;

return $mapper->where($query, $projection, $useCache);
return self::getDataMapperInstance()
->withoutSoftDelete()
->where($query, $projection, $useCache);
}

private static function searchWithBuilder(mixed $query, array $projection, bool $useCache): CursorInterface
{
$mapper = self::getBuilderInstance();

$mapper->withTrashed = true;

return $mapper->where(
new static(),
$query,
$projection,
$useCache
);
return self::getBuilderInstance()
->withoutSoftDelete()
->where(new static(), $query, $projection, $useCache);
}

private static function performSearch(mixed $query, array $projection, bool $useCache): CursorInterface
Expand Down
27 changes: 19 additions & 8 deletions src/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,14 @@
use Mongolid\Event\EventTriggerService;
use Mongolid\Model\Exception\ModelNotFoundException;
use Mongolid\Model\ModelInterface;
use Mongolid\Util\ObjectIdUtils;
use Mongolid\Util\QueryBuilder;

/**
* This class will abstract how a Model is persisted and retrieved
* from the database.
*/
class Builder
{
public bool $withTrashed = false;
private bool $ignoreSoftDelete = false;

/**
* Connection that is going to be used to interact with the database.
Expand Down Expand Up @@ -205,10 +203,11 @@ public function where(ModelInterface $model, $query = [], array $projection = []
{
$cursor = $useCache ? CacheableCursor::class : Cursor::class;

$query = $this->withTrashed ?
QueryBuilder::prepareValueForQueryCompatibility($query) :
QueryBuilder::prepareValueForSoftDeleteCompatibility($query, $model);

$query = Resolver::resolveQuery(
$query,
$model,
$this->ignoreSoftDelete
);
return new $cursor(
$model->getCollection(),
'find',
Expand Down Expand Up @@ -252,8 +251,13 @@ public function first(ModelInterface $model, $query = [], array $projection = []
return $this->where($model, $query, $projection, $useCache)->first();
}

$query = Resolver::resolveQuery(
$query,
$model,
);

return $model->getCollection()->findOne(
QueryBuilder::prepareValueForSoftDeleteCompatibility($query, $model),
$query,
['projection' => $this->prepareProjection($projection)],
);
}
Expand All @@ -280,6 +284,13 @@ public function firstOrFail(ModelInterface $model, $query = [], array $projectio
throw (new ModelNotFoundException())->setModel(get_class($model));
}

public function withoutSoftDelete(): self
{
$this->ignoreSoftDelete = true;

return $this;
}

/**
* Triggers an event. May return if that event had success.
*
Expand Down
39 changes: 22 additions & 17 deletions src/Util/QueryBuilder.php → src/Query/Resolver.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
<?php

namespace Mongolid\Util;
namespace Mongolid\Query;

use MongoDB\BSON\ObjectId;
use Mongolid\Model\ModelInterface;
use Mongolid\Util\ObjectIdUtils;

class QueryBuilder
class Resolver
{
public static function prepareValueForSoftDeleteCompatibility(mixed $query, ModelInterface $model): array
public static function resolveQuery(mixed $query, ModelInterface $model, bool $ignoreSoftDelete = false): array
{
$query = self::prepareValueForQueryCompatibility($query);
$query = self::prepareIdForQueryCompatibility($query);

if (!$model->isSoftDeleteEnabled ?? false) {
return $query;
}

if ($ignoreSoftDelete) {
return $query;
}

return self::addSoftDeleteFilterIfRequired($query, $model);
}
Expand All @@ -23,7 +32,7 @@ public static function getDeletedAtColumn(ModelInterface $model): string
: 'deleted_at';
}

public static function prepareValueForQueryCompatibility(mixed $query): array
private static function prepareIdForQueryCompatibility(mixed $query): array
{
if (!is_array($query)) {
$query = ['_id' => $query];
Expand All @@ -49,18 +58,14 @@ public static function prepareValueForQueryCompatibility(mixed $query): array

private static function addSoftDeleteFilterIfRequired(array $query, ModelInterface $model): array
{
if ($model->isSoftDeleteEnabled) {
$field = self::getDeletedAtColumn($model);

return array_merge(
$query,
[
$field => ['$exists' => false],
]
);
}

return $query;
$field = self::getDeletedAtColumn($model);

return array_merge(
$query,
[
$field => ['$exists' => false],
]
);
}

private static function convertStringIdsToObjectIds(array $query): array
Expand Down
2 changes: 0 additions & 2 deletions tests/Integration/PersistLegacyModelWithSoftDeleteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@

final class PersistLegacyModelWithSoftDeleteTest extends IntegrationTestCase
{
private ObjectId $_id;

public function testShouldFindUndeletedProduct(): void
{
// Set
Expand Down
Loading

0 comments on commit d13b7a0

Please sign in to comment.