kvstore
The kvstore built-in module is available only inside a PubNub Function. It stores persistent values by subscribe key, so Functions on the same keyset share the same store.
1const db = require('kvstore');
Storage constraints
- A KV store key can be up to 1,000 characters.
- A KV store value can be up to 32,000 characters.
- A KV store entry lives for 1 day by default, and a per-entry TTL can range from 1 minute up to 1 year. A TTL below the minimum becomes the minimum. A TTL above the maximum becomes the maximum.
set
set(key, value, ttl?)
| Parameter | Description |
|---|---|
key *Type: String | Storage key. |
value *Type: Object | JSON-serializable value to store. |
ttlType: Number | Time to live, in minutes. Defaults and clamps as described above. |
Returns: Promise.
1const db = require('kvstore');
2db.set('key', { value: true }, 2880);
The third argument sets the TTL in minutes (2880 minutes = 2 days).
get
get(key)
| Parameter | Description |
|---|---|
key *Type: String | Storage key to retrieve. |
Returns: Promise resolving to the stored value.
1const db = require('kvstore');
2db.get('key').then((value) => {
3 console.log('value', value);
4});
setItem
setItem(key, stringValue, ttl?)
| Parameter | Description |
|---|---|
key *Type: String | Storage key. |
stringValue *Type: String | String value to store. |
ttlType: Number | Time to live, in minutes. Same defaulting and clamping as set(). |
Same as set(), optimized for string values. Returns: Promise.
1const db = require('kvstore');
2db.setItem('key', 'value', 2880);
getItem
getItem(key)
| Parameter | Description |
|---|---|
key *Type: String | Storage key to retrieve. |
Same as get(), optimized for string values. Returns: Promise resolving to the stored string.
1const db = require('kvstore');
2db.getItem('key').then((value) => {
3 console.log('value', value);
4});
removeItem
removeItem(key)
| Parameter | Description |
|---|---|
key *Type: String | Storage key to remove. |
Returns: Promise.
1const db = require('kvstore');
2db.removeItem('key');
getKeys
getKeys(pagination_key?)
| Parameter | Description |
|---|---|
pagination_keyType: String | Last key returned from a previous call. Omit for the first page. |
Returns: Promise resolving to an array of keys, up to the maximum keys per call listed in API limits. Do not rely on a static index (such as result[99]) into the returned array, since the page size can change.
1const db = require('kvstore');
2db.getKeys().then((keys) => {
3 for (let i = 0; i < keys.length; i++) {
4 console.log(keys[i]);
5 }
6});
Counters
Counters increment a stored number atomically, so concurrent Function executions do not overwrite each other's updates. Counters are not subject to TTL.
getCounter
getCounter(key)
| Parameter | Description |
|---|---|
key *Type: String | Counter key to retrieve. |
Returns: Promise resolving to the counter value, or 0 if no counter under key has yet been incremented.
1const db = require('kvstore');
2db.getCounter('key').then((counter) => {
3 console.log('counter', counter);
4});
incrCounter
incrCounter(key, number?)
| Parameter | Description |
|---|---|
key *Type: String | Counter key to increment. Created and initialized to zero if it doesn't exist. |
numberType: Number | Amount to increment by. Defaults to 1. |
Returns: Promise.
1const db = require('kvstore');
2db.incrCounter('key', 5);
getCounterKeys
getCounterKeys(pagination_key?)
| Parameter | Description |
|---|---|
pagination_keyType: String | Last key returned from a previous call. Omit for the first page. |
Returns: Promise resolving to an array of counter keys, up to the maximum keys per call listed in API limits.
1const db = require('kvstore');
2db.getCounterKeys().then((counterKeys) => {
3 for (let i = 0; i < counterKeys.length; i++) {
4 console.log(counterKeys[i]);
5 }
6});
Related reference
- Modules and libraries. Every built-in module available inside a Function.
- API limits. Per-execution operation caps and KV store size limits.