EventStore is available in Hex and can be installed as follows:
-
Add eventstore to your list of dependencies in
mix.exs
:def deps do [{:eventstore, "~> 0.16"}] end
-
Add an
eventstore
config entry containing the PostgreSQL database connection details to each environment's mix config file (e.g.config/dev.exs
):config :eventstore, EventStore.Storage, serializer: EventStore.TermSerializer, username: "postgres", password: "postgres", database: "eventstore_dev", hostname: "localhost", pool_size: 10, pool_overflow: 5
The database connection pool configuration options are:
:pool_size
- The number of connections (default:10
).:pool_overflow
- The maximum number of overflow connections to start if all connections are checked out (default:0
).
-
Create the EventStore database and tables using the
mix
task:$ mix do event_store.create, event_store.init
You can use an existing PostgreSQL database with EventStore by running the mix
task:
$ mix event_store.init
To drop an existing EventStore database and recreate it you can run the following mix
tasks:
$ mix do event_store.drop, event_store.create, event_store.init
Warning this will delete all EventStore data.
EventStore has support for persisting event data and metadata as either:
- Binary data, using the
bytea
data type, designed for storing binary strings. - JSON data, using the
jsonb
data type, specifically designed for storing JSON encoded data.
The default data type is bytea
. This can be used with any binary serializer, such as the Erlang Term format, JSON data encoded to binary, and other serialization formats.
The advantage of using this format is that it allows you to execute ad-hoc SQL queries against the event data or metadata.
If you want to use PostgreSQL's native JSON support you need to configure EventStore to use the jsonb
data type. You must also use the EventStore.JsonbSerializer
serializer to ensure event data and metadata is correctly serialized to JSON and include the Postgres types module (EventStore.PostgresTypes
) for the Postgrex library to support JSON.
# config/config.exs
config :eventstore, column_data_type: "jsonb"
config :eventstore, EventStore.Storage,
serializer: EventStore.JsonbSerializer,
types: EventStore.PostgresTypes
Finally, you need to include the Jason library in mix.exs
to enable Postgrex type with JSON support.
# mix.exs
defp deps do
[{:jason, "~> 1.1"}]
end
These settings must be configured before creating the EventStore database. It's not possible to migrate between bytea
and jsonb
data types once you've created the database. This must be decided in advance.