The KVS (Key-Value Store) serves as a convenient, high-level data storing mechanism. The KVS is an abstraction layer on top of the configured Botpress Database
- Source:
Example
bp.kvs
bp.db.kvs // ⚠️ Deprecated, will be removed in Botpress 11
Methods
(async, static) get(key, pathopt) → (nullable) {*}
Returns the unserialized value stored at a given key
- Source:
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
key |
String
|
The unique key where you want to get the value at |
|
path |
String
|
<optional> |
If specified, returns the value at the path inside the retrieved object (if stored object was an object). |
Returns:
- Type:
-
*
Returns the unserialized object of any type stored at that key or null if it doesn't exist
Example
// Assuming 'user001' is an Object like `{ profile: { first_name: "Sylvain" } }`
const first_name = await bp.kvs.get('user001', 'profile.first_name')
const fullUser = await bp.kvs.get('user001')
// You can also retrieve array elements
const first_subscriber = await bp.kvs.get('subscribers', '[0].name')
(async, static) set(key, value, pathopt)
Serializes and stores any value at the specified key, and optionally set a value inside an existing object at path
.
- Source:
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
key |
String
|
The unique key of the value you want to store |
|
value |
*
|
The value to store. Note that if you provide an object or array, it will be serialized to JSON automatically. Therefore, you have to make sure that your object is serializable (i.e. it has no circular references) |
|
path |
String
|
<optional> |
The path inside the object to set the value (see example) |
Example
const user = { profile: { name: 'Sylvain' } }
await bp.kvs.set('user001', user)
// You can later overwrite the `name` property directly
await bp.kvs.set('user001', 'Sylvain Perron', 'name')