Module dittolive_ditto::dql

source ·
Expand description

Use DQL Querys 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(
         "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();

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

Structs§

  • DQL query statement
  • Arguments that can be used with an associated Query. Currently QueryArguments can only be created from types that implement serde::Serialize.
  • 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.