Skip to content

Commit

Permalink
Update docs
Browse files Browse the repository at this point in the history
Add some info about the new signature of the constructor for the EventEntity class
Add info about `updateState` being called whenever events are added or confirmed
  • Loading branch information
roziscoding authored and khaosdoctor committed Jan 18, 2019
1 parent bfedcd3 commit e067136
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ class PersonEmailChanged extends Event<IPersonEmailChangeParams> {

The main `Person` entity.

> Since version 2.9.0, EventEntity's constructor receives, as a second parameter the Entity class itself. This is used to update the state internally when adding new events. For now, this second parameter is optional. Not passing it, though, is considered deprecated and will stop being supported on the future
```ts
import ObjectId from 'bson-objectid'
import { EventEntity } from '@nxcd/paradox'
Expand All @@ -139,7 +141,7 @@ export class Person extends EventEntity<Person> {
constructor() {
super({
[ PersonWasCreated.eventName ]: PersonWasCreated.commit
})
}, Person)
}

static create (email: string, name: string, user: string): Person { // Method to create a person
Expand Down Expand Up @@ -259,7 +261,7 @@ An `EventEntity` is a business class which posesses the implementation of all ev
- `persistedEvents`: An array of events which were already persistted to the database. It follows the `{id, name, data, timestamp}` format
- `pendingEvents`: An array of events which were not yet saved to the database

When created, the new entity will receive (as a parameter) an object, of which the keys must be the name of an event and its value must be the `commit` function, which can be located anywhere, but, in our little example above, we created it as a static method inside the event entity itself.
When created, the new entity will receive (as a parameter) an object, of which the keys must be the name of an event and its value must be the `commit` function, which can be located anywhere, but, in our little example above, we created it as a static method inside the event entity itself. Since v2.9.0, it also receives the entity class itself, to be used for internal purposes.

This procedure is the same for all the events that entity might have, this is due to the fact that the `EventEntity`, when instantiated, will create a [Reducer](http://github.com/nxcd/tardis#reducer) instance in the property `this.reducer` and it'll pass on all these known events to it so it can be possible to manage all events inside the same class, without the need to instantiate something new.

Expand All @@ -273,6 +275,8 @@ Besides `state`, the `EventEntity` class will disclose several other methods suc
- `pushNewEvents`: Will receive an event array following the same `{id, name, data, timestamp}` format, but instead of adding them to the persisted events array, it'll add the events to the `pendingEvents` array and thus, notifying that there are events which were not yet persisted to the database and are only available inside this instance.
- `confirmEvents`: Will move all the items from the `pendingEvents` array to the `persistedEvents` array. This will confirm that all the events were successfuly saved into the database. This will be often used after we save the last state of the entity to the database.

All of the three methods above call the private method `updateState`, which sets all properties from the current state back to the instance of the entity class.

## Repositories

Repositories are places where data resides, by default, we would not have to create an event-base class for them, but, in order to standardize all the events saved into the database, it was necessary to create such class.
Expand Down

0 comments on commit e067136

Please sign in to comment.