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
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")
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")
Create a Key in a Key/Value Bucket
Examples
iex>:ok = Jetstream.API.KV.create_key(:gnat, "my_bucket", "my_key", "my_value")
Specs
Delete a Key/Value bucket
Examples
iex>:ok = Jetstream.API.KV.delete_bucket(:gnat, "my_bucket")
Delete a Key from a K/V Bucket
Examples
iex>:ok = Jetstream.API.KV.delete_key(:gnat, "my_bucket", "my_key")
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")
Specs
Returns true if the provided stream is a KV bucket, false otherwise
Parameters
stream_name
- the stream name to test
Specs
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
Examples
iex>:ok = Jetstream.API.KV.purge_key(:gnat, "my_bucket", "my_key")
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)
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)