pub struct Store { /* private fields */ }
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()?;
store.set("message", "Hello world".as_bytes())?;
Open the default store and get the ‘message’ key:
let store = spin_sdk::key_value::Store::open_default()?;
let message = store.get("message")?;
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")?;
let keys = store.get_keys()?;
Open the default store and delete the ‘message’ key:
let store = spin_sdk::key_value::Store::open_default()?;
store.delete("message")?;
An open key-value store
Implementations§
Source§impl Store
impl Store
Sourcepub fn open_default() -> Result<Self, Error>
pub fn open_default() -> Result<Self, Error>
Open the default store.
This is equivalent to Store::open("default")
.
Source§impl Store
impl Store
Sourcepub fn set_json<T: Serialize>(
&self,
key: impl AsRef<str>,
value: &T,
) -> Result<(), Error>
pub fn set_json<T: Serialize>( &self, key: impl AsRef<str>, value: &T, ) -> Result<(), Error>
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()?;
store.set_json(customer_id, &customer)?;
Sourcepub fn get_json<T: DeserializeOwned>(
&self,
key: impl AsRef<str>,
) -> Result<Option<T>, Error>
pub fn get_json<T: DeserializeOwned>( &self, key: impl AsRef<str>, ) -> Result<Option<T>, Error>
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()?;
let customer = store.get_json::<Customer>(customer_id)?;
Trait Implementations§
Auto Trait Implementations§
impl !Freeze for Store
impl RefUnwindSafe for Store
impl Send for Store
impl Sync for Store
impl Unpin for Store
impl UnwindSafe for Store
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more