Module dql

Module 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)",
         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").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", |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§

Diff
Represents a diff between two arrays.
DiffMove
Describes the move of an item in a Diff.
Differ
Calculates diffs between arrays of QueryResultItems.
Query
A DQL query string with its arguments
QueryResult
Represents results returned when executing a DQL query containing a QueryResultItem for each match.
QueryResultItem
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.

Traits§

IntoQuery
Types which can be used to construct a Query.