pig/workspace/kv
Key-value store operations for pig workspace.
Types
Custom error type for key-value operations.
pub type Error {
SqlError(sqlight.Error)
NotFound(key: String)
}
Constructors
-
SqlError(sqlight.Error) -
NotFound(key: String)
Values
pub fn list_keys(
conn: sqlight.Connection,
prefix: String,
) -> Result(List(String), Error)
List all keys that start with the given prefix, sorted alphabetically.
Escapes SQL LIKE wildcards in the prefix so they match literally.
pub fn recall(
conn: sqlight.Connection,
key: String,
) -> Result(String, Error)
Retrieve a value by key.
Returns an error if the key doesn’t exist.
Example
case kv.recall(conn, "user:1") {
Ok(name) -> io.println("Found: " <> name)
Error(kv.NotFound(key)) -> io.println("Key not found: " <> key)
Error(_) -> io.println("Database error")
}
pub fn remember(
conn: sqlight.Connection,
key: String,
value: String,
) -> Result(Nil, Error)
Store a key-value pair, updating the value if the key already exists.
Uses an upsert operation (INSERT … ON CONFLICT DO UPDATE) to handle both new keys and updates to existing keys atomically.
Example
let assert Ok(Nil) = kv.remember(conn, "user:1", "Alice")
let assert Ok(Nil) = kv.remember(conn, "user:1", "Alice Smith") // Updates