Key-Value Store JavaScript API
Everything the Key-Value Store extension adds to pb_hooks and page code, generated from the TypeScript declarations it ships (your app's types.d.ts has the same, so your editor knows them). For how to use it, see its docs; for Go, the Go API.
$kv
The app's key-value store (ext/kv, the kv/v1 contract): named JSON values kept without a collection or a migration, in PocketBase's _params, so they ride along in backups. Names are 1 to 128 characters: letters, digits and _ - . : / , starting with a letter or digit; start yours with a prefix of your own ("myapp/settings"). Values are any JSON up to 1 MiB. Every call is local and sync.
$kv.get()
$kv.get(name: string): any
The value stored under a name, parsed from its JSON.
| Parameter | Type | |
|---|---|---|
name |
string |
the value's name, such as "myapp/settings" |
Returns any: the value, or null when the name has none.
const settings = $kv.get("myapp/settings") ?? { theme: "light" }
Go: kvv1.Get.
$kv.set()
$kv.set(name: string, value: any): void
Stores a value under a name, replacing any value it had.
| Parameter | Type | |
|---|---|---|
name |
string |
the value's name, such as "myapp/settings" |
value |
any |
anything JSON.stringify takes, up to 1 MiB as JSON |
$kv.set("myapp/settings", { theme: "dark", lastRun: new Date().toISOString() })
Go: kvv1.Set.
$kv.delete()
$kv.delete(name: string): void
Removes a name's value. Removing a name with no value isn't an error.
| Parameter | Type | |
|---|---|---|
name |
string |
the value's name |
$kv.delete("myapp/settings")
Go: kvv1.Delete.
$kv.list()
$kv.list(prefix?: string): KvEntry[]
The values whose names start with a prefix, sorted by name.
| Parameter | Type | |
|---|---|---|
prefix |
string (optional) |
the start every name shares, such as "myapp/"; omit it for every value |
Returns KvEntry[].
for (const e of $kv.list("myapp/")) console.log(e.name, e.updated)
Go: kvv1.List.
Types
KvEntry
One value in the app's key-value store.
| Member | Type | |
|---|---|---|
name |
string |
Its name. |
value |
any |
The value, parsed from its JSON. |
created |
string |
When it was first stored (ISO 8601, UTC). |
updated |
string |
When it last changed (ISO 8601, UTC). |
Go: kvv1.Entry.
Questions and comments
Comments are a GitHub Discussions thread; sign in with GitHub to post.