Wraith.Store behaviour (Wraith v0.1.0)

View Source

The one table everything lives in.

Every item is a map with string keys. Two are mandatory — "pk" and "sk" — and two are optional index keys, "gsi1pk"/"gsi1sk", which put the item on the single global secondary index. Everything else is the entity's own business; Wraith.Store.Keys says what goes where.

Adapters (config :wraith, :store):

Writes are plain puts; put/2 with condition: :not_exists is the uniqueness primitive (slugs, emails) and condition: {:version, n} the optimistic-concurrency one (update/3 wraps it). transact/1 applies a handful of puts and deletes atomically (DynamoDB TransactWriteItems).

Summary

Functions

Every item of a query, pages followed. Use with care.

How many items a query would return (begins_with:/between:/index: as for query/2), without reading them out: SELECT count(*) on SQL, Select: COUNT on DynamoDB. For dashboards, not for paging.

The start_key that resumes a query right after item — what query/2 returns as next when item is the last of a page. For callers that filter a page down and need a cursor at the last item they kept.

A fresh, sortable id: UUIDv7 as a lowercase hex string. Ids minted in the same millisecond by this node still sort in minting order (a 12-bit counter in rand_a, UUIDv7 "method 1").

Milliseconds-precision ISO 8601 of now — the sort-key clock.

Items under a partition key (or an index partition when index: :gsi1), sorted by sort key. begins_with: narrows the sort key, forward: false walks newest-first, limit: + the returned next page through.

Read-modify-write with optimistic concurrency. fun gets the current item (or nil) and returns the new one; a concurrent writer makes the put fail with :conflict and we try again, up to five times.

Types

item()

@type item() :: %{required(String.t()) => term()}

key()

@type key() :: {String.t(), String.t()}

op()

@type op() ::
  {:put, item()} | {:put, item(), keyword()} | {:delete, String.t(), String.t()}

page()

@type page() :: %{items: [item()], next: term() | nil}

query_opt()

@type query_opt() ::
  {:begins_with, String.t()}
  | {:between, {String.t(), String.t()}}
  | {:index, :gsi1}
  | {:limit, pos_integer()}
  | {:forward, boolean()}
  | {:start_key, term()}

Callbacks

batch_get(list)

@callback batch_get([key()]) :: {:ok, [item()]} | {:error, term()}

count(t, list)

@callback count(String.t(), [query_opt()]) :: {:ok, non_neg_integer()} | {:error, term()}

cursor(item, boolean)

@callback cursor(item(), boolean()) :: term()

delete(t, t)

@callback delete(String.t(), String.t()) :: :ok

get(t, t)

@callback get(String.t(), String.t()) :: {:ok, item()} | :error

put(item, keyword)

@callback put(item(), keyword()) :: :ok | {:error, :exists | :conflict | term()}

query(t, list)

@callback query(String.t(), [query_opt()]) :: {:ok, page()} | {:error, term()}

transact(list)

@callback transact([op()]) :: :ok | {:error, term()}

Functions

all(pk, opts \\ [])

@spec all(String.t(), [query_opt()]) :: [item()]

Every item of a query, pages followed. Use with care.

batch_get(keys)

@spec batch_get([key()]) :: {:ok, [item()]} | {:error, term()}

count(pk, opts \\ [])

@spec count(String.t(), [query_opt()]) :: non_neg_integer()

How many items a query would return (begins_with:/between:/index: as for query/2), without reading them out: SELECT count(*) on SQL, Select: COUNT on DynamoDB. For dashboards, not for paging.

cursor(item, opts \\ [])

@spec cursor(item(), [query_opt()]) :: term()

The start_key that resumes a query right after item — what query/2 returns as next when item is the last of a page. For callers that filter a page down and need a cursor at the last item they kept.

delete(pk, sk)

@spec delete(String.t(), String.t()) :: :ok

get(pk, sk)

@spec get(String.t(), String.t()) :: {:ok, item()} | :error

id()

A fresh, sortable id: UUIDv7 as a lowercase hex string. Ids minted in the same millisecond by this node still sort in minting order (a 12-bit counter in rand_a, UUIDv7 "method 1").

impl()

now()

Milliseconds-precision ISO 8601 of now — the sort-key clock.

put(item, opts \\ [])

@spec put(item(), keyword()) :: :ok | {:error, term()}

query(pk, opts \\ [])

@spec query(String.t(), [query_opt()]) :: {:ok, page()} | {:error, term()}

Items under a partition key (or an index partition when index: :gsi1), sorted by sort key. begins_with: narrows the sort key, forward: false walks newest-first, limit: + the returned next page through.

transact(ops)

@spec transact([op()]) :: :ok | {:error, term()}

update(pk, sk, fun, attempts \\ 5)

Read-modify-write with optimistic concurrency. fun gets the current item (or nil) and returns the new one; a concurrent writer makes the put fail with :conflict and we try again, up to five times.