Skip to content

k.DB.keyValue ​

Site key-value store — shares the same storage as Key-Value Storage in the admin UI

Execution ContextPrerequisitesTypical 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 dataCache 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:

js
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 / PropertyDescriptionReturns
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 existsboolean
remove(key)Deletes a keyboolean
lengthNumber of stored key-value pairsnumber
valuesList of all stored valuesany[]
keysList 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.

ParameterTypeRequiredDescription
keystringYesNon-empty key name
valuestring | nullYesValue; 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.

js
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.

ParameterTypeRequiredDescription
keystringYesKey name

Returns: KeyValueObject | null.

js
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).

ParameterTypeRequiredDescription
keystringYesKey name

Returns: boolean. true if the key exists; otherwise false.

js
const exists = k.DB.keyValue.contains("featureFlags")

remove(key) ​

Deletes the specified key.

ParameterTypeRequiredDescription
keystringYesKey name to delete

Returns: boolean. true if the key existed and was removed; false if not found.

js
const removed = k.DB.keyValue.remove("featureFlags")

Collection Properties ​

Reads the stored key list, value list, or total entry count.

PropertyTypeDescription
keysstring[]All key names currently in the store
valuesany[]All values currently in the store
lengthnumberNumber 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.

js
k.api.get(() => {
    return {
        length: k.DB.keyValue.length,
        valuesCount: k.DB.keyValue.values.length
    }
})

Notes ​

  1. Value type: Stored as strings; use JSON.stringify / JSON.parse for objects.
  2. Key names: Consistent with the admin UI, new keys must start with a letter or digit and be unique.
  3. Server-side execution: Use in env="server" CodeBlocks, APIs, or scheduled tasks.
  4. Separate from IndexedDB tables: Do not access key-value data via k.DB.indexedDb.

TypeScript Definitions ​

ts
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>;
}