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 dynamic event stores #6

Merged
merged 3 commits into from
Jan 13, 2020
Merged

Support dynamic event stores #6

merged 3 commits into from
Jan 13, 2020

Conversation

slashdotdash
Copy link
Member

Allow a dynamically named and configured EventStore to be started by Commanded. The optional name is included in the event store meta data and passed to all event store functions.

Example usage

Define an application which uses the init/1 callback function to set the event store name and schema from config:

defmodule MyApp.Application do
  use Commanded.Application,
    otp_app: :my_app,
    event_store: [
      adapter: Commanded.EventStore.Adapters.EventStore,
      event_store: MyApp.EventStore
    ]

  def init(config) do
    name = Keyword.fetch!(config, :name)
    {schema, config} = Keyword.pop(config, :schema)

    config = put_in(config, [:event_store, :name], Module.concat([name, EventStore])
    config = put_in(config, [:event_store, :schema], schema)

    {:ok, config}
  end
end

Start an application per tenant with each using a separate event store and schema:

for tenant <- [:tenant1, :tenant2, :tenant3] do
  {:ok, _pid} = MyApp.Application.start_link(name: tenant, schema: Atom.to_string(tenant))
end

@slashdotdash slashdotdash marked this pull request as ready for review January 13, 2020 12:39
@slashdotdash slashdotdash merged commit a7f2d40 into master Jan 13, 2020
@slashdotdash slashdotdash deleted the feature/dynamic branch January 13, 2020 12:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant