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
23 changes: 9 additions & 14 deletions docs/pages/getting_started.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,12 +163,12 @@ use Patchlevel\EventSourcing\Attribute\Projector;
use Patchlevel\EventSourcing\Attribute\Setup;
use Patchlevel\EventSourcing\Attribute\Subscribe;
use Patchlevel\EventSourcing\Attribute\Teardown;
use Patchlevel\EventSourcing\Subscription\Subscriber\SubscriberUtil;

#[Projector('hotel')]
#[Projector(self::TABLE)]
final class HotelProjector
{
use SubscriberUtil;
// use a const for easier access in the projector & to keep projector id and table name in sync
private const TABLE = 'hotel';

public function __construct(
private readonly Connection $db,
Expand All @@ -178,14 +178,14 @@ final class HotelProjector
/** @return list<array{id: string, name: string, guests: int}> */
public function getHotels(): array
{
return $this->db->fetchAllAssociative("SELECT id, name, guests FROM {$this->table()};");
return $this->db->fetchAllAssociative(sprintf('SELECT id, name, guests FROM %s;'), self::TABLE);
}

#[Subscribe(HotelCreated::class)]
public function handleHotelCreated(HotelCreated $event): void
{
$this->db->insert(
$this->table(),
self::TABLE,
[
'id' => $event->hotelId->toString(),
'name' => $event->hotelName,
Expand All @@ -198,7 +198,7 @@ final class HotelProjector
public function handleGuestIsCheckedIn(GuestIsCheckedIn $event): void
{
$this->db->executeStatement(
"UPDATE {$this->table()} SET guests = guests + 1 WHERE id = ?;",
sprintf('UPDATE %s SET guests = guests + 1 WHERE id = ?;', self::TABLE),
[$event->hotelId->toString()],
);
}
Expand All @@ -207,26 +207,21 @@ final class HotelProjector
public function handleGuestIsCheckedOut(GuestIsCheckedOut $event): void
{
$this->db->executeStatement(
"UPDATE {$this->table()} SET guests = guests - 1 WHERE id = ?;",
sprintf('UPDATE %s SET guests = guests - 1 WHERE id = ?;', self::TABLE),
[$event->hotelId->toString()],
);
}

#[Setup]
public function create(): void
{
$this->db->executeStatement("CREATE TABLE IF NOT EXISTS {$this->table()} (id VARCHAR PRIMARY KEY, name VARCHAR, guests INTEGER);");
$this->db->executeStatement(sprintf('CREATE TABLE IF NOT EXISTS %s (id VARCHAR PRIMARY KEY, name VARCHAR, guests INTEGER);', self::TABLE));
}

#[Teardown]
public function drop(): void
{
$this->db->executeStatement("DROP TABLE IF EXISTS {$this->table()};");
}

private function table(): string
{
return 'projection_' . $this->subscriberId();
$this->db->executeStatement(sprintf('DROP TABLE IF EXISTS %s;', self::TABLE));
}
}
```
Expand Down
17 changes: 4 additions & 13 deletions docs/pages/subscription.md
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,6 @@ use Patchlevel\EventSourcing\Subscription\Lookup;
#[Projector('public_profile')]
final class PublicProfileProjection
{
use SubscriberUtil;

// ... constructor

#[Subscribe(Published::class)]
Expand Down Expand Up @@ -306,32 +304,26 @@ use Doctrine\DBAL\Connection;
use Patchlevel\EventSourcing\Attribute\Projector;
use Patchlevel\EventSourcing\Attribute\Setup;
use Patchlevel\EventSourcing\Attribute\Teardown;
use Patchlevel\EventSourcing\Subscription\Subscriber\SubscriberUtil;

#[Projector('profile_1')]
#[Projector(self::TABLE)]
final class ProfileProjector
{
use SubscriberUtil;
private const TABLE = 'profile_v1';

private Connection $connection;

#[Setup]
public function create(): void
{
$this->connection->executeStatement(
"CREATE TABLE IF NOT EXISTS {$this->table()} (id VARCHAR PRIMARY KEY, name VARCHAR NOT NULL);",
sprintf('CREATE TABLE IF NOT EXISTS %s (id VARCHAR PRIMARY KEY, name VARCHAR NOT NULL);', self::TABLE),
);
}

#[Teardown]
public function drop(): void
{
$this->connection->executeStatement("DROP TABLE IF EXISTS {$this->table()};");
}

private function table(): string
{
return 'projection_' . $this->subscriberId();
$this->connection->executeStatement(sprintf('DROP TABLE IF EXISTS %s;', self::TABLE));
}
}
```
Expand All @@ -346,7 +338,6 @@ final class ProfileProjector
If you change the subscriber id, you must also change the table/collection name.
The subscription engine will create a new subscription with the new subscriber id.
That means the setup method will be called again and the table/collection will conflict with the old existing projection.
You can use the `SubscriberUtil` to build the table/collection name.

!!! note

Expand Down
1 change: 1 addition & 0 deletions src/Subscription/Subscriber/SubscriberHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Patchlevel\EventSourcing\Metadata\Subscriber\SubscriberMetadata;
use Patchlevel\EventSourcing\Metadata\Subscriber\SubscriberMetadataFactory;

/** @deprecated since 3.15.0 will be removed with 4.0.0 */
final class SubscriberHelper
{
public function __construct(
Expand Down
1 change: 1 addition & 0 deletions src/Subscription/Subscriber/SubscriberUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Patchlevel\EventSourcing\Metadata\Subscriber\AttributeSubscriberMetadataFactory;
use Patchlevel\EventSourcing\Metadata\Subscriber\SubscriberMetadataFactory;

/** @deprecated since 3.15.0 will be removed with 4.0.0 */
trait SubscriberUtil
{
private static SubscriberMetadataFactory|null $metadataFactory = null;
Expand Down
Loading