Skip to content

Instantly share code, notes, and snippets.

View slashdotdash's full-sized avatar

Ben Smith slashdotdash

View GitHub Profile
defmodule InvalidEvent do
@doc """
Module to be used when an event cannot be deserialized from the event store.
The payload field will be populated with the source event data if
deserialization is possible.
Receiving this event usually indicates an event has been removed or renamed in
the source code between releases. Deserializing to this invalid event module
allows the application to continue running, otherwise it would terminate as
@slashdotdash
slashdotdash / Gruntfile.js
Created February 19, 2014 22:15
Grunt config to with task to compile React .jsx files to .js
/*global module:false*/
module.exports = function(grunt) {
grunt.initConfig({
react: {
jsx: {
files: [
{
expand: true,
cwd: 'public/js',

Expectations

What I expect from some system managing storage of streams of events, intended to be used in an event-sourced system.

  • ability to create streams
  • ability to delete streams
  • ability to use optimistic locking on stream level
  • ability to write a batch of events in one stream atomically and durably
  • ability to read events from one stream
  • ability to read all events in the store ( the so called "$all" stream)
@slashdotdash
slashdotdash / default_behaviour.ex
Created September 8, 2020 10:40 — forked from christhekeele/default_behaviour.ex
Behaviours with Defaults for Elixir
defmodule Default.Behaviour do
@moduledoc """
Creates a behaviour that carries its own default implementation.
When used into a behaviour module, when that module in turn is used, all functions
defined on it are given to the using module.
This allows you to have concrete implementations of the behaviour's default functionality
for testing, unlike cramming them all into a __using__ macro.
@slashdotdash
slashdotdash / upsert_profile.ex
Last active July 26, 2020 23:09
Upserts in Ecto using `Ecto.Multi`
defp upsert_profile(multi, source, source_uuid, profile_url) do
profile = %Profile{
source: source,
source_uuid: source_uuid,
profile: profile_url,
}
Ecto.Multi.insert(multi, source_uuid, profile, on_conflict: [set: [profile: profile_url]], conflict_target: [:source, :source_uuid])
end
@slashdotdash
slashdotdash / telemetry.ex
Last active July 13, 2020 08:42
A Commanded middleware to instrument the command dispatch pipeline with `:telemetry` events.
defmodule Commanded.Middleware.Telemetry do
@moduledoc """
A Commanded middleware to instrument the command dispatch pipeline with
`:telemetry` events.
It produces the following three events:
- `[:commanded, :command, :dispatch, :start]`
- `[:commanded, :command, :dispatch, :success]`
- `[:commanded, :command, :dispatch, :failure]`
@slashdotdash
slashdotdash / event_telemetry.ex
Created May 3, 2019 11:13
A Commanded event handler to produce `:telemetry` events for each recorded event.
defmodule Commanded.EventTelemetry do
@moduledoc """
A Commanded event handler to produce `:telemetry` events for each recorded
event.
It produces the following event:
- `[:commanded, :event, :published]`
"""
@slashdotdash
slashdotdash / README.md
Created May 28, 2020 14:53
Elixir GenServer with a `handle_info/2` callback will crash when receiving an unexpected message

Elixir GenServer with a handle_info/2 callback will crash when receiving an unexpected message

Define a GenServer with a handle_info callback function:

defmodule Echo do
  use GenServer

  def start_link(reply_to) do
    GenServer.start_link(__MODULE__, reply_to)
@slashdotdash
slashdotdash / json_serializer.ex
Created February 21, 2020 15:33
Commanded JSON serializer supporting string keys
defmodule JsonSerializer do
alias Commanded.EventStore.TypeProvider
alias Commanded.Serialization.JsonDecoder
@doc """
Serialize given term to JSON binary data.
"""
def serialize(term) do
Jason.encode!(term)
end
@slashdotdash
slashdotdash / README.md
Created February 18, 2020 15:23
Publish to an Absinthe GraphQL subscription