Struct bonsaidb_local::Storage
source · [−]pub struct Storage { /* private fields */ }
Expand description
A file-based, multi-database, multi-user database engine.
Converting from Database::open
to Storage::open
Database::open
is a simple method that uses Storage
to
create a database named default
with the schema provided. These two ways
of opening the database are the same:
// `bonsaidb_core` is re-exported to `bonsaidb::core` or `bonsaidb_local::core`.
use bonsaidb_core::{connection::StorageConnection, schema::Schema};
// `bonsaidb_local` is re-exported to `bonsaidb::local` if using the omnibus crate.
use bonsaidb_local::{
config::{Builder, StorageConfiguration},
Database, Storage,
};
// This creates a Storage instance, creates a database, and returns it.
let db = Database::open::<MySchema>(StorageConfiguration::new("my-db.bonsaidb")).await?;
// This is the equivalent code being executed:
let storage =
Storage::open(StorageConfiguration::new("my-db.bonsaidb").with_schema::<MySchema>()?)
.await?;
storage.create_database::<MySchema>("default", true).await?;
let db = storage.database::<MySchema>("default").await?;
Using multiple databases
This example shows how to use Storage
to create and use multiple databases
with multiple schemas:
use bonsaidb_core::{
connection::StorageConnection,
schema::{Collection, Schema},
};
use bonsaidb_local::{
config::{Builder, StorageConfiguration},
Storage,
};
use serde::{Deserialize, Serialize};
#[derive(Debug, Schema)]
#[schema(name = "my-schema", collections = [BlogPost, Author])]
struct MySchema;
#[derive(Debug, Serialize, Deserialize, Collection)]
#[collection(name = "blog-posts")]
struct BlogPost {
pub title: String,
pub contents: String,
pub author_id: u64,
}
#[derive(Debug, Serialize, Deserialize, Collection)]
#[collection(name = "blog-posts")]
struct Author {
pub name: String,
}
let storage = Storage::open(
StorageConfiguration::new("my-db.bonsaidb")
.with_schema::<BlogPost>()?
.with_schema::<MySchema>()?,
)
.await?;
storage
.create_database::<BlogPost>("ectons-blog", true)
.await?;
let ectons_blog = storage.database::<BlogPost>("ectons-blog").await?;
storage
.create_database::<MySchema>("another-db", true)
.await?;
let another_db = storage.database::<MySchema>("another-db").await?;
Implementations
sourceimpl Storage
impl Storage
sourcepub async fn open(configuration: StorageConfiguration) -> Result<Self, Error>
pub async fn open(configuration: StorageConfiguration) -> Result<Self, Error>
Creates or opens a multi-database Storage
with its data stored in directory
.
sourcepub fn unique_id(&self) -> StorageId
pub fn unique_id(&self) -> StorageId
Returns the unique id of the server.
This value is set from the StorageConfiguration
or randomly
generated when creating a server. It shouldn’t be changed after a server
is in use, as doing can cause issues. For example, the vault that
manages encrypted storage uses the server ID to store the vault key. If
the server ID changes, the vault key storage will need to be updated
with the new server ID.
Trait Implementations
sourceimpl StorageConnection for Storage
impl StorageConnection for Storage
sourcefn create_database_with_schema<'life0, 'life1, 'async_trait>(
&'life0 self,
name: &'life1 str,
schema: SchemaName,
only_if_needed: bool
) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'async_trait>> where
'life0: 'async_trait,
'life1: 'async_trait,
Self: 'async_trait,
fn create_database_with_schema<'life0, 'life1, 'async_trait>(
&'life0 self,
name: &'life1 str,
schema: SchemaName,
only_if_needed: bool
) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'async_trait>> where
'life0: 'async_trait,
'life1: 'async_trait,
Self: 'async_trait,
Creates a database named name
using the SchemaName
schema
. Read more
sourcefn database<'life0, 'life1, 'async_trait, DB: Schema>(
&'life0 self,
name: &'life1 str
) -> Pin<Box<dyn Future<Output = Result<Self::Database, Error>> + Send + 'async_trait>> where
DB: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Self: 'async_trait,
fn database<'life0, 'life1, 'async_trait, DB: Schema>(
&'life0 self,
name: &'life1 str
) -> Pin<Box<dyn Future<Output = Result<Self::Database, Error>> + Send + 'async_trait>> where
DB: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Self: 'async_trait,
Returns a reference to database name
with schema DB
.
sourcefn delete_database<'life0, 'life1, 'async_trait>(
&'life0 self,
name: &'life1 str
) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'async_trait>> where
'life0: 'async_trait,
'life1: 'async_trait,
Self: 'async_trait,
fn delete_database<'life0, 'life1, 'async_trait>(
&'life0 self,
name: &'life1 str
) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'async_trait>> where
'life0: 'async_trait,
'life1: 'async_trait,
Self: 'async_trait,
Deletes a database named name
. Read more
sourcefn list_databases<'life0, 'async_trait>(
&'life0 self
) -> Pin<Box<dyn Future<Output = Result<Vec<Database>, Error>> + Send + 'async_trait>> where
'life0: 'async_trait,
Self: 'async_trait,
fn list_databases<'life0, 'async_trait>(
&'life0 self
) -> Pin<Box<dyn Future<Output = Result<Vec<Database>, Error>> + Send + 'async_trait>> where
'life0: 'async_trait,
Self: 'async_trait,
Lists the databases in this storage.
sourcefn list_available_schemas<'life0, 'async_trait>(
&'life0 self
) -> Pin<Box<dyn Future<Output = Result<Vec<SchemaName>, Error>> + Send + 'async_trait>> where
'life0: 'async_trait,
Self: 'async_trait,
fn list_available_schemas<'life0, 'async_trait>(
&'life0 self
) -> Pin<Box<dyn Future<Output = Result<Vec<SchemaName>, Error>> + Send + 'async_trait>> where
'life0: 'async_trait,
Self: 'async_trait,
Lists the SchemaName
s registered with this storage.
sourcefn create_user<'life0, 'life1, 'async_trait>(
&'life0 self,
username: &'life1 str
) -> Pin<Box<dyn Future<Output = Result<u64, Error>> + Send + 'async_trait>> where
'life0: 'async_trait,
'life1: 'async_trait,
Self: 'async_trait,
fn create_user<'life0, 'life1, 'async_trait>(
&'life0 self,
username: &'life1 str
) -> Pin<Box<dyn Future<Output = Result<u64, Error>> + Send + 'async_trait>> where
'life0: 'async_trait,
'life1: 'async_trait,
Self: 'async_trait,
Creates a user.
sourcefn delete_user<'user, 'life0, 'async_trait, U: Nameable<'user, u64> + Send + Sync>(
&'life0 self,
user: U
) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'async_trait>> where
'user: 'async_trait,
U: 'async_trait,
'life0: 'async_trait,
Self: 'async_trait,
fn delete_user<'user, 'life0, 'async_trait, U: Nameable<'user, u64> + Send + Sync>(
&'life0 self,
user: U
) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'async_trait>> where
'user: 'async_trait,
U: 'async_trait,
'life0: 'async_trait,
Self: 'async_trait,
Deletes a user.
sourcefn set_user_password<'user, 'life0, 'async_trait, U: Nameable<'user, u64> + Send + Sync>(
&'life0 self,
user: U,
password: SensitiveString
) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'async_trait>> where
'user: 'async_trait,
U: 'async_trait,
'life0: 'async_trait,
Self: 'async_trait,
fn set_user_password<'user, 'life0, 'async_trait, U: Nameable<'user, u64> + Send + Sync>(
&'life0 self,
user: U,
password: SensitiveString
) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'async_trait>> where
'user: 'async_trait,
U: 'async_trait,
'life0: 'async_trait,
Self: 'async_trait,
Sets a user’s password.
sourcefn authenticate<'user, 'life0, 'async_trait, U: Nameable<'user, u64> + Send + Sync>(
&'life0 self,
user: U,
authentication: Authentication
) -> Pin<Box<dyn Future<Output = Result<Authenticated, Error>> + Send + 'async_trait>> where
'user: 'async_trait,
U: 'async_trait,
'life0: 'async_trait,
Self: 'async_trait,
fn authenticate<'user, 'life0, 'async_trait, U: Nameable<'user, u64> + Send + Sync>(
&'life0 self,
user: U,
authentication: Authentication
) -> Pin<Box<dyn Future<Output = Result<Authenticated, Error>> + Send + 'async_trait>> where
'user: 'async_trait,
U: 'async_trait,
'life0: 'async_trait,
Self: 'async_trait,
Authenticates as a user with a authentication method.
sourcefn add_permission_group_to_user<'user, 'group, 'life0, 'async_trait, U: Nameable<'user, u64> + Send + Sync, G: Nameable<'group, u64> + Send + Sync>(
&'life0 self,
user: U,
permission_group: G
) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'async_trait>> where
'user: 'async_trait,
'group: 'async_trait,
U: 'async_trait,
G: 'async_trait,
'life0: 'async_trait,
Self: 'async_trait,
fn add_permission_group_to_user<'user, 'group, 'life0, 'async_trait, U: Nameable<'user, u64> + Send + Sync, G: Nameable<'group, u64> + Send + Sync>(
&'life0 self,
user: U,
permission_group: G
) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'async_trait>> where
'user: 'async_trait,
'group: 'async_trait,
U: 'async_trait,
G: 'async_trait,
'life0: 'async_trait,
Self: 'async_trait,
Adds a user to a permission group.
sourcefn remove_permission_group_from_user<'user, 'group, 'life0, 'async_trait, U: Nameable<'user, u64> + Send + Sync, G: Nameable<'group, u64> + Send + Sync>(
&'life0 self,
user: U,
permission_group: G
) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'async_trait>> where
'user: 'async_trait,
'group: 'async_trait,
U: 'async_trait,
G: 'async_trait,
'life0: 'async_trait,
Self: 'async_trait,
fn remove_permission_group_from_user<'user, 'group, 'life0, 'async_trait, U: Nameable<'user, u64> + Send + Sync, G: Nameable<'group, u64> + Send + Sync>(
&'life0 self,
user: U,
permission_group: G
) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'async_trait>> where
'user: 'async_trait,
'group: 'async_trait,
U: 'async_trait,
G: 'async_trait,
'life0: 'async_trait,
Self: 'async_trait,
Removes a user from a permission group.
sourcefn add_role_to_user<'user, 'group, 'life0, 'async_trait, U: Nameable<'user, u64> + Send + Sync, G: Nameable<'group, u64> + Send + Sync>(
&'life0 self,
user: U,
role: G
) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'async_trait>> where
'user: 'async_trait,
'group: 'async_trait,
U: 'async_trait,
G: 'async_trait,
'life0: 'async_trait,
Self: 'async_trait,
fn add_role_to_user<'user, 'group, 'life0, 'async_trait, U: Nameable<'user, u64> + Send + Sync, G: Nameable<'group, u64> + Send + Sync>(
&'life0 self,
user: U,
role: G
) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'async_trait>> where
'user: 'async_trait,
'group: 'async_trait,
U: 'async_trait,
G: 'async_trait,
'life0: 'async_trait,
Self: 'async_trait,
Adds a user to a permission group.
sourcefn remove_role_from_user<'user, 'group, 'life0, 'async_trait, U: Nameable<'user, u64> + Send + Sync, G: Nameable<'group, u64> + Send + Sync>(
&'life0 self,
user: U,
role: G
) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'async_trait>> where
'user: 'async_trait,
'group: 'async_trait,
U: 'async_trait,
G: 'async_trait,
'life0: 'async_trait,
Self: 'async_trait,
fn remove_role_from_user<'user, 'group, 'life0, 'async_trait, U: Nameable<'user, u64> + Send + Sync, G: Nameable<'group, u64> + Send + Sync>(
&'life0 self,
user: U,
role: G
) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'async_trait>> where
'user: 'async_trait,
'group: 'async_trait,
U: 'async_trait,
G: 'async_trait,
'life0: 'async_trait,
Self: 'async_trait,
Removes a user from a permission group.
sourcefn create_database<'life0, 'life1, 'async_trait, DB>(
&'life0 self,
name: &'life1 str,
only_if_needed: bool
) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'async_trait, Global>> where
'life0: 'async_trait,
'life1: 'async_trait,
DB: Schema + 'async_trait,
Self: 'async_trait,
fn create_database<'life0, 'life1, 'async_trait, DB>(
&'life0 self,
name: &'life1 str,
only_if_needed: bool
) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'async_trait, Global>> where
'life0: 'async_trait,
'life1: 'async_trait,
DB: Schema + 'async_trait,
Self: 'async_trait,
Creates a database named name
with the Schema
provided. Read more
Auto Trait Implementations
impl !RefUnwindSafe for Storage
impl Send for Storage
impl Sync for Storage
impl Unpin for Storage
impl !UnwindSafe for Storage
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
sourcefn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
impl<T> Pointable for T
impl<T> Pointable for T
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcefn clone_into(&self, target: &mut T)
fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
fn vzip(self) -> V
sourceimpl<T> WithSubscriber for T
impl<T> WithSubscriber for T
sourcefn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
sourcefn with_current_subscriber(self) -> WithDispatch<Self>
fn with_current_subscriber(self) -> WithDispatch<Self>
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more