[go: up one dir, main page]

Storage

Struct Storage 

Source
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

Source

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.

Source

pub async fn get_multiple( &self, keys: Vec<impl Deref<Target = str>>, ) -> Result<Map>

Retrieves the values associated with each of the provided keys.

Source

pub async fn put<T: Serialize>(&self, key: &str, value: T) -> Result<()>

Stores the value and associates it with the given key.

Source

pub async fn put_raw(&self, key: &str, value: impl Into<JsValue>) -> Result<()>

Source

pub async fn put_multiple<T: Serialize>(&self, values: T) -> Result<()>

Takes a serializable struct and stores each of its keys and values to storage.

Source

pub async fn put_multiple_raw(&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);
Source

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

Deletes the key and associated value. Returns true if the key existed or false if it didn’t.

Source

pub async fn delete_multiple( &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.

Source

pub async fn delete_all(&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.

Source

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.

Source

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.

Source

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.

Source

pub async fn get_alarm_with_options( &self, options: GetAlarmOptions, ) -> Result<Option<i64>>

Source

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.

Source

pub async fn set_alarm_with_options( &self, scheduled_time: impl Into<ScheduledTime>, options: SetAlarmOptions, ) -> Result<()>

Source

pub async fn delete_alarm(&self) -> Result<()>

Deletes the alarm if one exists. Does not cancel the alarm handler if it is currently executing.

Source

pub async fn delete_alarm_with_options( &self, options: SetAlarmOptions, ) -> Result<()>

Source

pub async fn transaction<F, Fut>(&self, closure: F) -> Result<()>
where F: FnOnce(Transaction) -> Fut + 'static, Fut: Future<Output = Result<()>> + 'static,

Source

pub fn sql(&self) -> SqlStorage

Trait Implementations§

Source§

impl Debug for Storage

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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

Source§

impl<T> ErasedDestructor for T
where T: 'static,