Coder Social home page Coder Social logo

rails_event_store's Introduction

Build Status Gem Version Code Climate Test Coverage Join the chat at https://gitter.im/arkency/rails_event_store

EventStore

A Ruby implementation of an EventStore based on Active Record.

Installation

  • Add following line to your application's Gemfile:
gem 'rails_event_store'
  • Use provided task to generate a table to store events in you DB.
rails generate rails_event_store:migrate
rake db:migrate

Usage

To communicate with ES you have to create instance of RailsEventStore::Client class.

client = RailsEventStore::Client.new

Creating new event

Firstly you have to define own event model extending RailsEventStore::Event class.

class OrderCreated < RailsEventStore::Event
end
stream_name = "order_1"
event_data = {
               data: { data: "sample" },
               event_id: "b2d506fd-409d-4ec7-b02f-c6d2295c7edd"
             }
event = OrderCreated.new(event_data)

#publishing event for specific stream
client.publish_event(event, stream_name)

#publishing global event. In this case stream_name is 'all'.
client.publish_event(event)

Creating new event with optimistic locking:

class OrderCreated < RailsEventStore::Event
end
stream_name = "order_1"
event_data = {
               data: { data: "sample" },
               event_id: "b2d506fd-409d-4ec7-b02f-c6d2295c7edd"
             }
event = OrderCreated.new(event_data)
expected_version = "850c347f-423a-4158-a5ce-b885396c5b73" #last event_id
client.publish_event(event, stream_name, expected_version)

Reading stream's events forward in batch

stream_name = "order_1"
start = "850c347f-423a-4158-a5ce-b885396c5b73"
count = 40
client.read_all_events(stream_name, start, count)

Reading all events from stream forward

This method allows us to load all stream's events ascending.

stream_name = "order_1"
client.read_all_events(stream_name)

Reading all events forward

This method allows us to load all stored events ascending.

client.read_all_streams

Deleting stream

You can permanently delete all events from a specific stream. Use this wisely.

stream_name = "product_1"
client.delete_stream(stream_name)

Subscribing to events

To listen on specific events synchronously you have to create subscriber reprezentation. The only requirement is that subscriber class has to implement the 'handle_event(event)' method.

class InvoiceReadModel
  def handle_event(event)
    #we deal here with event's data
  end
end
  • You can subscribe on specific set of events
invoice = InvoiceReadModel.new
client.subscribe(invoice, ['PriceChanged', 'ProductAdded'])
  • You can also listen on all incoming events
invoice = InvoiceReadModel.new
client.subscribe_to_all_events(invoice)

Resources

There're already few blogposts about Rails EventStore. Check them out:

About

Arkency

Rails Event Store is funded and maintained by Arkency. Check out our other open-source projects.

You can also hire us or read our blog.

rails_event_store's People

Contributors

rybex avatar andrzejkrzywda avatar swistak35 avatar mpraglowski avatar gottfrois avatar voter101 avatar anderslime avatar

Watchers

Sheryl.Hsu avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.