Skip to main content

Key-Value Store

All Less deployments come with a built in key-value store.

Import the Key-Value Store

Import kvs from @chuva.io/less.

const { kvs } = require('@chuva.io/less');

Set Value for Key

The key should be a string. The value can be any primitive (number, object, etc.).

const { kvs } = require('@chuva.io/less');

await kvs.set('MY_KEY', 'This is my value.');

Get Value for Key

Get a value for a key.

const { kvs } = require('@chuva.io/less');

const my_value = await kvs.get('MY_KEY');

Delete Value and Key

Delete a key and its value.

const { kvs } = require('@chuva.io/less');

await kvs.delete('MY_KEY');

Subscribe to KVS changes

The Key-Value Store (KVS) allows you to subscribe to a stream of real-time updates by leveraging Less's Topics/Subscribers (Pub/Sub) feature.

Less Topics Documentation

Subscribe to create events

Create the kvs_created Topic.

mkdir -p less/topics/kvs_created/log
touch less/topics/kvs_created/log/index.js
less/topics/kvs_created/log/index.js
exports.process = async ({ key, new_value }) => {
console.log(`New item created with key: '${key}' and value: '${new_value}'.`);
}

Here is an example kvs_created event payload:

{
"key": "artists",
"new_value": [
{
"first_name": "Cesária",
"last_name": "Évora",
"dob": "08/27/1941"
}
]
}

Subscribe to update events

Create the kvs_updated Topic.

mkdir -p less/topics/kvs_updated/log
touch less/topics/kvs_updated/log/index.js
less/topics/kvs_updated/log/index.js
exports.process = async ({ key, old_value, new_value }) => {
console.log(`Item with with key: '${key}' updated.`);
console.log(`Old value: ${old_value}.`);
console.log(`New value: ${new_value}.`);
}

Here is an example kvs_updated event payload:

{
"key": "artists",
"new_value": [
{
"first_name": "Cesária",
"last_name": "Évora",
"dob": "08/27/1941"
},
{
"first_name": "Mayra",
"last_name": "Andrade",
"dob": "02/13/1985"
}
],
"old_value": [
{
"first_name": "Cesária",
"last_name": "Évora",
"dob": "08/27/1941"
}
]
}

Subscribe delete events

Create the kvs_deleted Topic.

mkdir -p less/topics/kvs_deleted/log
touch less/topics/kvs_deleted/log/index.js
less/topics/kvs_deleted/log/index.js
exports.process = async ({ key, old_value }) => {
console.log(`Item with with key: '${key}' deleted.`);
console.log(`Old value: ${old_value}.`);
}

Here is an example kvs_deleted event payload:

{
"key": "artists",
"old_value": [
{
"first_name": "Cesária",
"last_name": "Évora",
"dob": "08/27/1941"
}
]
}
Less Topics/Subscribers (Pub/Sub) Documentation

Time to live

KVS offers a valuable feature called Time to Live (TTL), which allows you to set an expiration time for an item, leading to its automatic deletion after the specified period. To use this feature, you can add the TTL parameter as a third argument when creating a new item. While the first two parameters are required, the TTL parameter is optional and should be an integer representing the time in seconds until the item expires.

const { kvs } = require('@chuva.io/less');

exports.process = async () => {
// This is two hours represented in seconds
const ttl = 2 * 60 * 60;

// The follow item should be expired within 2 hours after its creation
await kvs.set('UserName', 'Cesaria', ttl);
}