Integrating BonsaiDb Locally
BonsaiDb supports multiple databases and multiple schemas. However, for many applications, you only need a single database.
If you're only wanting a single database, the setup is straightforward: (from examples/basic-local/examples/basic-local.rs)
let db = Database::<Message>::open(
    StorageConfiguration::new("basic.bonsaidb")
).await?;
Under the hood, BonsaiDb is creating a multi-database Storage with a local Database named default for you. If you need to switch to a multi-database model, you can open the storage and access the default database: (adapted from examples/basic-local/examples/basic-local.rs)
let storage = Storage::open(
    Configuration::new("basic.bonsaidb")
        .with_schema::<Message>()?
).await?;
let db = storage.database::<Message>("default").await?;
You can register multiple schemas so that databases can be purpose-built.
Common Traits
To help your code transition between different modes of accessing BonsaiDb, you can use these common traits to make your methods accept any style of BonsaiDb access.
- Databaseimplements- Connection,- KeyValue, and- PubSub.
- Storageimplements- StorageConnection.
For example, examples/basic-local/examples/basic-local.rs uses this helper method to insert a record:
async fn insert_a_message<C: Connection>(
    connection: &C,
    value: &str,
) -> Result<(), bonsaidb::core::Error> {
    Message {
        contents: String::from(value),
        timestamp: SystemTime::now(),
    }
    .push_into(connection)
    .await?;
    Ok(())
}