k.DB.keyValue
Site key-value store — shares the same storage as Key-Value Storage in the admin UI
| Execution Context | Prerequisites | Typical Use |
|---|---|---|
Server-side KScript (k.api, <script env="server">, scheduled jobs) | No schema or connection string needed; 4096-byte UTF-8 value limit; use indexedDb/sqlite for structured or large data | Cache short config strings, feature flags, or compact JSON shared with the admin UI |
Quick Start
Write, check, and read back a string or serialized JSON configuration entry:
k.api.post(() => {
k.DB.keyValue.set("featureFlags", JSON.stringify({ beta: true }))
const exists = k.DB.keyValue.contains("featureFlags")
const raw = k.DB.keyValue.get("featureFlags")
return {
exists,
flags: raw ? JSON.parse(raw.toString()) : {}
}
})Quick Reference
| Method / Property | Description | Returns |
|---|---|---|
set(key, value) | Writes or updates a string value (max 4096 UTF-8 bytes) | void |
get(key) / [key] | Reads a value by key (null if missing) | KeyValueObject | null |
contains(key) | Checks whether a key exists | boolean |
remove(key) | Deletes a key | boolean |
length | Number of stored key-value pairs | number |
values | List of all stored values | any[] |
keys | List of all key names (see runtime limitation warning below) | string[] |
Configure in the admin UI
Entries can also be managed in Key-Value Storage; script set() writes to the same store as the admin UI.
Methods & Properties
set(key, value)
Writes or updates a key-value entry.
| Parameter | Type | Required | Description |
|---|---|---|---|
key | string | Yes | Non-empty key name |
value | string | null | Yes | Value; passing null stores a null value (contains(key) returns true while get(key) returns null), whereas passing "" stores an empty string. Non-null UTF-8 byte length must not exceed 4096 bytes, otherwise "Maximum value length reached" is thrown |
Returns: void.
k.DB.keyValue.set("featureFlags", JSON.stringify({ beta: true }))get(key)
Reads an entry by key, returning a KeyValueObject (call toString() to convert to a string; JSON responses also serialize it directly as a string), or null if not found or stored as null. Bracket access (k.DB.keyValue[key]) is equivalent.
| Parameter | Type | Required | Description |
|---|---|---|---|
key | string | Yes | Key name |
Returns: KeyValueObject | null.
const raw = k.DB.keyValue.get("featureFlags")
const text = raw ? raw.toString() : "{}"contains(key)
Checks whether a key exists (returns true even if its stored value is null).
| Parameter | Type | Required | Description |
|---|---|---|---|
key | string | Yes | Key name |
Returns: boolean. true if the key exists; otherwise false.
const exists = k.DB.keyValue.contains("featureFlags")remove(key)
Deletes the specified key.
| Parameter | Type | Required | Description |
|---|---|---|---|
key | string | Yes | Key name to delete |
Returns: boolean. true if the key existed and was removed; false if not found.
const removed = k.DB.keyValue.remove("featureFlags")Collection Properties
Reads the stored key list, value list, or total entry count.
| Property | Type | Description |
|---|---|---|
keys | string[] | All key names currently in the store |
values | any[] | All values currently in the store |
length | number | Number of key-value pairs |
Returns: The corresponding collection or entry count (see Type column above).
Known runtime limitation on keys
In the current runtime, once another endpoint in the server process caches string-array collection conversion, reading k.DB.keyValue.keys can throw a type-cast error. For known keys, use get(key) and contains(key) directly.
k.api.get(() => {
return {
length: k.DB.keyValue.length,
valuesCount: k.DB.keyValue.values.length
}
})Notes
- Value type: Stored as strings; use
JSON.stringify/JSON.parsefor objects. - Key names: Consistent with the admin UI, new keys must start with a letter or digit and be unique.
- Server-side execution: Use in
env="server"CodeBlocks, APIs, or scheduled tasks. - Separate from IndexedDB tables: Do not access key-value data via
k.DB.indexedDb.
TypeScript Definitions
interface kKeyValue extends Record<string, any> {
keys: string[];
values: any[];
length: number;
set(key: string, value: string): void;
get(key: string): KeyValueObject;
contains(key: string): boolean;
remove(key: string): boolean;
}
interface KeyValueObject {
source: string;
getTraceInfo(): Record<string, string>;
}Related Docs
- Key-Value Storage (CMS)
- k.DB Overview
- k.DB.indexedDb — Structured dynamic tables