33//! This module provides a generic interface for key-value storage, which may be implemented by the host various
44//! ways (e.g. via an in-memory table, a local file, or a remote database). Details such as consistency model and
55//! durability will depend on the implementation and may vary from one to store to the next.
6+ //!
7+ //! # Examples
8+ //!
9+ //! Open the default store and set the 'message' key:
10+ //!
11+ //! ```no_run
12+ //! # fn main() -> anyhow::Result<()> {
13+ //! let store = spin_sdk::key_value::Store::open_default()?;
14+ //! store.set("message", "Hello world".as_bytes())?;
15+ //! # Ok(())
16+ //! # }
17+ //! ```
618
719use super :: wit:: v2:: key_value;
820
921#[ cfg( feature = "json" ) ]
1022use serde:: { de:: DeserializeOwned , Serialize } ;
1123
1224#[ doc( inline) ]
13- pub use key_value:: { Error , Store } ;
25+ pub use key_value:: Error ;
26+
27+ /// An open key-value store.
28+ ///
29+ /// # Examples
30+ ///
31+ /// Open the default store and set the 'message' key:
32+ ///
33+ /// ```no_run
34+ /// # fn main() -> anyhow::Result<()> {
35+ /// let store = spin_sdk::key_value::Store::open_default()?;
36+ /// store.set("message", "Hello world".as_bytes())?;
37+ /// # Ok(())
38+ /// # }
39+ /// ```
40+ ///
41+ /// Open the default store and get the 'message' key:
42+ ///
43+ /// ```no_run
44+ /// # fn main() -> anyhow::Result<()> {
45+ /// let store = spin_sdk::key_value::Store::open_default()?;
46+ /// let message = store.gettt("message")?;
47+ /// let response = message.unwrap_or_else(|| "not found".into());
48+ /// # Ok(())
49+ /// # }
50+ /// ```
51+ ///
52+ /// Open a named store and list all the keys defined in it:
53+ ///
54+ /// ```no_run
55+ /// # fn main() -> anyhow::Result<()> {
56+ /// let store = spin_sdk::key_value::Store::open("finance")?;
57+ /// let keys = store.get_keys()?;
58+ /// # Ok(())
59+ /// # }
60+ /// ```
61+ ///
62+ /// Open the default store and delete the 'message' key:
63+ ///
64+ /// ```no_run
65+ /// # fn main() -> anyhow::Result<()> {
66+ /// let store = spin_sdk::key_value::Store::open_default()?;
67+ /// store.delete("message")?;
68+ /// # Ok(())
69+ /// # }
70+ /// ```
71+ #[ doc( inline) ]
72+ pub use key_value:: Store ;
1473
1574impl Store {
1675 /// Open the default store.
@@ -24,6 +83,31 @@ impl Store {
2483impl Store {
2584 #[ cfg( feature = "json" ) ]
2685 /// Serialize the given data to JSON, then set it as the value for the specified `key`.
86+ ///
87+ /// # Examples
88+ ///
89+ /// Open the default store and save a customer information document against the customer ID:
90+ ///
91+ /// ```no_run
92+ /// # use serde::{Deserialize, Serialize};
93+ /// #[derive(Deserialize, Serialize)]
94+ /// struct Customer {
95+ /// name: String,
96+ /// address: Vec<String>,
97+ /// }
98+ ///
99+ /// # fn main() -> anyhow::Result<()> {
100+ /// let customer_id = "CR1234567";
101+ /// let customer = Customer {
102+ /// name: "Alice".to_owned(),
103+ /// address: vec!["Wonderland Way".to_owned()],
104+ /// };
105+ ///
106+ /// let store = spin_sdk::key_value::Store::open_default()?;
107+ /// store.set_json(customer_id, &customer)?;
108+ /// # Ok(())
109+ /// # }
110+ /// ```
27111 pub fn set_json < T : Serialize > (
28112 & self ,
29113 key : impl AsRef < str > ,
@@ -34,6 +118,27 @@ impl Store {
34118
35119 #[ cfg( feature = "json" ) ]
36120 /// Deserialize an instance of type `T` from the value of `key`.
121+ ///
122+ /// # Examples
123+ ///
124+ /// Open the default store and retrieve a customer information document by customer ID:
125+ ///
126+ /// ```no_run
127+ /// # use serde::{Deserialize, Serialize};
128+ /// #[derive(Deserialize, Serialize)]
129+ /// struct Customer {
130+ /// name: String,
131+ /// address: Vec<String>,
132+ /// }
133+ ///
134+ /// # fn main() -> anyhow::Result<()> {
135+ /// let customer_id = "CR1234567";
136+ ///
137+ /// let store = spin_sdk::key_value::Store::open_default()?;
138+ /// let customer = store.get_json::<Customer>(customer_id)?;
139+ /// # Ok(())
140+ /// # }
141+ /// ```
37142 pub fn get_json < T : DeserializeOwned > (
38143 & self ,
39144 key : impl AsRef < str > ,
0 commit comments