Skip to content

Key Value Store

KV store provides a persistent way to keep track of state for things like counters

Functions

setText

Function (key: string, value: string) => Promise<void>

Store a text value in the key value store

ArgumentTypeDescription
keystringThe key to store the value under
valuestringThe text value to store
// "await" to wait until the value is stored
await api.kv.setText("test", "This is a test");
const text = await api.kv.getText("test");
console.log(text); // "This is a test"

getText

Function (key: string, defaultValue?: string) => Promise<string | null>

Get a text value from the key value store

ArgumentTypeDescription
keystringThe key the value is stored under
defaultValue OptionalstringOptional default value to use if the key is not present

Present key:

// "await" to wait until the value is stored
await api.kv.setText("test", "This is a test");
const text = await api.kv.getText("test");
console.log(text); // "This is a test"

Missing key:

const text = await api.kv.getText("test");
console.log(text); // null

Missing key with default:

const text = await api.kv.getText("test", "My Default");
console.log(text); // "My Default"

remove

Function (key: string) => Promise<void>

Remove a key from the key value store

ArgumentTypeDescription
keystringThe key to remove
// "await" to wait until the key is removed
await api.kv.remove("test");

setNumber

Function (key: string, value: number) => Promise<void>

Store a number value in the key value store

ArgumentTypeDescription
keystringThe key to store the value under
valuenumberThe number value to store
// "await" to wait until the value is stored
await api.kv.setNumber("test", 5);
const value = await api.kv.getNumber("test");
console.log(value); // 5

getNumber

Function (key: string, defaultValue?: number) => Promise<number | null>

Get a number value from the key value store

ArgumentTypeDescription
keystringThe key the value is stored under
defaultValue OptionalnumberOptional default value to use if the key is not present

Present key:

// "await" to wait until the value is stored
await api.kv.setNumber("test", 5);
const value = await api.kv.getNumber("test");
console.log(value); // 5

Missing key:

const value = await api.kv.getNumber("test");
console.log(value); // null

Missing key with default:

const value = await api.kv.getNumber("test", 5);
console.log(value); // 5

setArray

Function <T>(key: string, value: T[]) => Promise<void>

Store an array value in the key value store

ArgumentTypeDescription
keystringThe key to store the value under
valueT[]The array value to store
// "await" to wait until the value is stored
await api.kv.setArray("test", ["Test", "Test 1", "Test 2"]);
const value = await api.kv.getArray("test");
console.log(value); // ["Test", "Test 1", "Test 2"]

getArray

Function <T>(key: string, defaultValue?: T[]) => Promise<T[] | null>

Get an array value from the key value store

ArgumentTypeDescription
keystringThe key the value is stored under
defaultValue OptionalT[]Optional default value to use if the key is not present

Present key:

// "await" to wait until the value is stored
await api.kv.setArray("test", ["Test", "Test 1", "Test 2"]);
const value = await api.kv.getArray("test");
console.log(value); // ["Test", "Test 1", "Test 2"]

Missing key:

const value = await api.kv.getArray("test");
console.log(value); // null

Missing key with default:

const value = await api.kv.getArray("test", ["Test"]);
console.log(value); // ["Test"]

setObject

Function <T>(key: string, value: T) => Promise<void>

Store an object value in the key value store

ArgumentTypeDescription
keystringThe key to store the value under
valueTThe array value to store
// "await" to wait until the value is stored
await api.kv.setObject("test", { a: "b" });
const value = await api.kv.getObject("test");
console.log(value); // { "a": "b" }

getObject

Function <T>(key: string, defaultValue?: T) => Promise<T | null>

Get an object value from the key value store

ArgumentTypeDescription
keystringThe key the value is stored under
defaultValue OptionalnumberOptional default value to use if the key is not present

Present key:

// "await" to wait until the value is stored
await api.kv.setObject("test", { a: "b" });
const value = await api.kv.getObject("test");
console.log(value); // { "a": "b" }

Missing key:

const value = await api.kv.getObject("test");
console.log(value); // null

Missing key with default:

const value = await api.kv.getObject("test", { foo: "bar" });
console.log(value); // { "foo": "bar" }

createCounter

Function (key: string) => Counter

Get an object value from the key value store

ArgumentTypeDescription
keystringThe key the value is stored under

Present key:

const counter = api.kv.createCounter("myCounter");
// .. Work with the counter

Classes

Counter

Helper for making a simple counter. Useful for making a counter for a specific thing (i.e times you’ve won)

get

Method (this: Counter) => Promise<number>

Get the current value of the counter. Will return zero for newly created counters

const counter = api.kv.createCounter("myCounter");
const value = await counter.get();
console.log(value); // 0
// Increase counter
await counter.increase();
const value2 = await counter.get();
console.log(value2); // 1

set

Method (this: Counter, value: number) => Promise<void>

Sets the current value of the counter

ArgumentTypeDescription
valuenumberThe new value for the counter
const counter = api.kv.createCounter("myCounter");
const value = await counter.get();
console.log(value); // 0
await counter.set(50);
const value2 = await counter.get();
console.log(value2); // 50

increase

Method (this: Counter, amount?: number) => Promise<void>

Increase the counter

ArgumentTypeDescription
amount OptionalnumberAmount to increase the counter by (Default: 1)
const counter = api.kv.createCounter("myCounter");
const value = await counter.get();
console.log(value); // 0
// Increase counter
await counter.increase();
const value2 = await counter.get();
console.log(value2); // 1

By a specific amount:

const counter = api.kv.createCounter("myCounter");
const value = await counter.get();
console.log(value); // 0
// Increase counter
await counter.increase(50);
const value2 = await counter.get();
console.log(value2); // 50

decrease

Method (this: Counter, amount?: number) => Promise<void>

Decrease the counter

ArgumentTypeDescription
amount OptionalnumberAmount to decrease the counter by (Default: 1)
const counter = api.kv.createCounter("myCounter");
const value = await counter.get();
console.log(value); // 0
// Decrease counter
await counter.decrease();
const value2 = await counter.get();
console.log(value2); // 1

By a specific amount:

const counter = api.kv.createCounter("myCounter");
const value = await counter.get();
console.log(value); // 0
// Decrease counter
await counter.decrease(50);
const value2 = await counter.get();
console.log(value2); // 50