pub struct Store(/* private fields */);key-value only.Expand description
An open key-value store.
§Examples
Open the default store and set the ‘message’ key:
let store = spin_sdk::key_value::Store::open_default().await?;
store.set("message", "Hello world".as_bytes()).await?;Open the default store and get the ‘message’ key:
let store = spin_sdk::key_value::Store::open_default().await?;
let message = store.get("message").await?;
let response = message.unwrap_or_else(|| "not found".into());Open a named store and list all the keys defined in it:
let store = spin_sdk::key_value::Store::open("finance").await?;
let keys = store.get_keys().await;
println!("{:?}", keys.collect().await?);Open the default store and delete the ‘message’ key:
let store = spin_sdk::key_value::Store::open_default().await?;
store.delete("message").await?;Implementations§
Source§impl Store
impl Store
Sourcepub async fn open_default() -> Result<Self, Error>
pub async fn open_default() -> Result<Self, Error>
Open the default store.
This is equivalent to Store::open("default").await.
Source§impl Store
impl Store
Sourcepub async fn open(label: impl AsRef<str>) -> Result<Self, Error>
pub async fn open(label: impl AsRef<str>) -> Result<Self, Error>
Open the store with the specified label.
label must refer to a store allowed in the spin.toml manifest.
error::no-such-store will be raised if the label is not recognized.
Sourcepub async fn get(&self, key: impl AsRef<str>) -> Result<Option<Vec<u8>>, Error>
pub async fn get(&self, key: impl AsRef<str>) -> Result<Option<Vec<u8>>, Error>
Get the value associated with the specified key
Returns ok(none) if the key does not exist.
Sourcepub async fn set(
&self,
key: impl AsRef<str>,
value: impl AsRef<[u8]>,
) -> Result<(), Error>
pub async fn set( &self, key: impl AsRef<str>, value: impl AsRef<[u8]>, ) -> Result<(), Error>
Set the value associated with the specified key overwriting any existing value.
Sourcepub async fn delete(&self, key: impl AsRef<str>) -> Result<(), Error>
pub async fn delete(&self, key: impl AsRef<str>) -> Result<(), Error>
Delete the tuple with the specified key
No error is raised if a tuple did not previously exist for key.
Sourcepub async fn exists(&self, key: impl AsRef<str>) -> Result<bool, Error>
pub async fn exists(&self, key: impl AsRef<str>) -> Result<bool, Error>
Return whether a tuple exists for the specified key
Sourcepub async fn set_json<T: Serialize>(
&self,
key: impl AsRef<str>,
value: &T,
) -> Result<(), Error>
Available on crate feature json only.
pub async fn set_json<T: Serialize>( &self, key: impl AsRef<str>, value: &T, ) -> Result<(), Error>
json only.Serialize the given data to JSON, then set it as the value for the specified key.
§Examples
Open the default store and save a customer information document against the customer ID:
#[derive(Deserialize, Serialize)]
struct Customer {
name: String,
address: Vec<String>,
}
let customer_id = "CR1234567";
let customer = Customer {
name: "Alice".to_owned(),
address: vec!["Wonderland Way".to_owned()],
};
let store = spin_sdk::key_value::Store::open_default().await?;
store.set_json(customer_id, &customer).await?;Sourcepub async fn get_json<T: DeserializeOwned>(
&self,
key: impl AsRef<str>,
) -> Result<Option<T>, Error>
Available on crate feature json only.
pub async fn get_json<T: DeserializeOwned>( &self, key: impl AsRef<str>, ) -> Result<Option<T>, Error>
json only.Deserialize an instance of type T from the value of key.
§Examples
Open the default store and retrieve a customer information document by customer ID:
#[derive(Deserialize, Serialize)]
struct Customer {
name: String,
address: Vec<String>,
}
let customer_id = "CR1234567";
let store = spin_sdk::key_value::Store::open_default().await?;
let customer = store.get_json::<Customer>(customer_id).await?;