Skip to content

Instantly share code, notes, and snippets.

Avatar

Ben Smith slashdotdash

View GitHub Profile
@slashdotdash
slashdotdash / unique_username.ex
Last active Nov 9, 2022
Unique username command dispatch middleware for Commanded
View unique_username.ex
defmodule UniqueUsername do
@behaviour Commanded.Middleware
alias Commanded.Middleware.Pipeline
def before_dispatch(%Pipeline{command: %RegisterUser{} = command} = pipeline) do
%RegisterUser{username: username} = command
case Repo.insert(%Username{username: username}) do
{:ok, _} ->
@slashdotdash
slashdotdash / big-o.md
Created Jan 11, 2022 — forked from PJUllrich/big-o.md
Big-O Time Complexities for Elixir Data Structures
View big-o.md

Big-O Time Complexities for Elixir data structures

Map [1]

Operation Time Complexity
Access O(log n)
Search O(log n)
Insertion O(n) for < 32 elements, O(log n) for >= 32 elements [2]
Deletion O(n) for < 32 elements, O(log n) for >= 32 elements
@slashdotdash
slashdotdash / docker-postgres.sh
Created Nov 18, 2021
Run Postgres in-memory with Docker
View docker-postgres.sh
docker pull postgres:12-alpine
docker run --rm \
--name postgres10 \
--tmpfs=/pgtmpfs \
-e PGDATA=/pgtmpfs \
-e POSTGRES_PASSWORD=postgres \
-e POSTGRES_USER=postgres \
-p 5432:5432 \
postgres:10-alpine
@slashdotdash
slashdotdash / wait_until.ex
Last active Sep 19, 2021
ExUnit wait until helper function
View wait_until.ex
def wait_until(timeout \\ 1_000, fun)
def wait_until(0, fun), do: fun.()
def wait_until(timeout, fun) do
fun.()
rescue
ExUnit.AssertionError ->
:timer.sleep(10)
View category_stream_linker.ex
defmodule EventStore.CategoryStreamLinker do
@moduledoc """
Links streams from aggregate instances to their respective category streams.
example: events from stream_uuid of `contractors_contract-07c52787-da0c-444f-9783-5d380f7093f9` will be
linked to stream_uuid of `contractors_contract`.
"""
use Commanded.Event.Handler,
application: My.App,
View invalid_event.ex
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
View event_store_expectations.md

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 Sep 8, 2020 — forked from christhekeele/default_behaviour.ex
Behaviours with Defaults for Elixir
View default_behaviour.ex
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 / README.md
Created May 28, 2020
Elixir GenServer with a `handle_info/2` callback will crash when receiving an unexpected message
View README.md

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 Feb 21, 2020
Commanded JSON serializer supporting string keys
View json_serializer.ex
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