PubNub FunctionsKV Store Module

The KV store module is a persistent key value store that acts as a database for your PubNub Functions. This database is globally distributed and eventually consistent.

 
  1. The maximum recursion limit you can do is 3 - hops from one Function to another, using publish or fire, you can execute a maximum of three Functions.
  2. The combined maximum number within a single Function execution of KV store operations, XHRs, publish and fire is 3.

All KV store methods return a promise object.

The KV Store module is made available via the following require statement:

const db = require("kvstore");

KV store data scoped at a subscribe key level – this means that data stored within the KV store is globally replicated across all PoPs, and available for all Functions under the same Keyset to access.

 

The length of the key is 1000 characters max and the length of the value is 32000 characters max.

When data is stored in the KV store, you can set the Time to Live (TTL). By default the TTL is 1 day.

const db = require("kvstore");
db.set("key", {value: true});

Takes a String key as the first parameter, a Javascript Object as the second parameter. Data is stored persistently and is available whenever a block is triggered.

const db = require("kvstore");
db.set("key", {value: true}, 2880);

Takes a String key as the first parameter, a Javascript Object as the second parameter and an int TTL as the third parameter which is set in minutes. Data is stored persistently and is available whenever a block is triggered. TTLcauses expiration of the stored object. In the example the TTL is set to 2 days. The minimum value of TTL is 1 min and maximum 1 year.

const db = require("kvstore");
db.get("key").then((value) => {
    console.log("value", value);
});

Takes a String as the only parameter and retrieves the stored value from the database.

const db = require("kvstore");
db.setItem("key", "value");

Takes a String key as the first parameter and a String value as the second parameter. Same as db.set() but optimized for String values.

const db = require("kvstore");
db.setItem("key", "value", 2880);

Takes a String key as the first parameter, a String value as the second parameter and an int TTL as the third parameter which is set in minutes. TTLcauses expiration of the stored object. In the example the TTL is set to 2 days. The minimum value of TTL is 1 day and maximum 1 year.

const db = require("kvstore");
db.getItem("key").then((value) => {
    console.log("value", value);
});

Takes a String as the only parameter and retrieves the stored value from the database. Same as db.get() but optimized for String values.

const db = require("kvstore");
db.removeItem("key");

Takes a String as the only parameter and removes the stored value from the database.

const db = require("kvstore");
db.getKeys().then((keys) => {
        for(var i=0; i<keys.length;i++){
            console.log(keys[i])
        }
        
    });

List all keys from the KV Store.

The method returns a maximum of 100 keys. The method accepts a string argument for paginating results. The pagination_key should be the last key returned from a previous response (or undefined for the first response). Using a static index like result[99] would break code when the limit is changed.

Counters are an efficient way to increment a stored number in an atomic fashion. Counters stored in the key/value storage are not subjected to TTL.
const db = require("kvstore");
db.getCounter("key").then((counter) => {
    console.log("counter", counter);
});

Takes a String as the only parameter and retrieves the stored counter value from the database. Returns 0L if no counter under key has yet been incremented.

const db = require("kvstore");
db.incrCounter("key", number);

Takes a String as the first parameter and a number as the second parameter. Increments counter stored under key by number. Creates a new counter initialized to zero if none yet exist under key. If number is not set, defaults to 1.

const db = require("kvstore");
db.getCounterKeys().then((counterKeys) => {
        for(var i=0; i<counterKeys.length;i++){
            console.log(counterKeys[i])
        }
    });

List all keys from the KV Store Counter.

The method returns a maximum of 100 keys. The method accepts a string argument for paginating results. The pagination_key should be the last key returned from a previous response (or undefined for the first response). Using a static index like result[99] would break code when the limit is changed.

 
PubNub Functions provides a rich set of tools, and this documentation does not cover all of the potential situations you may encounter. If you need help with a situation not covered by the documentation, please contact PubNub Support.