Key-Value API
Run Redis-like operations against a Key-Value datastore. This API automatically available from the inside of any Codehook function.
API quick overview
Datastore.open()
Datastore.open() opens a connection to a datastore in the active project space.
import {Datastore} from 'codehooks-js'
async function myfunc() {
const conn = await Datastore.open();
// use conn to call API functions
...
}
Returns a Promise with a Datastore connection
set(key, value options)
Set a key-value pair in the a datastore.
Parameters
- key: Unique key in keyspace
- value: Any string value
- options: (optional)
- ttl: Time to live in milliseconds
- keyspace: Name of isolated keyspace
Returns A Promise with result string value
Code example
const ONE_DAY = 86400000; // 1000 * 60 * 60 * 24
const conn = await Datastore.open();
const opt = {
"ttl": ONE_DAY,
"keyspace": "sessions"
};
const result = await conn.set('my_special_key', 'f439a2d4-d21b-44ea-97ac-f210120f10f3', opt);
get(key, options)
Get a key-value pair in the a datastore.
Parameters
- key: Unique key in keyspace
- options: (optional)
- keyspace: Name of isolated keyspace
Returns A Promise with result string value
Code example
const conn = await Datastore.open();
const opt = {
"keyspace": "sessions"
};
const kval = await conn.get('my_special_key', opt);
console.log("key-val", kval);
getAll(keypattern, options)
Get all key-value pair that matches keypattern in the a datastore.
Parameters
- keypattern: Key pattern matches key starting with string
- options: (optional)
- keyspace: Name of isolated keyspace
Returns Promise / JSON object stream
Code example
const conn = await Datastore.open();
const opt = {
"keyspace": "sessions"
};
const stream = await conn.getAll('my_special_', opt);
stream.on('data', (data) => {
console.log(data);
}).on('end', () => {
console.log('The end');
})
incr(key, number, options)
Increment a key-value pair in the a datastore.
Parameters
- key: Unique key in keyspace
- number: Value to increment >= 1
- options: (optional)
- ttl: Time to live in milliseconds
- keyspace: Name of isolatedd keyspace
Returns A Promise with result string value
Code example
const conn = await Datastore.open();
const kval = await conn.incr('counter', 1);
console.log("key-val", kval);
decr(key, number, options)
Decrement a key-value pair in the a datastore.
Parameters
- key: Unique key in keyspace
- number: Value to decrement <= 1
- options: (optional)
- ttl: Time to live in milliseconds
- keyspace: Name of isolatedd keyspace
Returns A Promise with result string value
Code example
const conn = await Datastore.open();
const kval = await conn.decr('counter', 1);
console.log("key-val", kval);
del(key, options)
Delete a key-value pair in the a datastore.
Parameters
- key: Unique key in keyspace
- options: (optional)
- keyspace: Name of isolatedd keyspace
Returns A Promise with result string value
Code example
const conn = await Datastore.open();
const kval = await conn.del('my_special_key');
console.log("key-val", kval);
delAll(key, options)
Delete a range of key-value pairs in the a datastore.
Parameters
- keypattern: Key pattern in keyspace that matches start of string
- options: (optional)
- keyspace: Name of isolatedd keyspace
Returns A Promise with result JSON object
Code example
const conn = await Datastore.open();
const kval = await conn.delAll('my_special_');
console.log("key-val", kval);