Skip to content

Commit 1d4d7d1

Browse files
committed
Add attribute for update and create
1 parent 2ecf187 commit 1d4d7d1

File tree

4 files changed

+50
-21
lines changed

4 files changed

+50
-21
lines changed

src/Database/Adapter/SQL.php

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,12 @@ public function getDocument(string $collection, string $id, array $queries = [],
397397
unset($document['_permissions']);
398398
}
399399

400+
$document['main::$permissions'] = json_decode($document['main::$permissions'], true);
401+
402+
if ($this->sharedTables) {
403+
$document['main::$tenant'] = $document['main::$tenant'] === null ? null : (int)$document['main::$tenant'];
404+
}
405+
400406
return new Document($document);
401407
}
402408

@@ -1720,21 +1726,10 @@ protected function getAttributeProjection(array $selections, string $prefix): mi
17201726
return "{$this->quote($prefix)}.* {$this->addHiddenAttribute($selections)}";
17211727
}
17221728

1723-
$internalKeys = [
1724-
'$id',
1725-
'$sequence',
1726-
'$permissions',
1727-
'$createdAt',
1728-
'$updatedAt',
1729-
];
1730-
1731-
$selections = \array_diff($selections, [...$internalKeys, '$collection']);
1732-
1733-
foreach ($internalKeys as $internalKey) {
1734-
$selections[] = $this->getInternalKeyForAttribute($internalKey);
1735-
}
1729+
$selections = array_diff($selections, ['$collection']);
17361730

17371731
foreach ($selections as &$selection) {
1732+
$selection = $this->getInternalKeyForAttribute($selection);
17381733
$selection = "{$this->quote($prefix)}.{$this->quote($this->filter($selection))}";
17391734
}
17401735

