Integrating PliantDb Locally

PliantDb 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 pliantdb/examples/basic-local.rs)

let db = Database::<Message>::open_local(
    "basic.pliantdb", 
    &Configuration::default()
).await?;

Under the hood, PliantDb 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 pliantdb/examples/basic-local.rs)

let storage = Storage::open_local(
    "basic.pliantdb",
    &Configuration::default()
).await?;
storage.register_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 PliantDb, you can use these common traits to make your methods accept any style of PliantDb access.

For example, pliantdb/examples/basic-local.rs uses this helper method to insert a record:

async fn insert_a_message<C: Connection>(
    connection: &C,
    value: &str,
) -> anyhow::Result<()> {
    connection
        .collection::<Message>()
        .push(&Message {
            contents: String::from(value),
            timestamp: SystemTime::now(),
        })
        .await?;
    Ok(())
}