Introduction
There are two primary ways to use DQL within the Ditto SDK:DQL for Local CRUD Operations
Local CRUD operations using DQL include theditto.store.execute(...)
and
ditto.store.observeLocal(...)
APIs. These operations are only performed
against the local data store and can be used alongside existing legacy builder
queries.
- Local CRUD operations are backward-compatible
- Local CRUD operations can be used alongside legacy builder queries
- Local CRUD operations include all operations except those using
.subscribe()
- For example:
find(...)
,findById(...)
,upsert(...)
, and so on.
- For example:
Update to 4.11
Upgrade to v4.11 and disableDQL_STRICT_MODE
on all devices
Read Operations
DQL for CRUD operations can be adopted quickly or slowly depending on your business needs. For most customers, it will be best to take a phased approach to adopting DQL where there are both legacy queries and DQL queries used together. This is because it’s often not practical to change all operations in a single pass, either due to complexity or risk of introducing regressions.- Replace
observe
: Use DQL’sregisterObserver
statements instead of the legacyobserve
method. - Replace
exec
queries: Update your legacy execution queries to their DQL equivalents. - Test Compatibility: Verify that your application behaves correctly between mixed versions of your app.
Upsert Operations
INSERT ... DO UPDATE
statements behave the same as the legacy query builder upsert
.
Update Operations
The new update operations mimic the legacy behavior while allowing for more flexible document updates. Consider these examples: Updating a single documenttransaction()
method if you need to execute multiple database operations
in one atomic transaction. Read more at Transactions
DQL
UPDATE
statement in DQL. Instead, use INSERT ... DO UPDATE
as follows:
Serialization
When reading and writing documents from the database, the serialization format (CBOR
) is the same as the legacy query builder, so on-disk format is comatible between both query engines.
However, when using DQL, you must implement your own serialization for the
INSERT
and SELECT
statements. Ditto no longer supports built-in variations
such as Codable in Swift.
For example, the following code shows how to use Swift’s JSONDecoder
to serialize
a document before inserting it into the database:
Swift
JSONDecoder
to deserialize a document when reading
a QueryResultItem
from the database:
Swift
DQL for Data Sync Subscriptions
DQL Subscriptions are forward-compatible starting at v4.5.
ditto.sync.registerSubscription(...)
API on v4.5 or later will only be able to sync with other devices on v4.5 or later.
1
Ensure all devices in your production environment are using v4.5 and above
This is the minimum version required to use DQL for data sync subscriptions.
2
Success!
Now you are safe to start using DQL via the
ditto.sync.registerSubscription(...)
- Using DQL for new subscriptions
- Updating existing subscriptions one at a time
Reference Example
Legacy
JS
DQL
JS
Key Notes
- Subscriptions behave the same regardless of whether strict mode is enabled or disabled.
- v4.0-v4.4 clients will not fail or crash when they receive a DQL request from a v4.5 client
- v4.5 clients will continue to send data to v4.0-4.4 clients
- Legacy query subscriptions will continue to work across all v4 versions including v4.5 e.g.
ditto.store.collection("cars").find("cars == 'blue'").subscribe()
- Legacy query subscriptions will work alongside DQL subscriptions