use std::rc::Rc;
use crate::err::Error;
use crate::tx::Transaction;
use rexie::ObjectStore;
use rexie::Rexie;
pub struct Database {
pub(crate) datastore: Rc<Rexie>,
}
impl Database {
pub async fn new(path: &str) -> Result<Self, Error> {
let store = ObjectStore::new("kv");
match Rexie::builder(path).version(1).add_object_store(store).build().await {
Ok(db) => Ok(Database {
datastore: Rc::new(db),
}),
Err(_) => Err(Error::DbError),
}
}
pub async fn begin(&self, write: bool) -> Result<Transaction, Error> {
Ok(Transaction::new(Rc::clone(&self.datastore), write))
}
}