Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@getanthill/event-source

Package Overview
Dependencies
Maintainers
5
Versions
40
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@getanthill/event-source

Event-sourced data store handling library

  • 0.12.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
116
decreased by-40.51%
Maintainers
5
Weekly downloads
 
Created
Source

getanthill Event-Source

pipeline Quality Gate Status

Coverage Reliability Rating

Getting Started

Install the package

npm install -S @getanthill/event-source

Define your class

The following will be extended with the Event-Source functionalities.

// src/models/Users.ts
import { EventSourcedFactory } from '@getanthill/event-source';

/**
 * This is the main reducer for your events.
 * > f(state_{i}, event) = state_{i+1}
 */
const reducer = (state, event) => {
  if (event.type === 'CREATED') {
    return {
      firstname: event.firstname,
    };
  }

  return state;
};

export default class Users extends EventSourced('users', reducer) {
  /**
   * Create a new user
   * @param {string} firstname The user firstname
   * @return {Promise<object>} Updated state
   */
  create(firstname) {
    return this.handle(() => [
      {
        type: 'CREATED',
        firstname,
      },
    ]);
  }
}

Use your Event-Sourced class

// src/business/users.ts
import Users from '../models/Users';

/**
 * Create a user
 *
 * @param {object} db MongoDb connection
 * @param {string} firstname User firstname
 * @return {Promise<object>} State after success
 */
function createUser(db, firstname) {
  const user = new Users(db, 1); // 1 = correlation_id

  return user.create('John');
}

In your database, we will now have access to the events in the collection users_events:

db.users_events.find().pretty();
{
  "type": "CREATED",
  "correlation_id": 1,
  "version": 0,
  "firstname": "John",
  "created_at": "2020-11-01T06:05:43.210Z"
}

and also the final resulting state of your object:

db.users.find().pretty();
{
  "correlation_id": 1,
  "version": 0,
  "firstname": "John"
}

Conclusion

Now that you are able to have a fully Event-Source featured class, you can read further on what is possible to create with this library and how to custom a bit some features such as the correlation_id naming.

Keywords

FAQs

Package last updated on 24 Nov 2022

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc