diff --git a/lib/src/doc/document.rs b/lib/src/doc/document.rs index 852ae0a7..433673d9 100644 --- a/lib/src/doc/document.rs +++ b/lib/src/doc/document.rs @@ -1,3 +1,4 @@ +use crate::dbs::Level; use crate::dbs::Options; use crate::dbs::Transaction; use crate::dbs::Workable; @@ -52,7 +53,15 @@ impl<'a> Document<'a> { // Get the record id let id = self.id.as_ref().unwrap(); // Get the table definition - txn.clone().lock().await.get_tb(opt.ns(), opt.db(), &id.tb).await + let tb = txn.clone().lock().await.get_tb(opt.ns(), opt.db(), &id.tb).await; + // Return the table or attempt to define it + match tb { + Ok(tb) => Ok(tb), + Err(e) => match opt.auth.check(Level::Db) { + true => txn.clone().lock().await.add_tb(opt.ns(), opt.db(), &id.tb).await, + false => Err(e), + }, + } } // Get the events for this document pub async fn ev( diff --git a/lib/src/kvs/tx.rs b/lib/src/kvs/tx.rs index 8edede08..a6d47540 100644 --- a/lib/src/kvs/tx.rs +++ b/lib/src/kvs/tx.rs @@ -714,44 +714,37 @@ impl Transaction { Ok(val.into()) } /// Add a namespace with a default configuration. - pub async fn add_ns(&mut self, ns: &str) -> Result<(), Error> { + pub async fn add_ns(&mut self, ns: &str) -> Result { let key = crate::key::ns::new(ns); - let _ = self - .put( - key, - DefineNamespaceStatement { - name: ns.to_owned().into(), - }, - ) - .await; - Ok(()) + let val = DefineNamespaceStatement { + name: ns.to_owned().into(), + }; + let _ = self.put(key, &val).await?; + Ok(val) } /// Add a database with a default configuration. - pub async fn add_db(&mut self, ns: &str, db: &str) -> Result<(), Error> { + pub async fn add_db(&mut self, ns: &str, db: &str) -> Result { let key = crate::key::db::new(ns, db); - let _ = self - .put( - key, - DefineDatabaseStatement { - name: db.to_owned().into(), - }, - ) - .await; - Ok(()) + let val = DefineDatabaseStatement { + name: db.to_owned().into(), + }; + let _ = self.put(key, &val).await?; + Ok(val) } /// Add a table with a default configuration. - pub async fn add_tb(&mut self, ns: &str, db: &str, tb: &str) -> Result<(), Error> { + pub async fn add_tb( + &mut self, + ns: &str, + db: &str, + tb: &str, + ) -> Result { let key = crate::key::tb::new(ns, db, tb); - let _ = self - .put( - key, - DefineTableStatement { - name: tb.to_owned().into(), - ..DefineTableStatement::default() - }, - ) - .await; - Ok(()) + let val = DefineTableStatement { + name: tb.to_owned().into(), + ..DefineTableStatement::default() + }; + let _ = self.put(key, &val).await?; + Ok(val) } /// Writes the full database contents as binary SQL. pub async fn export(&mut self, ns: &str, db: &str, chn: Sender>) -> Result<(), Error> {