Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support multiple event stores #168

Merged
merged 8 commits into from Jul 4, 2019

Conversation

@slashdotdash
Copy link
Member

slashdotdash commented Jun 20, 2019

This pull request adds support for defining and running multiple event store modules within a single Elixir application.

Getting started

First, you must define your own event store module using the EventStore macro:

defmodule MyApp.EventStore do
  use EventStore, otp_app: :my_app

  # Optional `init/1` function to modify config at runtime.
  def init(config) do
    {:ok, config}
  end
end

You can name your event store module however you like.

Secondly, configure the MyApp.EventStore module in config/config.exs:

config :my_app, MyApp.EventStore,
  serializer: EventStore.JsonSerializer,
  username: "postgres",
  password: "postgres",
  database: "myapp_eventstore",
  hostname: "localhost",
  pool_size: 10

Finally, the event store module must be included within your application's supervision tree (e.g. in lib/my_app/application.ex, inside the start/2 function):

defmodule MyApp.Application do
  use Application

  def start(_type, _args) do
    children = [
      MyApp.EventStore
    ]

    opts = [strategy: :one_for_one, name: MyApp.Supervisor]
    Supervisor.start_link(children, opts)
  end
end

Optionally, you can configure the event store modules in config/config.exs to allow running the event store mix tasks without providing the event store module as a command line argument:

config :my_app, event_stores: [MyApp.EventStore]

The above configuration allows you to run mix event_store.init instead of mix event_store.init -e MyApp.EventStore (as an example).

Usage

Use your event store module exactly as you would have previously used the EventStore itself.

:ok = MyApp.EventStore.append_to_stream(stream_uuid, expected_version, events)

For ease of upgrading you can alias your own event store module:

alias MyApp.EventStore, as: EventStore

:ok = EventStore.append_to_stream(stream_uuid, expected_version, events)

You can define and use as many different event store modules as you like. Each store will use its own separate database and be completely isolated from one another.

@slashdotdash slashdotdash force-pushed the feature/multi-event-store branch 3 times, most recently from 3da7547 to c1baee5 Jun 24, 2019
@slashdotdash slashdotdash force-pushed the feature/multi-event-store branch 3 times, most recently from 553c062 to 31aa9d9 Jul 1, 2019
@slashdotdash slashdotdash marked this pull request as ready for review Jul 2, 2019
@slashdotdash slashdotdash force-pushed the feature/multi-event-store branch from 31aa9d9 to 021bc3b Jul 3, 2019
Prefix notification `GenServer`s with event store module name.
Include event store module in subscriptions.
Pass configured serializer to subscriptions.
Update benchmark suite to support multi event store changes.
Remove serializer module loaded check due to broken build.
Monitored server should handle process already started.
Tag unstable test as `:manual`.
@slashdotdash slashdotdash force-pushed the feature/multi-event-store branch from 021bc3b to c003190 Jul 4, 2019
@slashdotdash slashdotdash merged commit 4427e43 into master Jul 4, 2019
2 checks passed
2 checks passed
Travis CI - Branch Build Passed
Details
Travis CI - Pull Request Build Passed
Details
@slashdotdash slashdotdash deleted the feature/multi-event-store branch Jul 4, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
1 participant
You can’t perform that action at this time.