Skip to content

Commit

Permalink
Merge pull request #156 from castore-dev/start-v2
Browse files Browse the repository at this point in the history
feature: Rename some properties here & there
  • Loading branch information
ThomasAribart committed Sep 29, 2023
2 parents ecd9450 + 775ebd1 commit 3fdd2ac
Show file tree
Hide file tree
Showing 67 changed files with 566 additions and 785 deletions.
2 changes: 1 addition & 1 deletion castore.code-workspace
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
},
{
"path": "packages/dynamodb-event-storage-adapter",
"name": "🔌 DynamoDb"
"name": "🔌 DynamoDB"
},
{
"path": "packages/redux-event-storage-adapter",
Expand Down
4 changes: 2 additions & 2 deletions demo/blueprint/src/pokemons/eventStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { appearedEvent, caughtByTrainerEvent, levelledUpEvent } from './events';

export const pokemonsEventStore = new EventStore({
eventStoreId: 'POKEMONS',
eventStoreEvents: [appearedEvent, caughtByTrainerEvent, levelledUpEvent],
reduce: (pokemonAggregate: PokemonAggregate, event): PokemonAggregate => {
eventTypes: [appearedEvent, caughtByTrainerEvent, levelledUpEvent],
reducer: (pokemonAggregate: PokemonAggregate, event): PokemonAggregate => {
const { version, aggregateId } = event;

switch (event.type) {
Expand Down
8 changes: 4 additions & 4 deletions demo/blueprint/src/pokemons/mock.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { EventStoreEventsDetails } from '@castore/core';
import { EventStoreEventDetails } from '@castore/core';

import { pokemonsEventStore } from './eventStore';

export const pikachuId = 'pikachu-id';

export const pikachuAppearedEvent: EventStoreEventsDetails<
export const pikachuAppearedEvent: EventStoreEventDetails<
typeof pokemonsEventStore
> = {
aggregateId: pikachuId,
Expand All @@ -20,7 +20,7 @@ export const pikachuAppearedEvent: EventStoreEventsDetails<
},
};

export const pikachuCaughtEvent: EventStoreEventsDetails<
export const pikachuCaughtEvent: EventStoreEventDetails<
typeof pokemonsEventStore
> = {
aggregateId: pikachuId,
Expand All @@ -32,7 +32,7 @@ export const pikachuCaughtEvent: EventStoreEventsDetails<
},
};

export const pikachuLevelledUpEvent: EventStoreEventsDetails<
export const pikachuLevelledUpEvent: EventStoreEventDetails<
typeof pokemonsEventStore
> = {
aggregateId: pikachuId,
Expand Down
4 changes: 2 additions & 2 deletions demo/blueprint/src/trainers/eventStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { gameStartedEvent, pokemonCaughtEvent } from './events';

export const trainersEventStore = new EventStore({
eventStoreId: 'TRAINERS',
eventStoreEvents: [gameStartedEvent, pokemonCaughtEvent],
reduce: (trainerAggregate: TrainerAggregate, event): TrainerAggregate => {
eventTypes: [gameStartedEvent, pokemonCaughtEvent],
reducer: (trainerAggregate: TrainerAggregate, event): TrainerAggregate => {
const { version, aggregateId } = event;

switch (event.type) {
Expand Down
6 changes: 3 additions & 3 deletions demo/blueprint/src/trainers/mock.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { EventStoreEventsDetails } from '@castore/core';
import { EventStoreEventDetails } from '@castore/core';

import { trainersEventStore } from './eventStore';

export const ashId = 'ash-ketchum-id';

export const ashPokemonGameStartedEvent: EventStoreEventsDetails<
export const ashPokemonGameStartedEvent: EventStoreEventDetails<
typeof trainersEventStore
> = {
aggregateId: ashId,
Expand All @@ -16,7 +16,7 @@ export const ashPokemonGameStartedEvent: EventStoreEventsDetails<
},
};

export const ashPokemonCaughtEvent: EventStoreEventsDetails<
export const ashPokemonCaughtEvent: EventStoreEventDetails<
typeof trainersEventStore
> = {
aggregateId: ashId,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import MockDate from 'mockdate';

import { EventStoreEventsDetails } from '@castore/core';
import { EventStoreEventDetails } from '@castore/core';
import { catchPokemonCommand } from '@castore/demo-blueprint';
import { mockEventStore } from '@castore/test-tools';

import { pokemonsEventStore } from '~/libs/eventStores/pokemons';
import { trainersEventStore } from '~/libs/eventStores/trainers';

const pikachuId = 'pikachu1';
const pikachuAppearedEvent: EventStoreEventsDetails<typeof pokemonsEventStore> =
const pikachuAppearedEvent: EventStoreEventDetails<typeof pokemonsEventStore> =
{
aggregateId: pikachuId,
version: 1,
Expand All @@ -19,14 +19,13 @@ const pikachuAppearedEvent: EventStoreEventsDetails<typeof pokemonsEventStore> =
};

const ashId = 'ash';
const ashGameStartedEvent: EventStoreEventsDetails<typeof trainersEventStore> =
{
aggregateId: ashId,
version: 1,
type: 'GAME_STARTED',
timestamp: '2021-01-01T00:00:00.000Z',
payload: { trainerName: 'Ash Ketchum' },
};
const ashGameStartedEvent: EventStoreEventDetails<typeof trainersEventStore> = {
aggregateId: ashId,
version: 1,
type: 'GAME_STARTED',
timestamp: '2021-01-01T00:00:00.000Z',
payload: { trainerName: 'Ash Ketchum' },
};

describe('Commands - catchPokemon', () => {
const pokemonsEventStoreMock = mockEventStore(pokemonsEventStore, [
Expand Down
2 changes: 1 addition & 1 deletion demo/implementation/libs/eventStores/client.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { DynamoDBClient } from '@aws-sdk/client-dynamodb';

export const dynamoDbClient = new DynamoDBClient({});
export const dynamoDBClient = new DynamoDBClient({});
8 changes: 4 additions & 4 deletions demo/implementation/libs/eventStores/pokemons.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { pokemonsEventStore as $pokemonsEventStore } from '@castore/demo-blueprint';
import { DynamoDbEventStorageAdapter } from '@castore/dynamodb-event-storage-adapter';
import { LegacyDynamoDBEventStorageAdapter } from '@castore/dynamodb-event-storage-adapter';

import { dynamoDbClient } from './client';
import { dynamoDBClient } from './client';

export const pokemonsEventStore = $pokemonsEventStore;

pokemonsEventStore.storageAdapter = new DynamoDbEventStorageAdapter({
pokemonsEventStore.eventStorageAdapter = new LegacyDynamoDBEventStorageAdapter({
tableName: process.env.POKEMON_EVENTS_TABLE_NAME as string,
dynamoDbClient,
dynamoDBClient,
});
8 changes: 4 additions & 4 deletions demo/implementation/libs/eventStores/trainers.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { trainersEventStore as $trainersEventStore } from '@castore/demo-blueprint';
import { DynamoDbEventStorageAdapter } from '@castore/dynamodb-event-storage-adapter';
import { LegacyDynamoDBEventStorageAdapter } from '@castore/dynamodb-event-storage-adapter';

import { dynamoDbClient } from './client';
import { dynamoDBClient } from './client';

export const trainersEventStore = $trainersEventStore;

trainersEventStore.storageAdapter = new DynamoDbEventStorageAdapter({
trainersEventStore.eventStorageAdapter = new LegacyDynamoDBEventStorageAdapter({
tableName: process.env.TRAINER_EVENTS_TABLE_NAME as string,
dynamoDbClient,
dynamoDBClient,
});
6 changes: 3 additions & 3 deletions docs/docs/2-event-sourcing/1-events.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,12 @@ type PokemonCaughtEventTypeDetail = {
};
```

- `EventTypesDetails`: Returns the events details of a list of `EventType`
- `EventTypeDetails`: Returns the events details of a list of `EventType`

```ts
import type { EventTypesDetails } from '@castore/core';
import type { EventTypeDetails } from '@castore/core';

type PokemonEventTypeDetails = EventTypesDetails<
type PokemonEventTypeDetails = EventTypeDetails<
[typeof pokemonAppearedEventType, typeof pokemonCaughtEventType]
>;
// => EventTypeDetail<typeof pokemonAppearedEventType>
Expand Down
40 changes: 20 additions & 20 deletions docs/docs/2-event-sourcing/3-event-stores.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ import { EventStore } from '@castore/core';

const pokemonsEventStore = new EventStore({
eventStoreId: 'POKEMONS',
eventStoreEvents: [
eventTypes: [
pokemonAppearedEventType,
pokemonCaughtEventType,
pokemonLeveledUpEventType,
...
],
reduce: pokemonsReducer,
reducer: pokemonsReducer,
});
// ...and that's it 🥳
```
Expand All @@ -51,10 +51,10 @@ const pokemonsEventStore = new EventStore({
**Constructor:**

- <code>eventStoreId <i>(string)</i></code>: A string identifying the event store
- <code>eventStoreEvents <i>(EventType[])</i></code>: The list of event types in the event store
- <code>eventTypes <i>(EventType[])</i></code>: The list of event types in the event store
- <code>reduce <i>(EventType[])</i></code>: A <a href="../aggregates-reducers">reducer function</a> that can be applied to the store event types
- <code>onEventPushed <i>(?(pushEventResponse: PushEventResponse) => Promise&lt;void&gt;)</i></code>: To run a callback after events are pushed (input is exactly the return value of the <code>pushEvent</code> method)
- <code>storageAdapter <i>(?EventStorageAdapter)</i></code>: See <a href="../fetching-events">fetching events</a>
- <code>eventStorageAdapter <i>(?EventStorageAdapter)</i></code>: See <a href="../fetching-events">fetching events</a>

> ☝️ The return type of the `reducer` is used to infer the `Aggregate` type of the `EventStore`, so it is important to type it explicitely.
Expand All @@ -69,10 +69,10 @@ const pokemonsEventStoreId = pokemonsEventStore.eventStoreId;
// => 'POKEMONS'
```

- <code>eventStoreEvents <i>(EventType[])</i></code>
- <code>eventTypes <i>(EventType[])</i></code>

```ts
const pokemonsEventStoreEvents = pokemonsEventStore.eventStoreEvents;
const pokemonsEventTypes = pokemonsEventStore.eventTypes;
// => [pokemonAppearedEventType, pokemonCaughtEventType...]
```

Expand All @@ -90,26 +90,26 @@ const onEventPushed = pokemonsEventStore.onEventPushed;
// => undefined (we did not provide one in this example)
```

- <code>storageAdapter <i>(?EventStorageAdapter)</i></code>: See <a href="../fetching-events">fetching events</a>
- <code>eventStorageAdapter <i>(?EventStorageAdapter)</i></code>: See <a href="../fetching-events">fetching events</a>

```ts
const storageAdapter = pokemonsEventStore.storageAdapter;
const eventStorageAdapter = pokemonsEventStore.eventStorageAdapter;
// => undefined (we did not provide one in this example)
```

> ☝️ The `storageAdapter` is not read-only so you do not have to provide it right away.
> ☝️ The `eventStorageAdapter` is not read-only so you do not have to provide it right away.
---

**Sync Methods:**

- <code>getStorageAdapter <i>(() => EventStorageAdapter)</i></code>: Returns the event store event storage adapter if it exists. Throws an <code>UndefinedStorageAdapterError</code> if it doesn't.
- <code>getEventStorageAdapter <i>(() => EventStorageAdapter)</i></code>: Returns the event store event storage adapter if it exists. Throws an <code>UndefinedEventStorageAdapterError</code> if it doesn't.

```ts
import { UndefinedStorageAdapterError } from '@castore/core';
import { UndefinedEventStorageAdapterError } from '@castore/core';

expect(() => pokemonsEventStore.getStorageAdapter()).toThrow(
new UndefinedStorageAdapterError({ eventStoreId: 'POKEMONS' }),
expect(() => pokemonsEventStore.getEventStorageAdapter()).toThrow(
new UndefinedEventStorageAdapterError({ eventStoreId: 'POKEMONS' }),
);
// => true
```
Expand All @@ -126,7 +126,7 @@ const myPikachuAggregate = pokemonsEventStore.buildAggregate(myPikachuEvents);

**Async Methods:**

The following methods interact with the data layer of your event store through its [`EventStorageAdapter`](./4-fetching-events.md). They will throw an `UndefinedStorageAdapterError` if you did not provide one.
The following methods interact with the data layer of your event store through its [`EventStorageAdapter`](./4-fetching-events.md). They will throw an `UndefinedEventStorageAdapterError` if you did not provide one.

- <code>getEvents <i>((aggregateId: string, opt?: OptionsObj) => Promise&lt;ResponseObj&gt;)</i></code>: Retrieves the events of an aggregate, ordered by <code>version</code>. Returns an empty array if no event is found for this <code>aggregateId</code>.

Expand Down Expand Up @@ -287,21 +287,21 @@ type PokemonsEventStoreId = EventStoreId<typeof pokemonsEventStore>;
// => 'POKEMONS'
```

- `EventStoreEventsTypes`: Returns the `EventStore` list of events types
- `EventStoreEventTypes`: Returns the `EventStore` list of events types

```ts
import type { EventStoreEventsTypes } from '@castore/core';
import type { EventStoreEventTypes } from '@castore/core';

type PokemonEventTypes = EventStoreEventsTypes<typeof pokemonsEventStore>;
type PokemonEventTypes = EventStoreEventTypes<typeof pokemonsEventStore>;
// => [typeof pokemonAppearedEventType, typeof pokemonCaughtEventType...]
```

- `EventStoreEventsDetails`: Returns the union of all the `EventStore` possible events details
- `EventStoreEventDetails`: Returns the union of all the `EventStore` possible events details

```ts
import type { EventStoreEventsDetails } from '@castore/core';
import type { EventStoreEventDetails } from '@castore/core';

type PokemonEventDetails = EventStoreEventsDetails<typeof pokemonsEventStore>;
type PokemonEventDetails = EventStoreEventDetails<typeof pokemonsEventStore>;
// => EventTypeDetail<typeof pokemonAppearedEventType>
// | EventTypeDetail<typeof pokemonCaughtEventType>
// | ...
Expand Down
8 changes: 4 additions & 4 deletions docs/docs/2-event-sourcing/4-fetching-events.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@ For the moment, we didn't provide any actual way to store our events data. This
import { EventStore } from '@castore/core';

await pokemonsEventStore.getEvents('pikachu1');
// ❌ Will throw an `UndefinedStorageAdapterError`
// ❌ Will throw an `UndefinedEventStorageAdapterError`

const pokemonsEventStore = new EventStore({
eventStoreId: 'POKEMONS',
eventTypes: pokemonEventTypes,
reduce: pokemonsReducer,
reducer: pokemonsReducer,
// 👇 Provide it in the constructor
storageAdapter: mySuperStorageAdapter,
eventStorageAdapter: mySuperEventStorageAdapter,
});

// 👇 ...or set/switch it in context later
pokemonsEventStore.storageAdapter = mySuperStorageAdapter;
pokemonsEventStore.eventStorageAdapter = mySuperEventStorageAdapter;

const { events } = await pokemonsEventStore.getEvents('pikachu1');
const { aggregate } = await pokemonsEventStore.getAggregate('pikachu1');
Expand Down
12 changes: 6 additions & 6 deletions docs/docs/3-reacting-to-events/4-connected-event-store.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ await connectedPokemonsEventStore.pushEvent({

:::info

Note that setting a connected event store `storageAdapter` and `onEventPushed` properties will override those of the original event store instead.
Note that setting a connected event store `eventStorageAdapter` and `onEventPushed` properties will override those of the original event store instead.

:::

Expand Down Expand Up @@ -81,14 +81,14 @@ const messageChannel = connectedPokemonsEventStore.messageChannel;
// => appMessageBus
```

> ☝️ Note that the `storageAdapter` property will act as a pointer toward the original event store `storageAdapter`:
> ☝️ Note that the `eventStorageAdapter` property will act as a pointer toward the original event store `eventStorageAdapter`:
>
> ```ts
> originalEventStore.storageAdapter = myStorageAdapter;
> connectedEventStore.storageAdapter; // => myStorageAdapter
> originalEventStore.eventStorageAdapter = myEventStorageAdapter;
> connectedEventStore.eventStorageAdapter; // => myEventStorageAdapter
>
> connectedEventStore.storageAdapter = anotherStorageAdapter;
> originalEventStore.storageAdapter; // => anotherStorageAdapter
> connectedEventStore.eventStorageAdapter = anotherEventStorageAdapter;
> originalEventStore.eventStorageAdapter; // => anotherEventStorageAdapter
> ```
</details>
10 changes: 5 additions & 5 deletions packages/core/src/command/command.fixtures.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { vi } from 'vitest';

import { EventType, EventTypeDetail } from '~/event/eventType';
import { EventStorageAdapter } from '~/eventStorageAdapter';
import {
eventAlreadyExistsErrorCode,
EventAlreadyExistsError,
EventStore,
} from '~/eventStore';
import { StorageAdapter } from '~/storageAdapter';

import { tuple, Command } from './command';

Expand All @@ -19,7 +19,7 @@ export const putSnapshotMock = vi.fn();
export const getLastSnapshotMock = vi.fn();
export const listSnapshotsMock = vi.fn();

export const mockStorageAdapter: StorageAdapter = {
export const eventStorageAdapterMock: EventStorageAdapter = {
pushEvent: pushEventMock,
pushEventGroup: pushEventGroupMock,
groupEvent: groupEventMock,
Expand Down Expand Up @@ -110,13 +110,13 @@ export const countersReducer = (

export const counterEventStore = new EventStore({
eventStoreId: 'Counters',
eventStoreEvents: [
eventTypes: [
counterCreatedEvent,
counterIncrementedEvent,
counterDeletedEvent,
],
reduce: countersReducer,
storageAdapter: mockStorageAdapter,
reducer: countersReducer,
eventStorageAdapter: eventStorageAdapterMock,
});

export const requiredEventStores = tuple(counterEventStore);
Expand Down
Loading

0 comments on commit 3fdd2ac

Please sign in to comment.