Use KV Store with a Redis-compatible client
Reach KV Store through a Redis-like client interface, with the familiar set, get, delete, and hash operations mapped onto Azion's library.
The azion library exposes a Redis-like client for KV Store, so an application already written against Redis patterns keeps the shape of its calls. This guide covers the client, its options, the operations it carries, and how each one maps to the Redis command it stands in for.
Prerequisites
- An Azion account. To create one, refer to How to create an account on Azion.
- A KV Store namespace. It is created through the Azion API; refer to Namespaces.
- Node.js 18 or later, or a compatible JavaScript runtime.
Install the library
Create a client
The client follows a Redis-like pattern, with chainable methods and a connection step:
Client options
| Option | Type | Description |
|---|---|---|
namespace | string | The KV Store namespace the client addresses |
apiToken | string | An Azion API token, required by the API provider |
Store a value
set writes a value, with optional expiration and metadata:
Expiration types
| Type | Description |
|---|---|
EX | Expiration time in seconds |
PX | Expiration time in milliseconds |
EXAT | Unix timestamp in seconds |
PXAT | Unix timestamp in milliseconds |
Read a value
get returns the value, or null when the namespace holds no such key:
Read a value with its metadata
getWithMetadata returns the value and the metadata that was written with it:
Delete a key
delete removes a key, and del is an alias for it:
Hash operations
The client carries Redis-compatible hash operations for field-value pairs under one key. Each one has an uppercase alias.
hSet
Writes one field of a hash:
hGetAll
Returns every field and value of a hash:
hVals
Returns every value of a hash, without the field names:
Provider detection
The client detects the runtime it is executing in and selects a provider:
- Native provider: selected inside the Azion Runtime, where a function executes.
- API provider: selected outside Azion, in local development or on an external server.
getProviderType reports which one is in use:
Handle errors
The client reports failures through an error event:
A single operation is also wrapped in try/catch:
Close the connection
Close the client when the work is finished:
Complete example
The operations above, composed into one run:
Redis method mapping
| Redis command | Client method | Description |
|---|---|---|
GET | get(key) | Read a value |
SET | set(key, value, options?) | Write a value |
DEL | delete(key) / del(key) | Remove a key |
HSET | hSet(key, field, value) / HSET(...) | Write a hash field |
HGETALL | hGetAll(key) / HGETALL(key) | Read every hash field |
HVALS | hVals(key) / HVALS(key) | Read every hash value |