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
Argument | Type | Description |
---|---|---|
key | string | The key to store the value under |
value | string | The text value to store |
// "await" to wait until the value is storedawait 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
Argument | Type | Description |
---|---|---|
key | string | The key the value is stored under |
defaultValue Optional | string | Optional default value to use if the key is not present |
Present key:
// "await" to wait until the value is storedawait 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
Argument | Type | Description |
---|---|---|
key | string | The key to remove |
// "await" to wait until the key is removedawait api.kv.remove("test");
setNumber
Function (key: string, value: number) => Promise<void>
Store a number value in the key value store
Argument | Type | Description |
---|---|---|
key | string | The key to store the value under |
value | number | The number value to store |
// "await" to wait until the value is storedawait 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
Argument | Type | Description |
---|---|---|
key | string | The key the value is stored under |
defaultValue Optional | number | Optional default value to use if the key is not present |
Present key:
// "await" to wait until the value is storedawait 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
Argument | Type | Description |
---|---|---|
key | string | The key to store the value under |
value | T[] | The array value to store |
// "await" to wait until the value is storedawait 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
Argument | Type | Description |
---|---|---|
key | string | The key the value is stored under |
defaultValue Optional | T[] | Optional default value to use if the key is not present |
Present key:
// "await" to wait until the value is storedawait 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
Argument | Type | Description |
---|---|---|
key | string | The key to store the value under |
value | T | The array value to store |
// "await" to wait until the value is storedawait 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
Argument | Type | Description |
---|---|---|
key | string | The key the value is stored under |
defaultValue Optional | number | Optional default value to use if the key is not present |
Present key:
// "await" to wait until the value is storedawait 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
Argument | Type | Description |
---|---|---|
key | string | The 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 counterawait 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
Argument | Type | Description |
---|---|---|
value | number | The 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
Argument | Type | Description |
---|---|---|
amount Optional | number | Amount to increase the counter by (Default: 1) |
const counter = api.kv.createCounter("myCounter");const value = await counter.get();console.log(value); // 0
// Increase counterawait 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 counterawait counter.increase(50);
const value2 = await counter.get();console.log(value2); // 50
decrease
Method (this: Counter, amount?: number) => Promise<void>
Decrease the counter
Argument | Type | Description |
---|---|---|
amount Optional | number | Amount to decrease the counter by (Default: 1) |
const counter = api.kv.createCounter("myCounter");const value = await counter.get();console.log(value); // 0
// Decrease counterawait 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 counterawait counter.decrease(50);
const value2 = await counter.get();console.log(value2); // 50