pub struct Storage { /* private fields */ }
Expand description

A file-based, multi-database, multi-user database engine. This type blocks the current thread when used. See AsyncStorage for this type’s async counterpart.

Converting between Blocking and Async Types

AsyncStorage and Storage can be converted to and from each other using:

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"))?;

// This is the equivalent code being executed:
let storage =
    Storage::open(StorageConfiguration::new("my-db.bonsaidb").with_schema::<MySchema>()?)?;
storage.create_database::<MySchema>("default", true)?;
let db = storage.database::<MySchema>("default")?;

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>()?,
)?;

storage.create_database::<BlogPost>("ectons-blog", true)?;
let ectons_blog = storage.database::<BlogPost>("ectons-blog")?;
storage.create_database::<MySchema>("another-db", true)?;
let another_db = storage.database::<MySchema>("another-db")?;

Implementations

Stores a copy of all data in this instance to location.

Restores all data from a previously stored backup location.

Creates or opens a multi-database Storage with its data stored in directory.

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.

Registers a schema for use within the server.

Restricts an unauthenticated instance to having effective_permissions. Returns None if a session has already been established.

Converts this instance into its blocking version, which is able to be used without async. The returned instance uses the current Tokio runtime handle to spawn blocking tasks.

Panics

Panics if called outside the context of a Tokio runtime.

Converts this instance into its blocking version, which is able to be used without async. The returned instance uses the provided runtime handle to spawn blocking tasks.

Converts this instance into its blocking version, which is able to be used without async. The returned instance uses the current Tokio runtime handle to spawn blocking tasks.

Panics

Panics if called outside the context of a Tokio runtime.

Converts this instance into its blocking version, which is able to be used without async. The returned instance uses the provided runtime handle to spawn blocking tasks.

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Converts to this type from the input type.

Converts to this type from the input type.

Returns the currently authenticated session, if any.

Checks if action is permitted against resource_name.

Checks if action is permitted against resource_name. If permission is denied, returns a PermissionDenied error. Read more

The type that represents a database for this implementation.

The StorageConnection type returned from authentication calls.

Returns the administration database.

Creates a database named name using the SchemaName schema. Read more

Returns a reference to database name with schema DB.

Deletes a database named name. Read more

Lists the databases in this storage.

Lists the SchemaNames registered with this storage.

Creates a user.

Deletes a user.

Sets a user’s password.

Authenticates using the active session, returning a connection with a new session upon success. The existing connection will remain usable with the existing authentication, if any. Read more

Assumes the identity. If successful, the returned instance will have the permissions from identity. Read more

Adds a user to a permission group.

Removes a user from a permission group.

Adds a user to a permission group.

Removes a user from a permission group.

Creates a database named name with the Schema provided. Read more

Authenticates using an AuthenticationToken. If successful, the returned instance will have the permissions from identity. Read more

Authenticates a User using a password. If successful, the returned instance will have the permissions from identity. Read more

Returns the path of the database storage.

Returns a new instance of Storage with session as the effective authentication session. This call will only succeed if there is no current session. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The alignment of pointer.

The type for initializers.

Initializes a with the given initializer. Read more

Dereferences the given pointer. Read more

Mutably dereferences the given pointer. Read more

Drops the object pointed to by the given pointer. Read more

Should always be Self

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more