Manage key-value data from a function
Store, read, and delete keys in a KV Store namespace from a function, with metadata, binary values, streams, and several namespaces.
You store, read, and delete keys in a KV Store namespace from a function, with the Azion.KV client. Every specimen on this page opens the namespace with await Azion.KV.open(name): the constructor is private, and there is no default namespace.
Creating the namespace itself is an Azion API task, and a function cannot do it. For more information, refer to Namespaces.
Prerequisites
- A function you can edit. To create one, refer to Functions quickstart.
- An application the function is instantiated on. To bind the function to one, refer to Instantiate a function on an application.
- A KV Store namespace, created through the Azion API. To create one, refer to Namespaces.
Create the function
The client is a global of Azion Runtime, so the code reaches it with no import line and no credential. To create the function that holds one of the specimens below:
Access Azion Console > Products Menu > Libraries > Functions.
Enter a name for the function. For example: kv-store-handler.
In the Code tab, replace the placeholder code with one of the specimens on this page, and replace my-namespace with your namespace.
The function is saved. It answers a request once a Rules Engine rule runs its instance on the application.
Store a value
put creates a key and replaces the value of a key that already exists, so one call covers both. A string is stored as it is, an object is serialized to JSON, and a third argument carries metadata and an expiry. To store three keys in one invocation:
The handler writes three keys and answers 201. For every option put accepts, refer to KV client.
Read a value
get reads one key and returns the value in the type its second argument names, which defaults to text. A key the namespace does not hold returns null, so the handler checks for it rather than catching an error. To read a string, an object, and a key that may be absent:
The handler answers 404 when greeting is absent, and otherwise returns both values in one JSON body. A key the namespace does not hold reaches the null branch rather than throwing, which is why the check is an equality test and not a try.
Read several keys at once
An array as the first argument of get reads several keys in one call and returns a plain object keyed by key name. A key the namespace does not hold appears in that object with the value null. To write three keys and read four:
The three writes address three different keys, and user-4 comes back as null because the namespace holds no key by that name. The array form accepts only text and json: any other return type throws INVALID_MULTIPLE_GET_RETURN_TYPE.
Read a value with its metadata
getWithMetadata returns an object carrying value and metadata, so one call reads both. The metadata is whatever the write passed in the metadata option. To store a key with metadata and read the pair back:
The handler returns result.value and result.metadata in the same response body.
Delete a key
delete removes one key and takes no array form, so several keys are removed with one call each. A read before the delete tells the handler which of the keys the namespace holds. To delete two keys and answer 404 when it holds neither:
The handler answers 200 with the number of keys it removed, 404 when the namespace holds neither key, and 500 carrying the message of a call that rejected.
Store and read binary data
put accepts an ArrayBuffer, and arrayBuffer as the return type of get reads the bytes back. TextEncoder and TextDecoder convert between a string and that buffer. To store a string as bytes and decode it on the way out:
The handler returns the decoded string, which is the text the write encoded.
Read a value as a stream
stream as the return type returns a ReadableStream instead of the assembled value, so a large value reaches the response without the handler holding it. An absent key still returns null, so the handler checks before it builds the response. To pass a value straight into the response body:
The handler answers 404 when the key is absent, and otherwise streams the value as the response body.
Use a specific namespace
Azion.KV.open binds a client to the namespace it names, so a function that reaches two namespaces opens two clients. The same key name in two namespaces addresses two separate values. To write and read one key in each of two namespaces:
Each config key belongs to its own namespace, so neither write overwrites the other. A name that belongs to no namespace on the account throws NotFound: KV namespace "staging-data" does not exist.