1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
//! Use DQL [`Query`]s to [subscribe to], [observe], and [edit] documents.
//!
//! # Example: Write and Read with DQL
//!
//! Let's look at how to execute an immediate query on the data in the `Store`
//! using [`ditto.store().execute(...)`]:
//!
//! ```
//! use dittolive_ditto::prelude::*;
//! use dittolive_ditto::dql::QueryResult;
//! # async fn example(ditto: &Ditto) -> anyhow::Result<()> {
//!
//! // Insert a document into a collection
//! let insert_result: QueryResult = ditto
//! .store()
//! .execute(
//! "INSERT INTO cars DOCUMENTS (:newCar)",
//! Some(serde_json::json!({
//! "newCar": {
//! "make": "ford",
//! "color": "blue"
//! }
//! }).into())
//! )
//! .await?;
//!
//! // Select all documents from a collection
//! let select_result: QueryResult = ditto.store().execute("SELECT * FROM cars", None).await?;
//!
//! // Extract documents as `serde_json::Value`s
//! let documents: Vec<serde_json::Value> = select_result
//! .iter()
//! .flat_map(|item| item.deserialize_value().ok())
//! .collect();
//!
//! # Ok(())
//! # }
//! ```
//!
//! # Example: Observe document updates with DQL
//!
//! If instead we want to be notified when documents are changed, we can use
//! [`ditto.store().register_observer(...)`] to receive callbacks for documents
//! matching a given DQL query.
//!
//! ```
//! use dittolive_ditto::{
//! dql::{QueryResult, QueryResultItem},
//! prelude::*,
//! };
//! # fn example(ditto: &Ditto) -> anyhow::Result<()> {
//!
//! let _observer = ditto.store().register_observer(
//! "SELECT * FROM cars",
//! None, // No query arguments
//! |query_result: QueryResult| {
//! let changed_documents = query_result
//! .iter()
//! .flat_map(|item: QueryResultItem| {
//! item.deserialize_value::<serde_json::Value>().ok()
//! })
//! .collect::<Vec<_>>();
//!
//! for doc in &changed_documents {
//! println!("Observed change to document: {doc}");
//! }
//! },
//! )?;
//! # Ok(())
//! # }
//! ```
//!
//! [subscribe to]: crate::sync::Sync::register_subscription
//! [observe]: crate::store::Store::register_observer
//! [edit]: crate::store::Store::execute
//! [`ditto.store().execute(...)`]: crate::store::Store::execute
//! [`ditto.store().register_observer(...)`]: crate::store::Store::register_observer
/// Query
pub(crate) mod query;
pub use query::{Query, QueryArguments};
/// Dql Result
pub(crate) mod query_result;
pub use query_result::{QueryResult, QueryResultItem};
// TODO(v5): Remove re-exports from deprecated location
#[doc(hidden)]
#[deprecated(note = "Use `dittolive_ditto::store::ChangeHandler` instead")]
pub use crate::store::ChangeHandler;
#[doc(hidden)]
#[deprecated(note = "Use `dittolive_ditto::store::ChangeHandlerWithSignalNext` instead")]
pub use crate::store::ChangeHandlerWithSignalNext;
#[doc(hidden)]
#[deprecated(note = "Use `dittolive_ditto::store::SignalNext` instead")]
pub use crate::store::SignalNext;
#[doc(hidden)]
#[deprecated(note = "Use `dittolive_ditto::store::StoreObserver` instead")]
pub use crate::store::StoreObserver;
// NOTE(nimo): Ensure that even though this is deprecated, it's available
/// ```
/// use dittolive_ditto::{dql::SyncSubscription as _, store::dql::SyncSubscription as _};
/// ```
#[doc(hidden)]
#[deprecated(note = "Use `sync::SyncSubscription` instead")]
pub use crate::sync::SyncSubscription;