Crate dittolive_ditto
source ·Expand description
What is Ditto?
Ditto is a cross-platform, peer-to-peer database that allows apps to sync with and without internet connectivity.
Install Ditto into your application, then use the APIs to read and write data into its storage system, and it will then automatically sync any changes to other devices.
Unlike other synchronization solutions, Ditto is designed for “peer-to-peer” scenarios where it can directly communicate with other devices even without an Internet connection.
Additionally, Ditto automatically manages the complexity of using multiple network transports, like Bluetooth, P2P Wi-Fi, and Local Area Network, to find and connect to other devices and then synchronize any changes.
Ditto Platform Docs
Visit https://docs.ditto.live to learn about the full Ditto platform, including multi-language SDKs, the Ditto Cloud offering, and more.
Rust developers should be sure to check out these essential topics:
- Ditto Edge Sync Platform Basics
- Mesh Networking 101
- Data-Handling Essentials
- Ditto Query Language (DQL)
Playground Quickstart
Ditto offers a “playground” mode that lets you start playing and developing with Ditto without any authentication hassle.
use dittolive_ditto::prelude::*;
fn main() -> anyhow::Result<()> {
let app_id = AppId::from_env("DITTO_APP_ID")?;
let playground_token = std::env::var("DITTO_PLAYGROUND_TOKEN")?;
let cloud_sync = true;
let custom_auth_url = None;
// Initialize Ditto
let ditto = Ditto::builder()
.with_root(Arc::new(PersistentRoot::from_current_exe()?))
.with_identity(|ditto_root| {
identity::OnlinePlayground::new(
ditto_root,
app_id,
playground_token,
cloud_sync,
custom_auth_url,
)
})?
.build()?;
// Start syncing with peers
ditto.start_sync()?;
Ok(())
}
Write data using Ditto Query Language (DQL)
The preferred method to write data to Ditto is by using DQL.
To do this, we’ll first access the Ditto Store
, then
execute a DQL insert statement.
use dittolive_ditto::prelude::*;
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
struct Car {
color: String,
make: String,
}
async fn dql_insert_car(ditto: &Ditto, car: &Car) -> anyhow::Result<()> {
let store = ditto.store();
let query_result = store.execute(
// `cars` is the collection name
"INSERT INTO cars DOCUMENTS (:newCar)",
Some(serde_json::json!({
"newCar": car
}).into())
).await?;
// Optional: See the count of items inserted
let item_count = query_result.item_count();
// Optional: Inspect each item that was inserted
for query_item in query_result.iter() {
println!("Inserted: {}", query_item.json_string());
}
Ok(())
}
// To call:
let my_car = Car {
color: "blue".to_string(),
make: "ford".to_string(),
};
dql_insert_car(&ditto, &my_car).await?;
- See the DQL INSERT documentation for more info on DQL inserts
- See
QueryResult
andQueryResultItem
to learn about the returned values - See the Ditto Rust template repository for full examples
- Tip: Make sure you have
serde
added to yourCargo.toml
with thederive
feature - Tip: Make sure you have
serde_json
added to yourCargo.toml
Read data using DQL
use dittolive_ditto::prelude::*;
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
struct Car {
color: String,
make: String,
}
async fn dql_select_cars(ditto: &Ditto, color: &str) -> anyhow::Result<Vec<Car>> {
let store = ditto.store();
let query_result = store.execute(
"SELECT * FROM cars WHERE color = :myColor",
Some(serde_json::json!({
"myColor": color
}).into())
).await?;
let cars = query_result.iter()
.map(|query_item| query_item.deserialize_value::<Car>())
.collect::<Result<Vec<Car>, _>>()?;
Ok(cars)
}
// To call:
let cars: Vec<Car> = dql_select_cars(&ditto, "blue").await?;
- See the DQL SELECT documentation for more info on DQL selects
- See the Ditto Rust template repository for full examples
Modules
- Provides access to authentication information and methods for logging on to Ditto Cloud. Relevant when using an
OnlineWithAuthentication
identity. - Monitor disk usage on a device
- Entry point for the DittoSDK
- Ditto Error Types
- Ditto Root Directory Wrapper
- Ditto needs an
Identity
to start syncing with other peers. - Contains the observer pattern, used to subscribe to updates on
Peer
s,Collection
s, and the file system. - The prelude contains all common imports when using this crate.
Subscription
s are used to register interest in receiving updates for specifiedDittoDocument
s.- Manage the synchronization properties of your Ditto instance from here.
- Configure the transports for of your
Ditto
instance. - The Ditto custom types.