refactor(indexeddb): add transaction fns for getting the next available media content id
Signed-off-by: Michael Goldenberg <m@mgoldenberg.net>
This commit is contained in:
committed by
Damir Jelić
parent
c258368925
commit
b4d702f1ef
@@ -81,7 +81,9 @@ impl From<TransactionError> for EventCacheStoreError {
|
||||
match value {
|
||||
DomException { .. } => Self::InvalidData { details: value.to_string() },
|
||||
Serialization(e) => Self::Serialization(serde_json::Error::custom(e.to_string())),
|
||||
ItemIsNotUnique | ItemNotFound => Self::InvalidData { details: value.to_string() },
|
||||
ItemIsNotUnique | ItemNotFound | NumericalOverflow => {
|
||||
Self::InvalidData { details: value.to_string() }
|
||||
}
|
||||
Backend(e) => GenericError::from(e.to_string()).into(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -71,7 +71,9 @@ impl From<TransactionError> for MediaStoreError {
|
||||
match value {
|
||||
DomException { .. } => Self::InvalidData { details: value.to_string() },
|
||||
Serialization(e) => Self::Serialization(serde_json::Error::custom(e.to_string())),
|
||||
ItemIsNotUnique | ItemNotFound => Self::InvalidData { details: value.to_string() },
|
||||
ItemIsNotUnique | ItemNotFound | NumericalOverflow => {
|
||||
Self::InvalidData { details: value.to_string() }
|
||||
}
|
||||
Backend(e) => GenericError::from(e.to_string()).into(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -348,6 +348,25 @@ impl<'a> IndexeddbMediaStoreTransaction<'a> {
|
||||
self.get_item_by_key_components::<MediaContent, IndexedMediaContentIdKey>(id).await
|
||||
}
|
||||
|
||||
/// Query IndexedDB for the maximum [`IndexedMediaContentIdKey`] associated
|
||||
/// with a [`MediaContent`]
|
||||
pub async fn get_max_media_content_key_by_id(
|
||||
&self,
|
||||
) -> Result<Option<IndexedMediaContentIdKey>, TransactionError> {
|
||||
self.get_max_key::<MediaContent, IndexedMediaContentIdKey>(IndexedKeyRange::all(
|
||||
self.serializer().inner(),
|
||||
))
|
||||
.await
|
||||
}
|
||||
|
||||
/// Query IndexedDB for the next available [`MediaContent::id`]
|
||||
pub async fn get_next_media_content_id(&self) -> Result<u64, TransactionError> {
|
||||
Ok(match self.get_max_media_content_key_by_id().await? {
|
||||
Some(key) => key.checked_add(1).ok_or(TransactionError::NumericalOverflow)?,
|
||||
None => 0,
|
||||
})
|
||||
}
|
||||
|
||||
/// Adds [`MediaContent`] to IndexedDB. If an item with the same key already
|
||||
/// exists, it will be rejected.
|
||||
pub async fn add_media_content(&self, content: &MediaContent) -> Result<(), TransactionError> {
|
||||
|
||||
@@ -45,6 +45,8 @@ pub enum TransactionError {
|
||||
ItemIsNotUnique,
|
||||
#[error("item not found")]
|
||||
ItemNotFound,
|
||||
#[error("a numerical operation overflowed")]
|
||||
NumericalOverflow,
|
||||
#[error("backend: {0}")]
|
||||
Backend(Box<dyn AsyncErrorDeps>),
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user