boostercloud / booster

Booster Framework
https://www.boosterframework.com
Apache License 2.0
416 stars 86 forks source link

Create an entity initializer that guarantees that all reducers will receive a entity instance #1493

Open javiertoledo opened 10 months ago

javiertoledo commented 10 months ago

Feature Request

Description

In the current version of Booster, reducers and projections receive the previous state as an optional parameter, forcing developers to add additional code to handle a case that, in theory should be impossible:

  @Reduces(ProductDeleted)
  public static reduceProductDeleted(
    event: ProductDeleted,
    current?: DocumentEntity
  ): DocumentEntity {
    if (!current) {
      return DocumentEntity.DUMMY // Empty object created just to have something to return
    }

    ...
  }

Possible Solution

Add a new mandatory method to the entity interface or a decorator instructing Booster on how to build a valid initial state. Entities in Booster are created when the first event is reduced, generally an <EntityName>Created event, but not all events can be used to build the initial state.