Improve documentation of Rust crate

This commit is contained in:
Tobie Morgan Hitchcock 2022-08-28 13:18:12 +01:00
parent 3043538fdf
commit c0217078f5
2 changed files with 71 additions and 7 deletions

View file

@ -133,6 +133,21 @@ impl Datastore {
} }
/// Create a new transaction on this datastore /// Create a new transaction on this datastore
///
/// *You must ensure that a [`Transaction`] does not ever outlive a [`Datastore`] instance.*
///
/// ```rust,no_run
/// use surrealdb::Datastore;
/// use surrealdb::Error;
///
/// #[tokio::main]
/// async fn main() -> Result<(), Error> {
/// let ds = Datastore::new("file://database.db").await?;
/// let mut tx = ds.transaction(true, false).await?;
/// tx.cancel().await?;
/// Ok(())
/// }
/// ```
pub async fn transaction(&self, write: bool, lock: bool) -> Result<Transaction, Error> { pub async fn transaction(&self, write: bool, lock: bool) -> Result<Transaction, Error> {
match &self.inner { match &self.inner {
#[cfg(feature = "kv-echodb")] #[cfg(feature = "kv-echodb")]
@ -179,6 +194,21 @@ impl Datastore {
} }
/// Parse and execute an SQL query /// Parse and execute an SQL query
///
/// ```rust,no_run
/// use surrealdb::Datastore;
/// use surrealdb::Error;
/// use surrealdb::Session;
///
/// #[tokio::main]
/// async fn main() -> Result<(), Error> {
/// let ds = Datastore::new("memory").await?;
/// let ses = Session::for_kv();
/// let ast = "USE NS test DB test; SELECT * FROM person;";
/// let res = ds.execute(ast, &ses, None, false).await?;
/// Ok(())
/// }
/// ```
pub async fn execute( pub async fn execute(
&self, &self,
txt: &str, txt: &str,
@ -212,6 +242,22 @@ impl Datastore {
} }
/// Execute a pre-parsed SQL query /// Execute a pre-parsed SQL query
///
/// ```rust,no_run
/// use surrealdb::Datastore;
/// use surrealdb::Error;
/// use surrealdb::Session;
/// use surrealdb::sql::parse;
///
/// #[tokio::main]
/// async fn main() -> Result<(), Error> {
/// let ds = Datastore::new("memory").await?;
/// let ses = Session::for_kv();
/// let ast = parse("USE NS test DB test; SELECT * FROM person;")?;
/// let res = ds.process(ast, &ses, None, false).await?;
/// Ok(())
/// }
/// ```
pub async fn process( pub async fn process(
&self, &self,
ast: Query, ast: Query,
@ -242,7 +288,24 @@ impl Datastore {
exe.execute(ctx, opt, ast).await exe.execute(ctx, opt, ast).await
} }
/// Execute a pre-parsed SQL query /// Ensure a SQL [`Value`] is fully computed
///
/// ```rust,no_run
/// use surrealdb::Datastore;
/// use surrealdb::Error;
/// use surrealdb::Session;
/// use surrealdb::sql::Function;
/// use surrealdb::sql::Value;
///
/// #[tokio::main]
/// async fn main() -> Result<(), Error> {
/// let ds = Datastore::new("memory").await?;
/// let ses = Session::for_kv();
/// let val = Value::Function(Box::new(Function::Future(Value::True)));
/// let res = ds.compute(val, &ses, None, false).await?;
/// Ok(())
/// }
/// ```
pub async fn compute( pub async fn compute(
&self, &self,
val: Value, val: Value,

View file

@ -1,10 +1,11 @@
//! This library provides an easy-to-use client for [SurrealDB](https://surrealdb.com), //! This library provides the low-level database library implementation, and query language
//! the ultimate cloud database for tomorrow's applications. SurrealDB is a scalable, //! definition, for [SurrealDB](https://surrealdb.com), the ultimate cloud database for
//! distributed, collaborative, document-graph database for the realtime web. //! tomorrow's applications. SurrealDB is a scalable, distributed, collaborative, document-graph
//! database for the realtime web.
//! //!
//! This library can be used to start an embedded in-memory datastore, an embedded //! This library can be used to start an embedded in-memory datastore, an embedded datastore
//! datastore persisted to disk, or for connecting to a distributed [TiKV](https://tikv.org) //! persisted to disk, a browser-based embedded datastore backed by IndexedDB, or for connecting
//! key-value store. //! to a distributed [TiKV](https://tikv.org) key-value store.
#[macro_use] #[macro_use]
extern crate log; extern crate log;