Skip to content

Latest commit

 

History

History

eventive

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 

Eventive

Event Sourcing Framework in MongoDB

Getting Started

$ yarn add mongodb eventive

Setup

1. Define Domain Model

type MyDomainEvent =
  | BaseDomainEvent<
      "init",
      {
        // ...
      }
    >
  | BaseDomainEvent<
      "update",
      {
        // ...
      }
    >;

2. Define my state

type MyState = {
  // ...
};

3. Implement my business logic clearly in reducer

import { BaseReducer } from "eventive";

const reducer: BaseReducer<MyDomainEvent, MyState> = (prevState, event) => {
  // ...

  return nextState;
};

4. Maps older revision events to newer event interfaces for backwards compatibility

import { BaseMapper } from "eventive";

const mapper: BaseMapper<MyDomainEvent> = (event) => {
  // ...

  return currentRevisionEvent;
};

5. Then, eventive automatically make common repository interface

import { eventive } from "eventive";
import { MongoClient } from "mongodb";

const mongoClient = new MongoClient();
const db = mongoClient.db("my_database");

const myRepository = eventive({
  db,
  entityName: "MyModel",
  reducer,
  mapper,
  dbCollectionName: "events", // optional
});

Usage

all()

/**
 * Scan all entities
 */
myRepository.all();

findOne()

/**
 * Find one entity with `entityId`
 */
myRepository.findOne({
  entityId: "...",
});

batchGet()

/**
 * Find many entities with `entityIds`
 */
myRepository.batchGet({
  entityIds: ["...", "..."],
});

create()

/**
 * Create entity with initial DomainEvent
 */
const { commit } = myRepository.create({
  eventName: "init",
  eventBody: {
    // ...
  },
});

dispatch()

/**
 * Dispatch DomainEvent to entity
 */
const { commit } = myRepository.dispatch({
  entity,
  eventName: "edit",
  eventBody: {
    // ...
  },
});