dittolive_ditto

Module dql

Source
Expand description

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;

// Insert a document into a collection
let insert_result: QueryResult = ditto
    .store()
    .execute_v2((
         "INSERT INTO cars DOCUMENTS (:newCar)",
         serde_json::json!({
             "newCar": {
                 "make": "ford",
                 "color": "blue"
             }
         })
    ))
    .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();

§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::*,
};

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}");
        }
    },
)?;

Re-exports§

  • pub use query::Query;
    Deprecated
  • pub use query::QueryArguments;
    Deprecated

Structs§

  • Represents a diff between two arrays.
  • Describes the move of an item in a Diff.
  • Calculates diffs between arrays of QueryResultItems.
  • Represents results returned when executing a DQL query containing a QueryResultItem for each match.
  • Represents a single match of a DQL query, similar to a “row” in SQL terms. It’s a reference type serving as a “cursor”, allowing for efficient access of the underlying data in various formats.
  • A DQL query string with its arguments

Traits§