Store

Struct Store 

Source
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

Source

pub fn open_default() -> Result<Self, Error>

Open the default store.

This is equivalent to Store::open("default").

Source§

impl Store

Source

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)?;
Source

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)?;
Source§

impl Store

Source

pub fn open(label: &str) -> Result<Store, 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.

Source§

impl Store

Source

pub fn get(&self, key: &str) -> Result<Option<Vec<u8>>, Error>

Get the value associated with the specified key

Returns ok(none) if the key does not exist.

Source§

impl Store

Source

pub fn set(&self, key: &str, value: &[u8]) -> Result<(), Error>

Set the value associated with the specified key overwriting any existing value.

Source§

impl Store

Source

pub fn delete(&self, key: &str) -> Result<(), Error>

Delete the tuple with the specified key

No error is raised if a tuple did not previously exist for key.

Source§

impl Store

Source

pub fn exists(&self, key: &str) -> Result<bool, Error>

Return whether a tuple exists for the specified key

Source§

impl Store

Source

pub fn get_keys(&self) -> Result<Vec<String>, Error>

Return a list of all the keys

Trait Implementations§

Source§

impl Debug for Store

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V