pub struct Storage { /* private fields */ }
Expand description
Access a Durable Object’s Storage API. Each method is implicitly wrapped inside a transaction, such that its results are atomic and isolated from all other storage operations, even when accessing multiple key-value pairs.
Implementations§
source§impl Storage
impl Storage
sourcepub async fn get<T: DeserializeOwned>(&self, key: &str) -> Result<T>
pub async fn get<T: DeserializeOwned>(&self, key: &str) -> Result<T>
Retrieves the value associated with the given key. The type of the returned value will be whatever was previously written for the key.
Returns Err if the key does not exist.
sourcepub async fn get_multiple(
&self,
keys: Vec<impl Deref<Target = str>>,
) -> Result<Map>
pub async fn get_multiple( &self, keys: Vec<impl Deref<Target = str>>, ) -> Result<Map>
Retrieves the values associated with each of the provided keys.
sourcepub async fn put<T: Serialize>(&mut self, key: &str, value: T) -> Result<()>
pub async fn put<T: Serialize>(&mut self, key: &str, value: T) -> Result<()>
Stores the value and associates it with the given key.
pub async fn put_raw( &mut self, key: &str, value: impl Into<JsValue>, ) -> Result<()>
sourcepub async fn put_multiple<T: Serialize>(&mut self, values: T) -> Result<()>
pub async fn put_multiple<T: Serialize>(&mut self, values: T) -> Result<()>
Takes a serializable struct and stores each of its keys and values to storage.
sourcepub async fn put_multiple_raw(&mut self, values: Object) -> Result<()>
pub async fn put_multiple_raw(&mut self, values: Object) -> Result<()>
Takes an object and stores each of its keys and values to storage.
use worker::JsValue;
let obj = js_sys::Object::new();
js_sys::Reflect::set(&obj, &JsValue::from_str("foo"), JsValue::from_u64(1));
storage.put_multiple_raw(obj);
sourcepub async fn delete(&mut self, key: &str) -> Result<bool>
pub async fn delete(&mut self, key: &str) -> Result<bool>
Deletes the key and associated value. Returns true if the key existed or false if it didn’t.
sourcepub async fn delete_multiple(
&mut self,
keys: Vec<impl Deref<Target = str>>,
) -> Result<usize>
pub async fn delete_multiple( &mut self, keys: Vec<impl Deref<Target = str>>, ) -> Result<usize>
Deletes the provided keys and their associated values. Returns a count of the number of key-value pairs deleted.
sourcepub async fn delete_all(&mut self) -> Result<()>
pub async fn delete_all(&mut self) -> Result<()>
Deletes all keys and associated values, effectively deallocating all storage used by the Durable Object. In the event of a failure while the operation is still in flight, it may be that only a subset of the data is properly deleted.
sourcepub async fn list(&self) -> Result<Map>
pub async fn list(&self) -> Result<Map>
Returns all keys and values associated with the current Durable Object in ascending lexicographic sorted order.
Be aware of how much data may be stored in your Durable Object before calling this version
of list without options, because it will all be loaded into the Durable Object’s memory,
potentially hitting its limit.
If that is a concern, use the alternate list_with_options()
method.
sourcepub async fn list_with_options(&self, opts: ListOptions<'_>) -> Result<Map>
pub async fn list_with_options(&self, opts: ListOptions<'_>) -> Result<Map>
Returns keys associated with the current Durable Object according to the parameters in the provided options object.
sourcepub async fn get_alarm(&self) -> Result<Option<i64>>
pub async fn get_alarm(&self) -> Result<Option<i64>>
Retrieves the current alarm time (if set) as integer milliseconds since epoch.
The alarm is considered to be set if it has not started, or if it has failed
and any retry has not begun. If no alarm is set, get_alarm()
returns None
.
pub async fn get_alarm_with_options( &self, options: GetAlarmOptions, ) -> Result<Option<i64>>
sourcepub async fn set_alarm(
&self,
scheduled_time: impl Into<ScheduledTime>,
) -> Result<()>
pub async fn set_alarm( &self, scheduled_time: impl Into<ScheduledTime>, ) -> Result<()>
Sets the current alarm time to the given datetime.
If set_alarm()
is called with a time equal to or before Date.now(), the alarm
will be scheduled for asynchronous execution in the immediate future. If the
alarm handler is currently executing in this case, it will not be canceled.
Alarms can be set to millisecond granularity and will usually execute within
a few milliseconds after the set time, but can be delayed by up to a minute
due to maintenance or failures while failover takes place.
pub async fn set_alarm_with_options( &self, scheduled_time: impl Into<ScheduledTime>, options: SetAlarmOptions, ) -> Result<()>
sourcepub async fn delete_alarm(&self) -> Result<()>
pub async fn delete_alarm(&self) -> Result<()>
Deletes the alarm if one exists. Does not cancel the alarm handler if it is currently executing.