New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

eventre

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

eventre

Event Sourcing Framework in MongoDB

0.1.1
latest
npm
Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created
Source

eventre

Event Sourcing Framework in MongoDB

Usage

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

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

/**
 * 1. Define your domain event
 */
type MyDomainEvent =
  | BaseDomainEvent<
      "init",
      {
        // ...
      }
    >
  | BaseDomainEvent<
      "update",
      {
        // ...
      }
    >;

/**
 * 2. Define your state
 */
type MyState = {
  // ...
};

/**
 * 3. Implement your own business logic clearly in reducer
 */
const reducer: BaseReducer<MyDomainEvent, MyState> = (prevState, event) => {
  // ...
};

/**
 * 4. Then, `eventre` automatically make common repository interface
 */
const repository = eventre({
  db,
  aggregateName: "MyModel",
  reducer,
});

// Scan all aggregates
repository.all();

// Find one aggregate with `aggregateId`
repository.findOne({
  aggregateId: "...",
});

// Find many aggregates with `aggregateIds`
repository.batchGet({
  aggregateIds: ["...", "..."],
});

// Create aggregate with initial DomainEvent
repository.create({
  initialEventName: "init",
  initialEventData: {
    // ...
  },
});

// Dispatch DomainEvent to aggregate
repository.dispatch({
  aggregate,
  eventName: "edit",
  eventData: {
    // ...
  },
});

FAQs

Package last updated on 11 Mar 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts