NATS KV Store

Read, write, list, and delete keys in a NATS JetStream Key-Value bucket.

Operations

OperationDescription
getRead a value by key.
putWrite event.data.content to key; without key, write every entry of event.data.entries under key_prefix.
listList keys matching a prefix.
deleteDelete a key.

Configuration

- nats_kv_store:
    name: write_flow
    operation: put
    credentials_path: /etc/nats/credentials.json
    url: "{{env.NATS_URL}}"
    bucket: flowgen_system
    key: "flows.{{event.data.path}}"

Fields

FieldTypeDefaultDescription
namestringrequiredTask name.
operationstringrequiredOne of get, put, list, delete.
credentials_pathstringoptionalPath to NATS credentials file.
urlstringlocalhost:4222NATS server URL.
bucketstringrequiredKV bucket name.
keystringKey for get, put, and delete. Supports templating.
key_prefixstringKey prefix for list, and for the keys of put entries. Supports templating.
pruneboolfalseFor a put of entries: delete the keys under key_prefix that no entry names. Requires a key_prefix ending in . or /.
allow_emptyboolfalseWith prune: let an empty entries list delete every key under key_prefix. Otherwise it fails and deletes nothing.
include_valuesboolfalseFor list: also return each key’s value under values.
depends_onlistUpstream task names.
retryobjectRetry configuration.

Output

Format: JSON

get

FieldTypeDescription
keystringRequested key.
contentstring / nullStored value, or null if not found.
foundboolWhether the key exists.

put

FieldTypeDescription
keystringWritten key.
revisionintKV store revision number.

A put of entries returns instead:

FieldTypeDescription
putarrayKeys written because they were new or changed.
deletedarrayKeys removed by prune.
unchangedintEntries that already held their value.

delete

FieldTypeDescription
keystringDeleted key.

list

FieldTypeDescription
keysarrayMatching key names.
countintNumber of keys returned.
prefixstringPrefix that was searched.

Examples

Write to KV

- nats_kv_store:
    name: save_config
    operation: put
    bucket: flowgen_system
    key: "config.{{event.data.name}}"
    credentials_path: /etc/nats/credentials.json
    url: "{{env.NATS_URL}}"

Mirror a set of keys

The incoming event carries {"entries": [{"key": "orders/sync", "value": "..."}]}. Keys under key_prefix end up exactly as listed: new and changed values are written, identical ones are left alone, and keys that are no longer listed are deleted.

- nats_kv_store:
    name: apply_flows
    operation: put
    bucket: flowgen_system
    key_prefix: "flows.platform/"
    prune: true
    credentials_path: /etc/nats/credentials.json
    url: "{{env.NATS_URL}}"

Read from KV

- nats_kv_store:
    name: load_config
    operation: get
    bucket: flowgen_system
    key: "config.my_setting"
    credentials_path: /etc/nats/credentials.json
    url: "{{env.NATS_URL}}"

Returns {key, content, found}. content is null if the key does not exist.

List keys

- nats_kv_store:
    name: list_flows
    operation: list
    bucket: flowgen_system
    key_prefix: "flows."
    credentials_path: /etc/nats/credentials.json
    url: "{{env.NATS_URL}}"

Returns {prefix, keys, count}.

Delete a key

- nats_kv_store:
    name: remove_config
    operation: delete
    bucket: flowgen_system
    key: "config.old_setting"
    credentials_path: /etc/nats/credentials.json
    url: "{{env.NATS_URL}}"