Struct bonsaidb_local::Database
source · pub struct Database { /* private fields */ }
Expand description
A database stored in BonsaiDb. This type blocks the current thread when
used. See AsyncDatabase
for this type’s async counterpart.
Converting between Blocking and Async Types
AsyncDatabase
and Database
can be converted to and from each other
using:
AsyncDatabase::into_blocking()
AsyncDatabase::to_blocking()
AsyncDatabase::as_blocking()
Database::into_async()
Database::to_async()
Database::into_async_with_runtime()
Database::to_async_with_runtime()
Using Database
to create a single database
Database
provides an easy mechanism to open and access a single database:
// `bonsaidb_core` is re-exported to `bonsaidb::core` or `bonsaidb_local::core`.
use bonsaidb_core::schema::Collection;
// `bonsaidb_local` is re-exported to `bonsaidb::local` if using the omnibus crate.
use bonsaidb_local::{
config::{Builder, StorageConfiguration},
Database,
};
use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize, Collection)]
#[collection(name = "blog-posts")]
struct BlogPost {
pub title: String,
pub contents: String,
}
let db = Database::open::<BlogPost>(StorageConfiguration::new("my-db.bonsaidb"))?;
Under the hood, this initializes a Storage
instance pointing at
“./my-db.bonsaidb”. It then returns (or creates) a database named “default”
with the schema BlogPost
.
In this example, BlogPost
implements the Collection
trait, and all
collections can be used as a [Schema
].
Implementations§
source§impl Database
impl Database
sourcepub fn with_effective_permissions(
&self,
effective_permissions: Permissions
) -> Option<Self>
pub fn with_effective_permissions( &self, effective_permissions: Permissions ) -> Option<Self>
Restricts an unauthenticated instance to having effective_permissions
.
Returns None
if a session has already been established.
sourcepub fn open<DB: Schema>(
configuration: StorageConfiguration
) -> Result<Self, Error>
pub fn open<DB: Schema>( configuration: StorageConfiguration ) -> Result<Self, Error>
Creates a Storage
with a single-database named “default” with its data
stored at path
. This requires exclusive access to the storage location
configured. Attempting to open the same path multiple times concurrently
will lead to errors.
Using this method is perfect if only one database is being used.
However, if multiple databases are needed, it is much better to store
multiple databases in a single Storage
instance rather than creating
multiple independent databases using this method.
When opening multiple databases using this function, each database will
have its own thread pool, cache, task worker pool, and more. By using a
single Storage
instance, BonsaiDb will use less resources and likely
perform better.
sourcepub fn schematic(&self) -> &Schematic
pub fn schematic(&self) -> &Schematic
Returns the [Schematic
] for the schema for this database.
sourcepub fn into_async(self) -> AsyncDatabase
pub fn into_async(self) -> AsyncDatabase
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.
sourcepub fn into_async_with_runtime(self, runtime: Handle) -> AsyncDatabase
pub fn into_async_with_runtime(self, runtime: Handle) -> AsyncDatabase
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.
sourcepub fn to_async(&self) -> AsyncDatabase
pub fn to_async(&self) -> AsyncDatabase
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.
sourcepub fn to_async_with_runtime(&self, runtime: Handle) -> AsyncDatabase
pub fn to_async_with_runtime(&self, runtime: Handle) -> AsyncDatabase
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§
source§impl Connection for Database
impl Connection for Database
source§fn storage(&self) -> Self::Storage
fn storage(&self) -> Self::Storage
StorageConnection
] implementor that this database belongs to.source§fn list_executed_transactions(
&self,
starting_id: Option<u64>,
result_limit: Option<u32>
) -> Result<Vec<Executed>, Error>
fn list_executed_transactions( &self, starting_id: Option<u64>, result_limit: Option<u32> ) -> Result<Vec<Executed>, Error>
Schema
. By default, a maximum of 1000 entries will
be returned, but that limit can be overridden by setting result_limit
.
A hard limit of 100,000 results will be returned. To begin listing after
another known transaction_id
, pass transaction_id + 1
into
starting_id
.source§fn last_transaction_id(&self) -> Result<Option<u64>, Error>
fn last_transaction_id(&self) -> Result<Option<u64>, Error>
source§fn compact(&self) -> Result<(), Error>
fn compact(&self) -> Result<(), Error>
source§fn compact_key_value_store(&self) -> Result<(), Error>
fn compact_key_value_store(&self) -> Result<(), Error>
§fn collection<C>(&self) -> Collection<'_, Self, C>where
C: Collection,
fn collection<C>(&self) -> Collection<'_, Self, C>where
C: Collection,
Schema
.§fn view<V>(&self) -> View<'_, Self, V, <V as View>::Key>where
V: SerializedView,
fn view<V>(&self) -> View<'_, Self, V, <V as View>::Key>where
V: SerializedView,
schema::View
] from this connection.§fn compact_collection<C>(&self) -> Result<(), Error>where
C: Collection,
fn compact_collection<C>(&self) -> Result<(), Error>where
C: Collection,
source§impl DatabaseNonBlocking for Database
impl DatabaseNonBlocking for Database
source§impl<'a> From<&'a AsyncDatabase> for Database
impl<'a> From<&'a AsyncDatabase> for Database
source§fn from(database: &'a AsyncDatabase) -> Self
fn from(database: &'a AsyncDatabase) -> Self
source§impl From<AsyncDatabase> for Database
impl From<AsyncDatabase> for Database
source§fn from(database: AsyncDatabase) -> Self
fn from(database: AsyncDatabase) -> Self
source§impl HasSession for Database
impl HasSession for Database
§fn allowed_to<'a, R, P>(&self, resource_name: R, action: &P) -> boolwhere
R: AsRef<[Identifier<'a>]>,
P: Action,
fn allowed_to<'a, R, P>(&self, resource_name: R, action: &P) -> boolwhere
R: AsRef<[Identifier<'a>]>,
P: Action,
action
is permitted against resource_name
.§fn check_permission<'a, R, P>(
&self,
resource_name: R,
action: &P
) -> Result<(), Error>where
R: AsRef<[Identifier<'a>]>,
P: Action,
fn check_permission<'a, R, P>(
&self,
resource_name: R,
action: &P
) -> Result<(), Error>where
R: AsRef<[Identifier<'a>]>,
P: Action,
action
is permitted against resource_name
. If permission
is denied, returns a PermissionDenied
error.source§impl KeyValue for Database
impl KeyValue for Database
source§fn execute_key_operation(&self, op: KeyOperation) -> Result<Output, Error>
fn execute_key_operation(&self, op: KeyOperation) -> Result<Output, Error>
KeyOperation
].§fn set_key<S, V, 'a>(&'a self, key: S, value: &'a V) -> Builder<'a, Self, V>
fn set_key<S, V, 'a>(&'a self, key: S, value: &'a V) -> Builder<'a, Self, V>
key
to value
. This function returns a builder that is also a
Future. Awaiting the builder will execute [Command::Set
] with the options
given.§fn set_binary_key<S, 'a>(
&'a self,
key: S,
bytes: &'a [u8]
) -> Builder<'a, Self, ()>
fn set_binary_key<S, 'a>( &'a self, key: S, bytes: &'a [u8] ) -> Builder<'a, Self, ()>
key
to bytes
. This function returns a builder that is also
a Future. Awaiting the builder will execute [Command::Set
] with
the options given.§fn set_numeric_key<S, V>(&self, key: S, value: V) -> Builder<'_, Self, ()>
fn set_numeric_key<S, V>(&self, key: S, value: V) -> Builder<'_, Self, ()>
key
to value
. This stores the value as a Numeric
,
enabling atomic math operations to be performed on this key. This
function returns a builder that is also a Future. Awaiting the
builder will execute [Command::Set
] with the options given.§fn increment_key_by<S, V>(&self, key: S, value: V) -> Builder<'_, Self, V>
fn increment_key_by<S, V>(&self, key: S, value: V) -> Builder<'_, Self, V>
key
by value
. The value stored must be a Numeric
,
otherwise an error will be returned. The result of the increment
will be the value
’s type. For example, if the stored value is
currently a u64
, but value
is a f64
, the current value will be
converted to an f64
, and the stored value will be an f64
.§fn decrement_key_by<S, V>(&self, key: S, value: V) -> Builder<'_, Self, V>
fn decrement_key_by<S, V>(&self, key: S, value: V) -> Builder<'_, Self, V>
key
by value
. The value stored must be a Numeric
,
otherwise an error will be returned. The result of the decrement
will be the value
’s type. For example, if the stored value is
currently a u64
, but value
is a f64
, the current value will be
converted to an f64
, and the stored value will be an f64
.§fn get_key<S>(&self, key: S) -> Builder<'_, Self>
fn get_key<S>(&self, key: S) -> Builder<'_, Self>
key
. This function returns a builder that is also a
Future. Awaiting the builder will execute [Command::Get
] with the options
given.§fn delete_key<S>(&self, key: S) -> Result<KeyStatus, Error>
fn delete_key<S>(&self, key: S) -> Result<KeyStatus, Error>
key
.§fn key_namespace(&self) -> Option<&str>
fn key_namespace(&self) -> Option<&str>
§fn with_key_namespace(&self, namespace: &str) -> Namespaced<'_, Self>
fn with_key_namespace(&self, namespace: &str) -> Namespaced<'_, Self>
Namespaced
] instance, all keys specified will be separated into their
own storage designated by namespace
.source§impl LowLevelConnection for Database
impl LowLevelConnection for Database
source§fn apply_transaction(
&self,
transaction: Transaction
) -> Result<Vec<OperationResult>, Error>
fn apply_transaction( &self, transaction: Transaction ) -> Result<Vec<OperationResult>, Error>
Transaction
] to the [schema::Schema
]. If any operation in the
[Transaction
] fails, none of the operations will be applied to the
[schema::Schema
].source§fn get_from_collection(
&self,
id: DocumentId,
collection: &CollectionName
) -> Result<Option<OwnedDocument>, Error>
fn get_from_collection( &self, id: DocumentId, collection: &CollectionName ) -> Result<Option<OwnedDocument>, Error>
source§fn list_from_collection(
&self,
ids: Range<DocumentId>,
sort: Sort,
limit: Option<u32>,
collection: &CollectionName
) -> Result<Vec<OwnedDocument>, Error>
fn list_from_collection( &self, ids: Range<DocumentId>, sort: Sort, limit: Option<u32>, collection: &CollectionName ) -> Result<Vec<OwnedDocument>, Error>
ids
from the named
collection
. To retrieve all documents, pass in ..
for ids
. Read moresource§fn list_headers_from_collection(
&self,
ids: Range<DocumentId>,
sort: Sort,
limit: Option<u32>,
collection: &CollectionName
) -> Result<Vec<Header>, Error>
fn list_headers_from_collection( &self, ids: Range<DocumentId>, sort: Sort, limit: Option<u32>, collection: &CollectionName ) -> Result<Vec<Header>, Error>
ids
from the named
collection
. To retrieve all documents, pass in ..
for ids
. Read moresource§fn count_from_collection(
&self,
ids: Range<DocumentId>,
collection: &CollectionName
) -> Result<u64, Error>
fn count_from_collection( &self, ids: Range<DocumentId>, collection: &CollectionName ) -> Result<u64, Error>
source§fn get_multiple_from_collection(
&self,
ids: &[DocumentId],
collection: &CollectionName
) -> Result<Vec<OwnedDocument>, Error>
fn get_multiple_from_collection( &self, ids: &[DocumentId], collection: &CollectionName ) -> Result<Vec<OwnedDocument>, Error>
ids
from the named collection
.
Documents that are not found are not returned, but no error will be
generated. Read moresource§fn compact_collection_by_name(
&self,
collection: CollectionName
) -> Result<(), Error>
fn compact_collection_by_name( &self, collection: CollectionName ) -> Result<(), Error>
source§fn query_by_name(
&self,
view: &ViewName,
key: Option<SerializedQueryKey>,
order: Sort,
limit: Option<u32>,
access_policy: AccessPolicy
) -> Result<Vec<Serialized>, Error>
fn query_by_name( &self, view: &ViewName, key: Option<SerializedQueryKey>, order: Sort, limit: Option<u32>, access_policy: AccessPolicy ) -> Result<Vec<Serialized>, Error>
view
. Read moresource§fn query_by_name_with_docs(
&self,
view: &ViewName,
key: Option<SerializedQueryKey>,
order: Sort,
limit: Option<u32>,
access_policy: AccessPolicy
) -> Result<MappedSerializedDocuments, Error>
fn query_by_name_with_docs( &self, view: &ViewName, key: Option<SerializedQueryKey>, order: Sort, limit: Option<u32>, access_policy: AccessPolicy ) -> Result<MappedSerializedDocuments, Error>
view
with their source
documents. Read moresource§fn reduce_by_name(
&self,
view_name: &ViewName,
key: Option<SerializedQueryKey>,
access_policy: AccessPolicy
) -> Result<Vec<u8>, Error>
fn reduce_by_name( &self, view_name: &ViewName, key: Option<SerializedQueryKey>, access_policy: AccessPolicy ) -> Result<Vec<u8>, Error>
view
. Read moresource§fn reduce_grouped_by_name(
&self,
view_name: &ViewName,
key: Option<SerializedQueryKey>,
access_policy: AccessPolicy
) -> Result<Vec<MappedSerializedValue>, Error>
fn reduce_grouped_by_name( &self, view_name: &ViewName, key: Option<SerializedQueryKey>, access_policy: AccessPolicy ) -> Result<Vec<MappedSerializedValue>, Error>
view
, reducing the values by each
unique key. Read moresource§fn delete_docs_by_name(
&self,
view: &ViewName,
key: Option<SerializedQueryKey>,
access_policy: AccessPolicy
) -> Result<u64, Error>
fn delete_docs_by_name( &self, view: &ViewName, key: Option<SerializedQueryKey>, access_policy: AccessPolicy ) -> Result<u64, Error>
view
. Read more§fn insert<C, PrimaryKey, B>(
&self,
id: Option<&PrimaryKey>,
contents: B
) -> Result<CollectionHeader<<C as Collection>::PrimaryKey>, Error>
fn insert<C, PrimaryKey, B>( &self, id: Option<&PrimaryKey>, contents: B ) -> Result<CollectionHeader<<C as Collection>::PrimaryKey>, Error>
schema::Schema
]
for the Collection
C
. If id
is None
a unique id will be
generated. If an id is provided and a document already exists with that
id, a conflict error will be returned. Read more§fn update<C, D>(&self, doc: &mut D) -> Result<(), Error>
fn update<C, D>(&self, doc: &mut D) -> Result<(), Error>
schema::Schema
] for the
Collection
C
. Upon success, doc.revision
will be updated with
the new revision. Read more§fn overwrite<C, PrimaryKey>(
&self,
id: &PrimaryKey,
contents: Vec<u8>
) -> Result<CollectionHeader<<C as Collection>::PrimaryKey>, Error>where
C: Collection,
PrimaryKey: KeyEncoding<<C as Collection>::PrimaryKey>,
fn overwrite<C, PrimaryKey>(
&self,
id: &PrimaryKey,
contents: Vec<u8>
) -> Result<CollectionHeader<<C as Collection>::PrimaryKey>, Error>where
C: Collection,
PrimaryKey: KeyEncoding<<C as Collection>::PrimaryKey>,
doc.revision
will be updated with the new revision information. Read more§fn get<C, PrimaryKey>(
&self,
id: &PrimaryKey
) -> Result<Option<OwnedDocument>, Error>where
C: Collection,
PrimaryKey: KeyEncoding<<C as Collection>::PrimaryKey> + ?Sized,
fn get<C, PrimaryKey>(
&self,
id: &PrimaryKey
) -> Result<Option<OwnedDocument>, Error>where
C: Collection,
PrimaryKey: KeyEncoding<<C as Collection>::PrimaryKey> + ?Sized,
§fn get_multiple<'id, C, PrimaryKey, DocumentIds, I>(
&self,
ids: DocumentIds
) -> Result<Vec<OwnedDocument>, Error>where
C: Collection,
DocumentIds: IntoIterator<Item = &'id PrimaryKey, IntoIter = I> + Send + Sync,
I: Iterator<Item = &'id PrimaryKey> + Send + Sync,
PrimaryKey: KeyEncoding<<C as Collection>::PrimaryKey> + 'id + ?Sized,
fn get_multiple<'id, C, PrimaryKey, DocumentIds, I>(
&self,
ids: DocumentIds
) -> Result<Vec<OwnedDocument>, Error>where
C: Collection,
DocumentIds: IntoIterator<Item = &'id PrimaryKey, IntoIter = I> + Send + Sync,
I: Iterator<Item = &'id PrimaryKey> + Send + Sync,
PrimaryKey: KeyEncoding<<C as Collection>::PrimaryKey> + 'id + ?Sized,
ids
. Documents that are not found are
not returned, but no error will be generated. Read more§fn list<'id, C, R, PrimaryKey>(
&self,
ids: R,
order: Sort,
limit: Option<u32>
) -> Result<Vec<OwnedDocument>, Error>
fn list<'id, C, R, PrimaryKey>( &self, ids: R, order: Sort, limit: Option<u32> ) -> Result<Vec<OwnedDocument>, Error>
ids
. To retrieve all
documents, pass in ..
for ids
. Read more§fn list_headers<'id, C, R, PrimaryKey>(
&self,
ids: R,
order: Sort,
limit: Option<u32>
) -> Result<Vec<Header>, Error>
fn list_headers<'id, C, R, PrimaryKey>( &self, ids: R, order: Sort, limit: Option<u32> ) -> Result<Vec<Header>, Error>
ids
. To retrieve all
documents, pass in ..
for ids
. Read more§fn count<'id, C, R, PrimaryKey>(&self, ids: R) -> Result<u64, Error>
fn count<'id, C, R, PrimaryKey>(&self, ids: R) -> Result<u64, Error>
ids
. Read more§fn delete<C, H>(&self, doc: &H) -> Result<(), Error>
fn delete<C, H>(&self, doc: &H) -> Result<(), Error>
Document
from the database. Read more§fn query<V, Key>(
&self,
key: Option<QueryKey<'_, <V as View>::Key, Key>>,
order: Sort,
limit: Option<u32>,
access_policy: AccessPolicy
) -> Result<Vec<CollectionMap<<<V as View>::Collection as Collection>::PrimaryKey, <V as View>::Key, <V as View>::Value>>, Error>
fn query<V, Key>( &self, key: Option<QueryKey<'_, <V as View>::Key, Key>>, order: Sort, limit: Option<u32>, access_policy: AccessPolicy ) -> Result<Vec<CollectionMap<<<V as View>::Collection as Collection>::PrimaryKey, <V as View>::Key, <V as View>::Value>>, Error>
§fn query_with_docs<V, Key>(
&self,
key: Option<QueryKey<'_, <V as View>::Key, Key>>,
order: Sort,
limit: Option<u32>,
access_policy: AccessPolicy
) -> Result<MappedDocuments<OwnedDocument, V>, Error>
fn query_with_docs<V, Key>( &self, key: Option<QueryKey<'_, <V as View>::Key, Key>>, order: Sort, limit: Option<u32>, access_policy: AccessPolicy ) -> Result<MappedDocuments<OwnedDocument, V>, Error>
§fn query_with_collection_docs<V, Key>(
&self,
key: Option<QueryKey<'_, <V as View>::Key, Key>>,
order: Sort,
limit: Option<u32>,
access_policy: AccessPolicy
) -> Result<MappedDocuments<CollectionDocument<<V as View>::Collection>, V>, Error>
fn query_with_collection_docs<V, Key>( &self, key: Option<QueryKey<'_, <V as View>::Key, Key>>, order: Sort, limit: Option<u32>, access_policy: AccessPolicy ) -> Result<MappedDocuments<CollectionDocument<<V as View>::Collection>, V>, Error>
§fn reduce<V, Key>(
&self,
key: Option<QueryKey<'_, <V as View>::Key, Key>>,
access_policy: AccessPolicy
) -> Result<<V as View>::Value, Error>
fn reduce<V, Key>( &self, key: Option<QueryKey<'_, <V as View>::Key, Key>>, access_policy: AccessPolicy ) -> Result<<V as View>::Value, Error>
§fn reduce_grouped<V, Key>(
&self,
key: Option<QueryKey<'_, <V as View>::Key, Key>>,
access_policy: AccessPolicy
) -> Result<Vec<MappedValue<<V as View>::Key, <V as View>::Value>>, Error>
fn reduce_grouped<V, Key>( &self, key: Option<QueryKey<'_, <V as View>::Key, Key>>, access_policy: AccessPolicy ) -> Result<Vec<MappedValue<<V as View>::Key, <V as View>::Value>>, Error>
source§impl PubSub for Database
impl PubSub for Database
§type Subscriber = Subscriber
type Subscriber = Subscriber
PubSub
connection.source§fn create_subscriber(&self) -> Result<Self::Subscriber, Error>
fn create_subscriber(&self) -> Result<Self::Subscriber, Error>
Subscriber
] for this relay.source§fn publish_bytes(&self, topic: Vec<u8>, payload: Vec<u8>) -> Result<(), Error>
fn publish_bytes(&self, topic: Vec<u8>, payload: Vec<u8>) -> Result<(), Error>
payload
to all subscribers of topic
.source§fn publish_bytes_to_all(
&self,
topics: impl IntoIterator<Item = Vec<u8>> + Send,
payload: Vec<u8>
) -> Result<(), Error>
fn publish_bytes_to_all( &self, topics: impl IntoIterator<Item = Vec<u8>> + Send, payload: Vec<u8> ) -> Result<(), Error>
payload
to all subscribers of all topics
.§fn publish<Topic, Payload>(
&self,
topic: &Topic,
payload: &Payload
) -> Result<(), Error>
fn publish<Topic, Payload>( &self, topic: &Topic, payload: &Payload ) -> Result<(), Error>
payload
to all subscribers of topic
.§fn publish_to_all<'topics, Topics, Topic, Payload>(
&self,
topics: Topics,
payload: &Payload
) -> Result<(), Error>where
Topics: IntoIterator<Item = &'topics Topic> + 'topics,
Topic: Serialize + 'topics,
Payload: Serialize,
fn publish_to_all<'topics, Topics, Topic, Payload>(
&self,
topics: Topics,
payload: &Payload
) -> Result<(), Error>where
Topics: IntoIterator<Item = &'topics Topic> + 'topics,
Topic: Serialize + 'topics,
Payload: Serialize,
payload
to all subscribers of all topics
.