Ditto Query Language (DQL) — the dedicated query language you'll use to perform various filter operations to carry out traditional CREATE, READ, UPDATE, and DELETE (CRUD) database operations — is a familiar SQL‑like syntax designed specifically for Ditto's edge sync features that enable the platform's offline-first capabilities.
To execute CRUD operations, call the Ditto SDK's execute API method as follows:
The following table provides a high-level overview of the different ways you can perform CRUD in Ditto:
Using the INSERT statement, either insert a new document or update an existing document for a given document ID. (See Creating)
Using the SELECT statement, retrieve documents based on the specific criteria you pass as parameters in your function. (See Reading)
Using the UPDATE method, write changes to Ditto. (See Updating)
Using the EVICT method, remove local data from your store. (See Deleting)
In addition, using a soft-delete pattern, indicate data as deleted without physically removing it from Ditto.
To create a new document in your local Ditto store, call INSERT. Following is an example of how to perform an INSERT operation using Ditto's SDK:
To retrieve data in Ditto, depending on your goals and use case, use any of the following query types:
With the execute API method and SELECT query, search for documents within your local Ditto store:
When you need to actively monitor changes within your local Ditto store and respond to them immediately; for example, watching updates to end-user profiles, use a store observer.
A store observer is a DQL query that runs continuously and, once Ditto detects relevant changes, asynchronously triggers the callback function you defined when you set up your store observer. For instance, when the end user updates their profile, display the profile changes to the end user in realtime.
Following is a snippet demonstrating how to establish a store observer in Ditto:
With the UPDATE statement, you can update fields within one or more documents in your local Ditto store.
For example, executing an UPDATE operation within the cars collection, changing the color to 'blue' and the mileage to 3001 in documents where the _id field is '123':
Data storage management is essential for preventing unnecessary resource usage, which affects not only performance but also battery life and overall end-user experience.
Call the Evict method to clear one or more documents from the local Ditto store. Once invoked, the documents are no longer accessible by local queries; however, they remain accessible from other peers connected in the mesh.
EVICT can be used with soft-delete patterns to safely coordinate the deletion of data across peers.
The following snippet shows how to write a basic EVICT operation to purge the document with an _id field of '123' from the local Ditto store: