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 a named store and list all the keys defined in it:
42+ ///
43+ /// ```no_run
44+ /// # fn main() -> anyhow::Result<()> {
45+ /// let store = spin_sdk::key_value::Store::open("finance")?;
46+ /// let keys = store.get_keys()?;
47+ /// # Ok(())
48+ /// # }
49+ /// ```
50+ ///
51+ /// Open the default store and delete the 'message' key:
52+ ///
53+ /// ```no_run
54+ /// # fn main() -> anyhow::Result<()> {
55+ /// let store = spin_sdk::key_value::Store::open_default()?;
56+ /// store.delete("message")?;
57+ /// # Ok(())
58+ /// # }
59+ /// ```
60+ #[ doc( inline) ]
61+ pub use key_value:: Store ;
1462
1563impl Store {
1664 /// Open the default store.
@@ -24,6 +72,31 @@ impl Store {
2472impl Store {
2573 #[ cfg( feature = "json" ) ]
2674 /// Serialize the given data to JSON, then set it as the value for the specified `key`.
75+ ///
76+ /// # Examples
77+ ///
78+ /// Open the default store and save a customer information document against the customer ID:
79+ ///
80+ /// ```no_run
81+ /// # use serde::{Deserialize, Serialize};
82+ /// #[derive(Deserialize, Serialize)]
83+ /// struct Customer {
84+ /// name: String,
85+ /// address: Vec<String>,
86+ /// }
87+ ///
88+ /// # fn main() -> anyhow::Result<()> {
89+ /// let customer_id = "CR1234567";
90+ /// let customer = Customer {
91+ /// name: "Alice".to_owned(),
92+ /// address: vec!["Wonderland Way".to_owned()],
93+ /// };
94+ ///
95+ /// let store = spin_sdk::key_value::Store::open_default()?;
96+ /// store.set_json(customer_id, &customer)?;
97+ /// # Ok(())
98+ /// # }
99+ /// ```
27100 pub fn set_json < T : Serialize > (
28101 & self ,
29102 key : impl AsRef < str > ,
@@ -34,6 +107,27 @@ impl Store {
34107
35108 #[ cfg( feature = "json" ) ]
36109 /// Deserialize an instance of type `T` from the value of `key`.
110+ ///
111+ /// # Examples
112+ ///
113+ /// Open the default store and retrieve a customer information document by customer ID:
114+ ///
115+ /// ```no_run
116+ /// # use serde::{Deserialize, Serialize};
117+ /// #[derive(Deserialize, Serialize)]
118+ /// struct Customer {
119+ /// name: String,
120+ /// address: Vec<String>,
121+ /// }
122+ ///
123+ /// # fn main() -> anyhow::Result<()> {
124+ /// let customer_id = "CR1234567";
125+ ///
126+ /// let store = spin_sdk::key_value::Store::open_default()?;
127+ /// let customer = store.get_json::<Customer>(customer_id)?;
128+ /// # Ok(())
129+ /// # }
130+ /// ```
37131 pub fn get_json < T : DeserializeOwned > (
38132 & self ,
39133 key : impl AsRef < str > ,
0 commit comments