Skip to content

Commit 19231e6

Browse files
authored
Merge pull request #486 from utopia-php/0.53.x-update-bulk-events
Update Create Documents to fire individual events
2 parents b5bd17e + 69e210d commit 19231e6

File tree

10 files changed

+49
-52
lines changed

10 files changed

+49
-52
lines changed

composer.lock

Lines changed: 28 additions & 28 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Database/Adapter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@ abstract public function createAttribute(string $collection, string $id, string
481481
*
482482
* @return bool
483483
*/
484-
abstract public function updateAttribute(string $collection, string $id, string $type, int $size, bool $signed = true, bool $array = false, string $newKey = null): bool;
484+
abstract public function updateAttribute(string $collection, string $id, string $type, int $size, bool $signed = true, bool $array = false, ?string $newKey = null): bool;
485485

486486
/**
487487
* Delete Attribute

src/Database/Adapter/MariaDB.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ public function createAttribute(string $collection, string $id, string $type, in
394394
* @throws Exception
395395
* @throws PDOException
396396
*/
397-
public function updateAttribute(string $collection, string $id, string $type, int $size, bool $signed = true, bool $array = false, string $newKey = null): bool
397+
public function updateAttribute(string $collection, string $id, string $type, int $size, bool $signed = true, bool $array = false, ?string $newKey = null): bool
398398
{
399399
$name = $this->filter($collection);
400400
$id = $this->filter($id);

src/Database/Adapter/Mongo.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public function create(string $name): bool
122122
* @return bool
123123
* @throws Exception
124124
*/
125-
public function exists(string $database, string $collection = null): bool
125+
public function exists(string $database, ?string $collection = null): bool
126126
{
127127
if (!\is_null($collection)) {
128128
$collection = $this->getNamespace() . "_" . $collection;
@@ -1007,7 +1007,7 @@ public function deleteDocuments(string $collection, array $ids): int
10071007
*
10081008
* @return bool
10091009
*/
1010-
public function updateAttribute(string $collection, string $id, string $type, int $size, bool $signed = true, bool $array = false, string $newKey = null): bool
1010+
public function updateAttribute(string $collection, string $id, string $type, int $size, bool $signed = true, bool $array = false, ?string $newKey = null): bool
10111011
{
10121012
if (!empty($newKey) && $newKey !== $id) {
10131013
return $this->renameAttribute($collection, $id, $newKey);

src/Database/Adapter/Postgres.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -478,12 +478,12 @@ public function renameAttribute(string $collection, string $old, string $new): b
478478
* @param int $size
479479
* @param bool $signed
480480
* @param bool $array
481-
* @param string $newKey
481+
* @param string|null $newKey
482482
* @return bool
483483
* @throws Exception
484484
* @throws PDOException
485485
*/
486-
public function updateAttribute(string $collection, string $id, string $type, int $size, bool $signed = true, bool $array = false, string $newKey = null): bool
486+
public function updateAttribute(string $collection, string $id, string $type, int $size, bool $signed = true, bool $array = false, ?string $newKey = null): bool
487487
{
488488
$name = $this->filter($collection);
489489
$id = $this->filter($id);

src/Database/Adapter/SQLite.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -332,12 +332,12 @@ public function analyzeCollection(string $collection): bool
332332
* @param int $size
333333
* @param bool $signed
334334
* @param bool $array
335-
* @param string $newKey
335+
* @param string|null $newKey
336336
* @return bool
337337
* @throws Exception
338338
* @throws PDOException
339339
*/
340-
public function updateAttribute(string $collection, string $id, string $type, int $size, bool $signed = true, bool $array = false, string $newKey = null): bool
340+
public function updateAttribute(string $collection, string $id, string $type, int $size, bool $signed = true, bool $array = false, ?string $newKey = null): bool
341341
{
342342
if (!empty($newKey) && $newKey !== $id) {
343343
return $this->renameAttribute($collection, $id, $newKey);

src/Database/Database.php

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,7 @@ public function before(string $event, string $name, callable $callback): static
477477
* @param array<string>|null $listeners List of listeners to silence; if null, all listeners will be silenced
478478
* @return T
479479
*/
480-
public function silent(callable $callback, array $listeners = null): mixed
480+
public function silent(callable $callback, ?array $listeners = null): mixed
481481
{
482482
$previous = $this->silentListeners;
483483

@@ -1143,14 +1143,14 @@ public function delete(?string $database = null): bool
11431143
* @param string $id
11441144
* @param array<Document> $attributes
11451145
* @param array<Document> $indexes
1146-
* @param array<string> $permissions
1146+
* @param array<string>|null $permissions
11471147
* @param bool $documentSecurity
11481148
* @return Document
11491149
* @throws DatabaseException
11501150
* @throws DuplicateException
11511151
* @throws LimitException
11521152
*/
1153-
public function createCollection(string $id, array $attributes = [], array $indexes = [], array $permissions = null, bool $documentSecurity = true): Document
1153+
public function createCollection(string $id, array $attributes = [], array $indexes = [], ?array $permissions = null, bool $documentSecurity = true): Document
11541154
{
11551155
$permissions ??= [
11561156
Permission::create(Role::any()),
@@ -1437,7 +1437,7 @@ public function deleteCollection(string $id): bool
14371437
* @throws StructureException
14381438
* @throws Exception
14391439
*/
1440-
public function createAttribute(string $collection, string $id, string $type, int $size, bool $required, mixed $default = null, bool $signed = true, bool $array = false, string $format = null, array $formatOptions = [], array $filters = []): bool
1440+
public function createAttribute(string $collection, string $id, string $type, int $size, bool $required, mixed $default = null, bool $signed = true, bool $array = false, ?string $format = null, array $formatOptions = [], array $filters = []): bool
14411441
{
14421442
$collection = $this->silent(fn () => $this->getCollection($collection));
14431443

@@ -1802,7 +1802,7 @@ public function updateAttributeDefault(string $collection, string $id, mixed $de
18021802
* @return Document
18031803
* @throws Exception
18041804
*/
1805-
public function updateAttribute(string $collection, string $id, string $type = null, int $size = null, bool $required = null, mixed $default = null, bool $signed = null, bool $array = null, string $format = null, ?array $formatOptions = null, ?array $filters = null, ?string $newKey = null): Document
1805+
public function updateAttribute(string $collection, string $id, ?string $type = null, ?int $size = null, ?bool $required = null, mixed $default = null, ?bool $signed = null, ?bool $array = null, ?string $format = null, ?array $formatOptions = null, ?array $filters = null, ?string $newKey = null): Document
18061806
{
18071807
return $this->updateAttributeMeta($collection, $id, function ($attribute, $collectionDoc, $attributeIndex) use ($collection, $id, $type, $size, $required, $default, $signed, $array, $format, $formatOptions, $filters, $newKey) {
18081808
$altering = !\is_null($type)
@@ -3425,13 +3425,9 @@ public function createDocuments(string $collection, array $documents, int $batch
34253425
}
34263426

34273427
$documents[$key] = $this->decode($collection, $document);
3428+
$this->trigger(self::EVENT_DOCUMENT_CREATE, $documents[$key]);
34283429
}
34293430

3430-
$this->trigger(self::EVENT_DOCUMENTS_CREATE, new Document([
3431-
'$collection' => $collection->getId(),
3432-
'modified' => array_map(fn ($document) => $document->getId(), $documents)
3433-
]));
3434-
34353431
return $documents;
34363432
}
34373433

src/Database/Exception.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
class Exception extends \Exception
88
{
9-
public function __construct(string $message, int|string $code = 0, Throwable $previous = null)
9+
public function __construct(string $message, int|string $code = 0, ?Throwable $previous = null)
1010
{
1111
if (\is_string($code)) {
1212
if (\is_numeric($code)) {

src/Database/Mirror.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ protected function trigger(string $event, mixed $args = null): void
169169
$this->source->trigger($event, $args);
170170
}
171171

172-
public function silent(callable $callback, array $listeners = null): mixed
172+
public function silent(callable $callback, ?array $listeners = null): mixed
173173
{
174174
return $this->source->silent($callback, $listeners);
175175
}
@@ -194,7 +194,7 @@ public function delete(?string $database = null): bool
194194
return $this->delegate(__FUNCTION__, \func_get_args());
195195
}
196196

197-
public function createCollection(string $id, array $attributes = [], array $indexes = [], array $permissions = null, bool $documentSecurity = true): Document
197+
public function createCollection(string $id, array $attributes = [], array $indexes = [], ?array $permissions = null, bool $documentSecurity = true): Document
198198
{
199199
$result = $this->source->createCollection(
200200
$id,
@@ -292,7 +292,7 @@ public function deleteCollection(string $id): bool
292292
return $result;
293293
}
294294

295-
public function createAttribute(string $collection, string $id, string $type, int $size, bool $required, $default = null, bool $signed = true, bool $array = false, string $format = null, array $formatOptions = [], array $filters = []): bool
295+
public function createAttribute(string $collection, string $id, string $type, int $size, bool $required, $default = null, bool $signed = true, bool $array = false, ?string $format = null, array $formatOptions = [], array $filters = []): bool
296296
{
297297
$result = $this->source->createAttribute(
298298
$collection,
@@ -356,7 +356,7 @@ public function createAttribute(string $collection, string $id, string $type, in
356356
return $result;
357357
}
358358

359-
public function updateAttribute(string $collection, string $id, string $type = null, int $size = null, bool $required = null, mixed $default = null, bool $signed = null, bool $array = null, string $format = null, ?array $formatOptions = null, ?array $filters = null, ?string $newKey = null): Document
359+
public function updateAttribute(string $collection, string $id, ?string $type = null, ?int $size = null, ?bool $required = null, mixed $default = null, ?bool $signed = null, ?bool $array = null, ?string $format = null, ?array $formatOptions = null, ?array $filters = null, ?string $newKey = null): Document
360360
{
361361
$document = $this->source->updateAttribute(
362362
$collection,

tests/e2e/Adapter/Base.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17036,7 +17036,8 @@ public function testEvents(): void
1703617036
Database::EVENT_DOCUMENT_SUM,
1703717037
Database::EVENT_DOCUMENT_INCREASE,
1703817038
Database::EVENT_DOCUMENT_DECREASE,
17039-
Database::EVENT_DOCUMENTS_CREATE,
17039+
Database::EVENT_DOCUMENT_CREATE,
17040+
Database::EVENT_DOCUMENT_CREATE,
1704017041
Database::EVENT_DOCUMENT_UPDATE,
1704117042
Database::EVENT_DOCUMENT_UPDATE,
1704217043
Database::EVENT_DOCUMENT_UPDATE,

0 commit comments

Comments
 (0)