Skip to content

Commit e97cc6b

Browse files
authored
Merge pull request #585 from utopia-php/feat-select-internals
Re-force internal attribute selection
2 parents 490a669 + 0143a9b commit e97cc6b

File tree

5 files changed

+109
-237
lines changed

5 files changed

+109
-237
lines changed

src/Database/Adapter/MariaDB.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -128,14 +128,7 @@ public function createCollection(string $name, array $attributes = [], array $in
128128
$indexLength = $index->getAttribute('lengths')[$nested] ?? '';
129129
$indexLength = (empty($indexLength)) ? '' : '(' . (int)$indexLength . ')';
130130
$indexOrder = $index->getAttribute('orders')[$nested] ?? '';
131-
132-
$indexAttribute = match ($attribute) {
133-
'$id' => '_uid',
134-
'$createdAt' => '_createdAt',
135-
'$updatedAt' => '_updatedAt',
136-
default => $attribute
137-
};
138-
131+
$indexAttribute = $this->getInternalKeyForAttribute($attribute);
139132
$indexAttribute = $this->filter($indexAttribute);
140133

141134
if ($indexType === Database::INDEX_FULLTEXT) {

src/Database/Adapter/SQL.php

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1690,30 +1690,18 @@ protected function getAttributeProjection(array $selections, string $prefix = ''
16901690
return '*';
16911691
}
16921692

1693-
$selections = \array_diff($selections, ['$id', '$permissions', '$collection']);
1693+
$internalKeys = [
1694+
'$id',
1695+
'$internalId',
1696+
'$permissions',
1697+
'$createdAt',
1698+
'$updatedAt',
1699+
];
16941700

1695-
$selections[] = $this->getInternalKeyForAttribute('$id');
1696-
$selections[] = $this->getInternalKeyForAttribute('$permissions');
1701+
$selections = \array_diff($selections, [...$internalKeys, '$collection']);
16971702

1698-
if (\in_array('$internalId', $selections)) {
1699-
$selections[] = $this->getInternalKeyForAttribute('$internalId');
1700-
$selections = \array_diff($selections, ['$internalId']);
1701-
}
1702-
if (\in_array('$createdAt', $selections)) {
1703-
$selections[] = $this->getInternalKeyForAttribute('$createdAt');
1704-
$selections = \array_diff($selections, ['$createdAt']);
1705-
}
1706-
if (\in_array('$updatedAt', $selections)) {
1707-
$selections[] = $this->getInternalKeyForAttribute('$updatedAt');
1708-
$selections = \array_diff($selections, ['$updatedAt']);
1709-
}
1710-
if (\in_array('$collection', $selections)) {
1711-
$selections[] = $this->getInternalKeyForAttribute('$collection');
1712-
$selections = \array_diff($selections, ['$collection']);
1713-
}
1714-
if (\in_array('$tenant', $selections)) {
1715-
$selections[] = $this->getInternalKeyForAttribute('$tenant');
1716-
$selections = \array_diff($selections, ['$tenant']);
1703+
foreach ($internalKeys as $internalKey) {
1704+
$selections[] = $this->getInternalKeyForAttribute($internalKey);
17171705
}
17181706

17191707
if (!empty($prefix)) {
@@ -1726,7 +1714,7 @@ protected function getAttributeProjection(array $selections, string $prefix = ''
17261714
}
17271715
}
17281716

1729-
return \implode(', ', $selections);
1717+
return \implode(',', $selections);
17301718
}
17311719

17321720
protected function getInternalKeyForAttribute(string $attribute): string

src/Database/Database.php

Lines changed: 8 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -3330,20 +3330,6 @@ public function getDocument(string $collection, string $id, array $queries = [],
33303330
}
33313331
}
33323332

3333-
// Remove internal attributes if not queried for select query
3334-
// $id, $permissions and $collection are the default selected attributes for (MariaDB, MySQL, SQLite, Postgres)
3335-
// All internal attributes are default selected attributes for (MongoDB)
3336-
foreach ($queries as $query) {
3337-
if ($query->getMethod() === Query::TYPE_SELECT) {
3338-
$values = $query->getValues();
3339-
foreach ($this->getInternalAttributes() as $internalAttribute) {
3340-
if (!\in_array($internalAttribute['$id'], $values)) {
3341-
$document->removeAttribute($internalAttribute['$id']);
3342-
}
3343-
}
3344-
}
3345-
}
3346-
33473333
$this->trigger(self::EVENT_DOCUMENT_READ, $document);
33483334

33493335
return $document;
@@ -6049,6 +6035,7 @@ public function find(string $collection, array $queries = [], string $forPermiss
60496035
if ($this->resolveRelationships && (empty($selects) || !empty($nestedSelections))) {
60506036
$node = $this->silent(fn () => $this->populateDocumentRelationships($collection, $node, $nestedSelections));
60516037
}
6038+
60526039
$node = $this->casting($collection, $node);
60536040
$node = $this->decode($collection, $node, $selections);
60546041

@@ -6059,20 +6046,6 @@ public function find(string $collection, array $queries = [], string $forPermiss
60596046

60606047
unset($query);
60616048

6062-
// Remove internal attributes which are not queried
6063-
foreach ($queries as $query) {
6064-
if ($query->getMethod() === Query::TYPE_SELECT) {
6065-
$values = $query->getValues();
6066-
foreach ($results as $result) {
6067-
foreach ($this->getInternalAttributes() as $internalAttribute) {
6068-
if (!\in_array($internalAttribute['$id'], $values)) {
6069-
$result->removeAttribute($internalAttribute['$id']);
6070-
}
6071-
}
6072-
}
6073-
}
6074-
}
6075-
60766049
$this->trigger(self::EVENT_DOCUMENT_FIND, $results);
60776050

60786051
return $results;
@@ -6385,20 +6358,12 @@ public function decode(Document $collection, Document $document, array $selectio
63856358
}
63866359
}
63876360

6388-
if (empty($selections) || \in_array($key, $selections) || \in_array('*', $selections)) {
6389-
if (
6390-
empty($selections)
6391-
|| \in_array($key, $selections)
6392-
|| \in_array('*', $selections)
6393-
|| \in_array($key, ['$createdAt', '$updatedAt'])
6394-
) {
6395-
// Prevent null values being set for createdAt and updatedAt
6396-
if (\in_array($key, ['$createdAt', '$updatedAt']) && $value[0] === null) {
6397-
continue;
6398-
} else {
6399-
$document->setAttribute($key, ($array) ? $value : $value[0]);
6400-
}
6401-
}
6361+
if (
6362+
empty($selections)
6363+
|| \in_array($key, $selections)
6364+
|| \in_array('*', $selections)
6365+
) {
6366+
$document->setAttribute($key, ($array) ? $value : $value[0]);
64026367
}
64036368
}
64046369

@@ -6580,7 +6545,7 @@ private function validateSelections(Document $collection, array $queries): array
65806545
$selections[] = '$updatedAt';
65816546
$selections[] = '$permissions';
65826547

6583-
return $selections;
6548+
return \array_values(\array_unique($selections));
65846549
}
65856550

65866551
/**

0 commit comments

Comments
 (0)