silverbullet/website/API/datastore.md

63 lines
1.1 KiB
Markdown
Raw Normal View History

2025-01-17 03:41:52 +08:00
The Datastore API provides functions for interacting with a key-value store that has query capabilities.
2025-01-17 17:41:02 +08:00
# Key-Value Operations
2025-01-17 03:41:52 +08:00
2025-01-17 17:41:02 +08:00
## datastore.set(key, value)
2025-01-17 03:41:52 +08:00
Sets a value in the key-value store.
Example:
```lua
datastore.set("user:123", {name = "John", age = 30})
```
2025-01-17 17:41:02 +08:00
## datastore.get(key)
2025-01-17 03:41:52 +08:00
Gets a value from the key-value store.
Example:
```lua
local user = datastore.get("user:123")
print(user.name) -- prints "John"
```
2025-01-17 17:41:02 +08:00
## datastore.del(key)
2025-01-17 03:41:52 +08:00
Deletes a value from the key-value store.
Example:
```lua
datastore.del("user:123")
```
2025-01-17 17:41:02 +08:00
# Batch Operations
2025-01-17 03:41:52 +08:00
## datastore.batchSet(kvs)
2025-01-17 03:41:52 +08:00
Sets multiple key-value pairs in a single operation.
Example:
```lua
local kvs = {
{key = "user:1", value = {name = "Alice"}},
{key = "user:2", value = {name = "Bob"}}
}
datastore.batchSet(kvs)
2025-01-17 03:41:52 +08:00
```
## datastore.batchGet(keys)
2025-01-17 03:41:52 +08:00
Gets multiple values in a single operation.
Example:
```lua
local keys = {"user:1", "user:2"}
local values = datastore.batchGet(keys)
2025-01-17 03:41:52 +08:00
for _, value in ipairs(values) do
print(value.name)
end
```
## datastore.batchDel(keys)
2025-01-17 03:41:52 +08:00
Deletes multiple values in a single operation.
Example:
```lua
local keys = {"user:1", "user:2"}
datastore.batchDel(keys)