use crate::transaction::TransactionBuilder;
use web_sys::IdbDatabase;
#[derive(Debug)]
pub struct OwnedDatabase {
db: Option<Database>,
}
impl OwnedDatabase {
pub(crate) fn make_auto_close(db: Database) -> OwnedDatabase {
OwnedDatabase { db: Some(db) }
}
pub fn into_manual_close(mut self) -> Database {
self.db.take().expect("Database already taken")
}
pub fn close(self) {
}
}
impl std::ops::Deref for OwnedDatabase {
type Target = Database;
fn deref(&self) -> &Self::Target {
self.db.as_ref().expect("Database already taken")
}
}
impl Drop for OwnedDatabase {
fn drop(&mut self) {
match self.db.take() {
Some(db) => db.close(),
None => {} }
}
}
#[derive(Debug)]
pub struct Database {
sys: IdbDatabase,
}
impl Database {
pub(crate) fn from_sys(sys: IdbDatabase) -> Database {
Database { sys }
}
pub(crate) fn as_sys(&self) -> &IdbDatabase {
&self.sys
}
pub fn name(&self) -> String {
self.sys.name()
}
pub fn version(&self) -> u32 {
self.sys.version() as u32
}
pub fn object_store_names(&self) -> Vec<String> {
let names = self.sys.object_store_names();
let len = names.length();
let mut res = Vec::with_capacity(usize::try_from(len).unwrap());
for i in 0..len {
res.push(
names
.get(i)
.expect("DOMStringList did not contain as many elements as its length"),
);
}
res
}
pub fn transaction(&self, stores: &[&str]) -> TransactionBuilder {
TransactionBuilder::from_names(self.sys.clone(), stores)
}
pub fn close(&self) {
self.sys.close();
}
}