src/Database/Database.php

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3296,7 +3296,7 @@ public function getDocument(string $collection, string $id, array $queries = [],
32963296
if ($collection->getId() !== self::METADATA) {
32973297
if (!$validator->isValid([
32983298
...$collection->getRead(),
3299-
...($documentSecurity ? $document->getRead() : [])
3299+
...($documentSecurity ? $document->getRead('main::$permissions') : [])
33003300
])) {
33013301
return new Document();
33023302
}
@@ -3323,7 +3323,7 @@ public function getDocument(string $collection, string $id, array $queries = [],
33233323
if ($collection->getId() !== self::METADATA) {
33243324
if (!$validator->isValid([
33253325
...$collection->getRead(),
3326-
...($documentSecurity ? $document->getRead() : [])
3326+
...($documentSecurity ? $document->getRead('main::$permissions') : [])
33273327
])) {
33283328
return new Document();
33293329
}
@@ -3686,6 +3686,13 @@ public function createDocument(string $collection, Document $document): Document
36863686

36873687
$this->trigger(self::EVENT_DOCUMENT_CREATE, $document);
36883688

3689+
$document->setAttribute('main::$id', $document->getId());
3690+
$document->setAttribute('main::$sequence', $document->getSequence());
3691+
$document->setAttribute('main::$permissions', $document->getPermissions());
3692+
$document->setAttribute('main::$createdAt', $document->getCreatedAt());
3693+
$document->setAttribute('main::$updatedAt', $document->getUpdatedAt());
3694+
$document->setAttribute('main::$tenant', $document->getTenant());
3695+
36893696
return $document;
36903697
}
36913698

@@ -4133,6 +4140,10 @@ public function updateDocument(string $collection, string $id, Document $documen
41334140

41344141
$document = $this->withTransaction(function () use ($collection, $id, $document) {
41354142
$time = DateTime::now();
4143+
4144+
/**
4145+
* @var $old Document
4146+
*/
41364147
$old = Authorization::skip(fn () => $this->silent(
41374148
fn () => $this->getDocument($collection->getId(), $id, forUpdate: true)
41384149
));
@@ -4162,8 +4173,14 @@ public function updateDocument(string $collection, string $id, Document $documen
41624173
$relationships[$relationship->getAttribute('key')] = $relationship;
41634174
}
41644175

4176+
$alias = Query::DEFAULT_ALIAS;
4177+
41654178
// Compare if the document has any changes
41664179
foreach ($document as $key => $value) {
4180+
if (str_starts_with($key, $alias.'::') && Database::isInternalAttribute($key)){
4181+
continue;
4182+
}
4183+
41674184
// Skip the nested documents as they will be checked later in recursions.
41684185
if (\array_key_exists($key, $relationships)) {
41694186
// No need to compare nested documents more than max depth.
@@ -4211,6 +4228,9 @@ public function updateDocument(string $collection, string $id, Document $documen
42114228

42124229
if (\count($old->getAttribute($key)) !== \count($value)) {
42134230
$shouldUpdate = true;
4231+
var_dump('$shouldUpdate 1');
4232+
var_dump($shouldUpdate);
4233+
42144234
break;
42154235
}
42164236

@@ -4224,6 +4244,8 @@ public function updateDocument(string $collection, string $id, Document $documen
42244244
($relation instanceof Document && $relation->getId() !== $oldValue)
42254245
) {
42264246
$shouldUpdate = true;
4247+
var_dump('$shouldUpdate 2');
4248+
var_dump($shouldUpdate);
42274249
break;
42284250
}
42294251
}
@@ -4307,6 +4329,16 @@ public function updateDocument(string $collection, string $id, Document $documen
43074329

43084330
$this->trigger(self::EVENT_DOCUMENT_UPDATE, $document);
43094331

4332+
/**
4333+
* Make this smarter
4334+
*/
4335+
$document->setAttribute('main::$id', $document->getId());
4336+
$document->setAttribute('main::$sequence', $document->getSequence());
4337+
$document->setAttribute('main::$permissions', $document->getPermissions());
4338+
$document->setAttribute('main::$createdAt', $document->getCreatedAt());
4339+
$document->setAttribute('main::$updatedAt', $document->getUpdatedAt());
4340+
$document->setAttribute('main::$tenant', $document->getTenant());
4341+
43104342
return $document;
43114343
}
43124344

src/Database/Document.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,17 +80,17 @@ public function getCollection(): string
8080
/**
8181
* @return array<string>
8282
*/
83-
public function getPermissions(): array
83+
public function getPermissions(string $attribute = '$permissions'): array
8484
{
85-
return \array_values(\array_unique($this->getAttribute('$permissions', [])));
85+
return \array_values(\array_unique($this->getAttribute($attribute, [])));
8686
}
8787

8888
/**
8989
* @return array<string>
9090
*/
91-
public function getRead(): array
91+
public function getRead(string $attribute = '$permissions'): array
9292
{
93-
return $this->getPermissionsByType(Database::PERMISSION_READ);
93+
return $this->getPermissionsByType(Database::PERMISSION_READ, $attribute);
9494
}
9595

9696
/**
@@ -132,11 +132,11 @@ public function getWrite(): array
132132
/**
133133
* @return array<string>
134134
*/
135-
public function getPermissionsByType(string $type): array
135+
public function getPermissionsByType(string $type, string $attribute = '$permissions'): array
136136
{
137137
$typePermissions = [];
138138

139-
foreach ($this->getPermissions() as $permission) {
139+
foreach ($this->getPermissions($attribute) as $permission) {
140140
if (!\str_starts_with($permission, $type)) {
141141
continue;
142142
}

tests/e2e/Adapter/Scopes/RelationshipTests.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -751,10 +751,12 @@ public function testNoChangeUpdateDocumentWithRelationWithoutPermission(): void
751751
]));
752752

753753
var_dump($level1);
754+
754755
//$level1 = $database->getDocument('level1', $level1->getId());
755756

756757
$database->updateDocument('level1', $level1->getId(), new Document($level1->getArrayCopy()));
757758
$updatedLevel1 = $database->getDocument('level1', $level1->getId());
759+
758760
$this->assertEquals($level1, $updatedLevel1);
759761

760762
try {

0 commit comments

Comments
 (0)