Skip to content

Key-value database

The following functions in the KV module are exposed as soon as a keyvalue_db integration is added to this bot (either directly or through installing a skill).

KV.count(kv, pattern \\ nil, opts \\ [])

Get the key count from the KV store

Returns the total number of keys that are in the store; if the pattern is given, it returns the number of keys that match the pattern.

KV.erase(kv, key, opts \\ [])

Erases the given key from the KV store.

Returns 1 when the key was erased, or 0 if no such key was found.

KV.erase_all(kv, opts \\ [])

Erases all keysfrom the KV store.

Returns the number of keys that were erased.

KV.erase_many(kv, pattern, opts \\ [])

Erases the keys matching the given pattern from the KV store.

For the 'pattern' argument, see the 'get_many' function.

Returns the number of keys that were erased.

KV.get(kv, key, opts \\ [])

Retrieve a single value for the given key from the KV store.

KV.get_all(kv, opts \\ [])

Retrieve all values from the KV store for the given pattern.

The return value of this function is always a map, in which the keys are all the keys in the store, and the values are their corresponding values.

KV.get_many(kv, pattern, opts \\ [])

Retrieve all values from the KV store for the given pattern.

The pattern can contain '%' characters which are used in the SQL 'like' expression that is used for the lookup. The pattern is case sensitive.

In addition to a single pattern, a list of patterns (or key names) can also be passed in.

The return value of this function is always a map, in which the keys are the keys that matched the patterns, and the values are their corresponding values.

KV.keys(kv, pattern, opts \\ [])

Retrieves the keys from the KV store for the given pattern.

The pattern can contain '%' characters which are used in the SQL 'like' expression that is used for the lookup. The pattern is case sensitive.

In addition to a single pattern, a list of patterns (or key names) can also be passed in.

Returns list of keys.

KV.put(kv, key, value, opts \\ [])

Put a value in the given key in the KV store.

Options:

  • :ttl - Expiry value; can be a number (in seconds), or a time-interval string like "1d", "2h", "10s".

KV.put_many(kv, key_values, opts \\ [])

Put multiple key/value pairs in the given key in the KV store.

The key_values argument is a map with string keys, each value in the map will become an entry in the KV database with the corresponding key.

Options:

  • :ttl - Expiry value; can be a number (in seconds), or a time-interval string like "1d","2h","10s"`.