View Source Gnat.Jetstream.API.KV (gnat v1.9.0)

API for interacting with the Key/Value store functionality in Nats Jetstream.

Learn about the Key/Value store: https://docs.nats.io/nats-concepts/jetstream/key-value-store

Link to this section Summary

Functions

Get all the non-deleted key-value pairs for a Bucket

Create a new Key/Value bucket. Can include the following options

Create a Key in a Key/Value Bucket

Delete a Key/Value bucket

Delete a Key from a K/V Bucket

Get the value for a key in a particular K/V bucket

Returns true if the provided stream is a KV bucket, false otherwise

Returns a list of all the buckets in the KV

Purge a Key from a K/V bucket. This will remove any revision history the key had

Put a value into a Key in a K/V Bucket

Stops a previously running monitor. This will unsubscribe from the key changes and remove the ephemeral consumer

Starts a monitor for key changes in a given bucket. Supply a handler that will receive key change notifications.

Link to this section Types

Specs

bucket_options() ::
  {:history, non_neg_integer()}
  | {:ttl, non_neg_integer()}
  | {:max_bucket_size, non_neg_integer()}
  | {:max_value_size, non_neg_integer()}
  | {:description, binary()}
  | {:replicas, non_neg_integer()}
  | {:storage, :file | :memory}
  | {:placement, Gnat.Jetstream.API.Stream.placement()}

Link to this section Functions

Link to this function

contents(conn, bucket_name, domain \\ nil)

View Source

Specs

contents(conn :: Gnat.t(), bucket_name :: binary(), domain :: nil | binary()) ::
  {:ok, map()} | {:error, binary()}

Get all the non-deleted key-value pairs for a Bucket

Examples

iex>{:ok, %{"key1" => "value1"}} = Jetstream.API.KV.contents(:gnat, "my_bucket")
Link to this function

create_bucket(conn, bucket_name, params \\ [])

View Source

Specs

create_bucket(
  conn :: Gnat.t(),
  bucket_name :: binary(),
  params :: [bucket_options()]
) ::
  {:ok, Gnat.Jetstream.API.Stream.info()} | {:error, any()}

Create a new Key/Value bucket. Can include the following options

  • :history - How many historic values to keep per key (defaults to 1, max of 64)
  • :ttl - How long to keep values for (in nanoseconds)
  • :max_bucket_size - The max number of bytes the bucket can hold
  • :max_value_size - The max number of bytes a value may be
  • :description - A description for the bucket
  • :replicas - How many replicas of the data to store
  • :storage - Storage backend to use (:file, :memory)
  • :placement - A map with :cluster (required) and :tags (optional)

Examples

iex>{:ok, info} = Jetstream.API.KV.create_bucket(:gnat, "my_bucket")

Link to this function

create_key(conn, bucket_name, key, value, opts \\ [])

View Source

Create a Key in a Key/Value Bucket

Examples

iex>:ok = Jetstream.API.KV.create_key(:gnat, "my_bucket", "my_key", "my_value")
Link to this function

delete_bucket(conn, bucket_name)

View Source

Specs

delete_bucket(conn :: Gnat.t(), bucket_name :: binary()) ::
  :ok | {:error, any()}

Delete a Key/Value bucket

Examples

iex>:ok = Jetstream.API.KV.delete_bucket(:gnat, "my_bucket")

Link to this function

delete_key(conn, bucket_name, key, opts \\ [])

View Source

Delete a Key from a K/V Bucket

Examples

iex>:ok = Jetstream.API.KV.delete_key(:gnat, "my_bucket", "my_key")
Link to this function

get_value(conn, bucket_name, key)

View Source

Specs

get_value(conn :: Gnat.t(), bucket_name :: binary(), key :: binary()) ::
  binary() | {:error, any()} | nil

Get the value for a key in a particular K/V bucket

Examples

iex>"my_value" = Jetstream.API.KV.get_value(:gnat, "my_bucket", "my_key")
Link to this function

is_kv_bucket_stream?(stream_name)

View Source

Specs

is_kv_bucket_stream?(stream_name :: binary()) :: boolean()

Returns true if the provided stream is a KV bucket, false otherwise

Parameters

  • stream_name - the stream name to test

Specs

list_buckets(conn :: Gnat.t()) :: {:error, term()} | {:ok, [String.t()]}

Returns a list of all the buckets in the KV

Link to this function

purge_key(conn, bucket_name, key, opts \\ [])

View Source

Purge a Key from a K/V bucket. This will remove any revision history the key had

Examples

iex>:ok = Jetstream.API.KV.purge_key(:gnat, "my_bucket", "my_key")
Link to this function

put_value(conn, bucket_name, key, value, opts \\ [])

View Source

Put a value into a Key in a K/V Bucket

Examples

iex>:ok = Jetstream.API.KV.put_value(:gnat, "my_bucket", "my_key", "my_value")

Stops a previously running monitor. This will unsubscribe from the key changes and remove the ephemeral consumer

Examples

iex>:ok = Jetstream.API.KV.unwatch(pid)
Link to this function

watch(conn, bucket_name, handler)

View Source

Starts a monitor for key changes in a given bucket. Supply a handler that will receive key change notifications.

Examples

iex>{:ok, _pid} = Jetstream.API.KV.watch(:gnat, "my_bucket", fn action, key, value ->
  IO.puts("#{action} taken on #{key}")
end)