kurrentdb/stream_metadata

Stream metadata builders and JSON encoding.

KurrentDB stores stream metadata by appending a JSON event to the metadata stream for a regular stream. The metadata stream name is the original stream name prefixed with $$.

This module models KurrentDB’s well-known metadata keys, ACL shape, and custom user fields. It can decode metadata read from KurrentDB and encode metadata ready to write with a backend or the sans-IO metadata operation.

let metadata =
  stream_metadata.new()
  |> stream_metadata.max_count(100)
  |> stream_metadata.cache_control(30)

Types

Access control list metadata for a stream.

Each field is a list of role names. Empty lists mean that the corresponding ACL entry is not emitted when the metadata is encoded to JSON.

pub type StreamAcl {
  StreamAcl(
    read_roles: List(String),
    write_roles: List(String),
    delete_roles: List(String),
    meta_read_roles: List(String),
    meta_write_roles: List(String),
  )
}

Constructors

  • StreamAcl(
      read_roles: List(String),
      write_roles: List(String),
      delete_roles: List(String),
      meta_read_roles: List(String),
      meta_write_roles: List(String),
    )

KurrentDB stream metadata.

Known KurrentDB metadata fields are represented explicitly. Additional application-defined fields are preserved in custom as JSON values and are emitted alongside the known $... metadata keys.

pub type StreamMetadata {
  StreamMetadata(
    max_count: option.Option(Int),
    max_age: option.Option(Int),
    truncate_before: option.Option(Int),
    cache_control: option.Option(Int),
    acl: option.Option(StreamAcl),
    custom: List(#(String, json.Json)),
  )
}

Constructors

Values

pub fn acl(
  metadata: StreamMetadata,
  acl: StreamAcl,
) -> StreamMetadata

Set the ACL on stream metadata.

Use new_acl and the ACL builder functions to create the ACL value.

pub fn cache_control(
  metadata: StreamMetadata,
  cache_control: Int,
) -> StreamMetadata

Set $cacheControl on stream metadata, in seconds.

This controls how long clients may cache stream metadata.

pub fn custom(
  metadata: StreamMetadata,
  key: String,
  value: json.Json,
) -> StreamMetadata

Add a custom key-value field to stream metadata.

Custom fields are encoded as top-level JSON properties. If the same key is set multiple times the latest value replaces the previous one.

pub fn decode(
  data: BitArray,
) -> Result(StreamMetadata, json.DecodeError)

Decode stream metadata JSON from a KurrentDB metadata event body.

Known $... fields are decoded into the explicit record fields. Invalid JSON returns the original gleam/json.DecodeError.

pub fn delete_roles(
  acl: StreamAcl,
  roles: List(String),
) -> StreamAcl

Set the $d delete roles on an ACL.

pub fn max_age(
  metadata: StreamMetadata,
  max_age: Int,
) -> StreamMetadata

Set $maxAge on stream metadata, in seconds.

Events older than this value may be scavenged according to server policy.

pub fn max_count(
  metadata: StreamMetadata,
  max_count: Int,
) -> StreamMetadata

Set $maxCount on stream metadata.

$maxCount limits how many events KurrentDB should keep for the stream. Older events may be scavenged according to server policy.

pub fn meta_read_roles(
  acl: StreamAcl,
  roles: List(String),
) -> StreamAcl

Set the $mr metadata-read roles on an ACL.

pub fn meta_write_roles(
  acl: StreamAcl,
  roles: List(String),
) -> StreamAcl

Set the $mw metadata-write roles on an ACL.

pub fn metadata_stream_name(from stream_name: String) -> String

Return the metadata stream name for a regular stream.

KurrentDB stores metadata for orders in $$orders.

pub fn new() -> StreamMetadata

Construct empty stream metadata.

All known fields are unset, no ACL is present, and there are no custom fields. Use the builder functions in this module to set individual values.

pub fn new_acl() -> StreamAcl

Construct an empty StreamAcl with no roles set.

Empty ACL entries are not encoded, so an untouched ACL encodes as an empty JSON object.

pub fn read_roles(
  acl: StreamAcl,
  roles: List(String),
) -> StreamAcl

Set the $r read roles on an ACL.

pub fn to_json(metadata: StreamMetadata) -> json.Json

Encode stream metadata as JSON.

Unset known fields and empty ACL role lists are omitted from the resulting JSON object. Custom fields are included as top-level properties.

pub fn truncate_before(
  metadata: StreamMetadata,
  truncate_before: Int,
) -> StreamMetadata

Set $tb (truncate before) revision on stream metadata.

Events before this revision are considered logically truncated for reads.

pub fn write_roles(
  acl: StreamAcl,
  roles: List(String),
) -> StreamAcl

Set the $w write roles on an ACL.

Search Document