Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/Database/Adapter/MariaDB.php
Original file line number Diff line number Diff line change
Expand Up @@ -945,10 +945,10 @@ public function deleteDocument(string $collection, string $id): bool
* @param array<string> $orderTypes
* @param array<string, mixed> $cursor
* @param string $cursorDirection
*
* @param int|null $timeout
* @return array<Document>
* @throws Exception
* @throws PDOException
* @throws DatabaseException
* @throws Timeout
*/
public function find(string $collection, array $queries = [], ?int $limit = 25, ?int $offset = null, array $orderAttributes = [], array $orderTypes = [], array $cursor = [], string $cursorDirection = Database::CURSOR_AFTER, ?int $timeout = null): array
{
Expand All @@ -959,6 +959,7 @@ public function find(string $collection, array $queries = [], ?int $limit = 25,

$orderAttributes = \array_map(fn ($orderAttribute) => match ($orderAttribute) {
'$id' => '_uid',
'$internalId' => '_id',
'$createdAt' => '_createdAt',
'$updatedAt' => '_updatedAt',
default => $orderAttribute
Expand Down Expand Up @@ -1071,6 +1072,7 @@ public function find(string $collection, array $queries = [], ?int $limit = 25,

$attribute = match ($attribute) {
'_uid' => '$id',
'_id' => '$internalId',
'_createdAt' => '$createdAt',
'_updatedAt' => '$updatedAt',
default => $attribute
Expand Down Expand Up @@ -1301,6 +1303,7 @@ protected function getSQLCondition(Query $query): string
{
$query->setAttribute(match ($query->getAttribute()) {
'$id' => '_uid',
'$internalId' => '_id',
'$createdAt' => '_createdAt',
'$updatedAt' => '_updatedAt',
default => $query->getAttribute()
Expand Down
48 changes: 25 additions & 23 deletions src/Database/Adapter/Mongo.php
Original file line number Diff line number Diff line change
Expand Up @@ -857,9 +857,10 @@ public function find(string $collection, array $queries = [], ?int $limit = 25,
$orderType = $orderType === Database::ORDER_ASC ? Database::ORDER_DESC : Database::ORDER_ASC;
}

$attribute = $attribute == 'id' ? "_uid" : $attribute;
$attribute = $attribute == 'createdAt' ? "_createdAt" : $attribute;
$attribute = $attribute == 'updatedAt' ? "_updatedAt" : $attribute;
$attribute = $attribute == 'id' ? '_uid' : $attribute;
$attribute = $attribute == 'internalId' ? '_id' : $attribute;
$attribute = $attribute == 'createdAt' ? '_createdAt' : $attribute;
$attribute = $attribute == 'updatedAt' ? '_updatedAt' : $attribute;

$options['sort'][$attribute] = $this->getOrder($orderType);
}
Expand Down Expand Up @@ -1253,33 +1254,34 @@ protected function buildFilters(array $queries): array

if ($query->getAttribute() === '$id') {
$query->setAttribute('_uid');
}

if ($query->getAttribute() === '$createdAt') {
} elseif ($query->getAttribute() === '$internalId') {
$query->setAttribute('_id');
$values = $query->getValues();
foreach ($values as &$value) {
$value = new ObjectId($value);
}
$query->setValues($values);
} elseif ($query->getAttribute() === '$createdAt') {
$query->setAttribute('_createdAt');
}

if ($query->getAttribute() === '$updatedAt') {
} elseif ($query->getAttribute() === '$updatedAt') {
$query->setAttribute('_updatedAt');
}

$attribute = $query->getAttribute();
$operator = $this->getQueryOperator($query->getMethod());

switch ($query->getMethod()) {
case Query::TYPE_IS_NULL:
case Query::TYPE_IS_NOT_NULL:
$value = null;
break;
default:
$value = $this->getQueryValue(
$query->getMethod(),
count($query->getValues()) > 1
? $query->getValues()
: $query->getValues()[0]
);
break;
}
unset($value);

$value = match ($query->getMethod()) {
Query::TYPE_IS_NULL,
Query::TYPE_IS_NOT_NULL => null,
default => $this->getQueryValue(
$query->getMethod(),
count($query->getValues()) > 1
? $query->getValues()
: $query->getValues()[0]
),
};

if ($operator == '$eq' && \is_array($value)) {
$filters[$attribute]['$in'] = $value;
Expand Down
3 changes: 3 additions & 0 deletions src/Database/Adapter/Postgres.php
Original file line number Diff line number Diff line change
Expand Up @@ -972,6 +972,7 @@ public function find(string $collection, array $queries = [], ?int $limit = 25,

$orderAttributes = \array_map(fn ($orderAttribute) => match ($orderAttribute) {
'$id' => '_uid',
'$internalId' => '_id',
'$createdAt' => '_createdAt',
'$updatedAt' => '_updatedAt',
default => $orderAttribute
Expand Down Expand Up @@ -1077,6 +1078,7 @@ public function find(string $collection, array $queries = [], ?int $limit = 25,

$attribute = match ($attribute) {
'_uid' => '$id',
'_id' => '$internalId',
'_createdAt' => '$createdAt',
'_updatedAt' => '$updatedAt',
default => $attribute
Expand Down Expand Up @@ -1313,6 +1315,7 @@ protected function getSQLCondition(Query $query): string
{
$query->setAttribute(match ($query->getAttribute()) {
'$id' => '_uid',
'$internalId' => '_id',
'$createdAt' => '_createdAt',
'$updatedAt' => '_updatedAt',
default => $query->getAttribute()
Expand Down
8 changes: 6 additions & 2 deletions src/Database/Validator/Queries/Documents.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,18 @@ public function __construct(array $attributes, array $indexes)
'type' => Database::VAR_STRING,
'array' => false,
]);

$attributes[] = new Document([
'$id' => '$internalId',
'key' => '$internalId',
'type' => Database::VAR_STRING,
'array' => false,
]);
$attributes[] = new Document([
'$id' => '$createdAt',
'key' => '$createdAt',
'type' => Database::VAR_DATETIME,
'array' => false,
]);

$attributes[] = new Document([
'$id' => '$updatedAt',
'key' => '$updatedAt',
Expand Down
31 changes: 28 additions & 3 deletions tests/Database/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -1477,8 +1477,10 @@ public function testDeleteDocument(Document $document): void
}



public function testFind(): void
/**
* @return array<string, mixed>
*/
public function testFind(): array
{
Authorization::setRole(Role::any()->toString());
static::getDatabase()->createCollection('movies', permissions: [
Expand All @@ -1495,7 +1497,7 @@ public function testFind(): void
$this->assertEquals(true, static::getDatabase()->createAttribute('movies', 'with-dash', Database::VAR_STRING, 128, true));
$this->assertEquals(true, static::getDatabase()->createAttribute('movies', 'nullable', Database::VAR_STRING, 128, false));

static::getDatabase()->createDocument('movies', new Document([
$document = static::getDatabase()->createDocument('movies', new Document([
'$id' => ID::custom('frozen'),
'$permissions' => [
Permission::read(Role::any()),
Expand Down Expand Up @@ -1638,6 +1640,10 @@ public function testFind(): void
'with-dash' => 'Works3',
'nullable' => 'Not null'
]));

return [
'$internalId' => $document->getInternalId()
];
}

public function testFindBasicChecks(): void
Expand Down Expand Up @@ -1980,6 +1986,25 @@ public function testFindByID(): void
$this->assertEquals('Frozen', $documents[0]['name']);
}


/**
* @depends testFind
* @param array<string, mixed> $data
* @return void
* @throws \Utopia\Database\Exception
*/
public function testFindByInternalID(array $data): void
{
/**
* Test that internal ID queries are handled correctly
*/
$documents = static::getDatabase()->find('movies', [
Query::equal('$internalId', [$data['$internalId']]),
]);

$this->assertEquals(1, count($documents));
}

public function testFindOrderBy(): void
{
/**
Expand Down