0.59.0 • Published 3 months ago

@rouby/event-sourcing v0.59.0

Weekly downloads
-
License
MIT
Repository
github
Last release
3 months ago

@rouby/event-sourcing

This is an event sourcing library designed to provide a simple and efficient way to handle event sourcing in your applications.

Installation

npm install @rouby/event-sourcing

Usage

The idea is to define models that represent your domain and then apply events to them. The events are then stored in an event store and can be replayed to rebuild the model.

Events

To declare events you simply create functions that return an object with the event data.

export function createEntity(id: string) {
  return {
    type: 'createEntity' as const,
    version: 1,
    payload: {
      id,
      name: 'New Entity',
    },
  };
}

// to add typescript support, we can declare the event type
declare module '@rouby/event-sourcing' {
  interface RegisterEvents {
    createEntity: ReturnType<typeof createEntity>;
  }
}

Models

To declare models you can extend the Model class and implement the applyEvent method. You can also use the @applyEvent decorator to define event handlers.

import { Model, applyEvent } from '@rouby/event-sourcing';

export class Entity extends Model {
  kind = 'Entity' as const;

  // This decorator will automatically apply the event to the model
  @applyEvent('createEntity', 'name', matchesId)
  name = '';

  // The constructor should specify the keys needed to uniquely identify an instance of this model
  constructor(public id: string) {
    super();
  }

  applyEvent(event: SourcingEvent) {
    // This is an alternative to using the @applyEvent decorator
    if (event.type === 'createEntity' && event.payload.id === this.id) {
      this.name = event.payload.name;
    }
  }
}

function matchesId(this: Entity, e: { payload: { id: string } }) {
  return e.payload.id === this.id;
}

Bringing it together

import { EventSourcing } from '@rouby/event-sourcing';
import { createEntity } from '../events';
import * as models from '../models';

const source = new EventSourcing({ models });

await source.publishEvent({ event: createEntity('1') });

console.log('instance', source.getInstance(models.Entity, '1').name); //? New Entity

Examples

For basic examples on how to use this library see the examples directory.

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

0.59.0

3 months ago

0.57.0

3 months ago

0.55.0

3 months ago

0.58.0

3 months ago

0.56.0

3 months ago

0.54.0

6 months ago

0.53.0

7 months ago

0.52.0

7 months ago

0.51.0

7 months ago

0.50.0

7 months ago

0.49.0

7 months ago

0.48.0

7 months ago

0.47.0

7 months ago

0.46.0

7 months ago

0.45.0

7 months ago

0.44.0

7 months ago

0.43.0

7 months ago

0.42.0

7 months ago

0.41.0

7 months ago

0.40.0

7 months ago

0.39.0

7 months ago

0.38.0

7 months ago

0.37.0

7 months ago

0.36.0

7 months ago

0.35.0

7 months ago

0.34.0

7 months ago

0.33.0

7 months ago

0.32.0

7 months ago

0.31.0

7 months ago

0.30.0

7 months ago

0.29.0

7 months ago

0.28.0

7 months ago

0.27.0

7 months ago

0.26.0

7 months ago

0.25.0

7 months ago

0.24.0

7 months ago

0.23.0

7 months ago

0.22.0

7 months ago

0.21.0

7 months ago

0.20.0

7 months ago

0.19.0

7 months ago

0.18.0

7 months ago

0.17.0

7 months ago

0.16.0

7 months ago

0.15.0

7 months ago

0.14.0

7 months ago

0.13.0

7 months ago

0.12.0

8 months ago

0.11.0

8 months ago

0.10.0

8 months ago

0.9.0

8 months ago

0.8.0

8 months ago

0.7.0

8 months ago

0.6.0

8 months ago

0.5.0

8 months ago

0.4.0

8 months ago

0.3.0

8 months ago

0.2.0

8 months ago

0.1.0

8 months ago