Overview
This guide will help you successfully migrate your Ditto Swift application from the legacy query builder APIs to the modern DQL (Ditto Query Language). After reviewing this documentation, you’ll understand how to convert method chaining patterns to DQL syntax and systematically update your data operations.AI Agent Prompt
Use this prompt when working with an AI coding assistant to migrate your Ditto Swift app from legacy query builder to DQL.Copy AI Migration Prompt (Click to Expand)
Copy AI Migration Prompt (Click to Expand)
Syntax Change Reference
Document Query Syntax
Document Query AllQuery Response Handling
Legacy Query Builder → DQL ResponseObserver Migration
Legacy Query Builder → DQL Store Observer MigrationSync Subscriptions Migration
Legacy Query Builder → DQL Sync Subscriptions Subscribe with QueryCounter Type Migration
PN_COUNTER is the DQL equivalent of the legacy
DittoCounter type. When migrating counter operations from the legacy query builder’s .counter?.increment() method, use PN_INCREMENT BY in the APPLY clause. This maintains full compatibility with existing counter data created by DittoCounter.Attachment Operations with DQL
Attachment Creation and Storage[String: Any] dictionary — there is no DittoAttachmentToken type. Read it from the document’s attachment field on a query-result item (item.value is [String: Any?]), then pass it to fetchAttachment(token:) or fetchAttachmentPublisher(attachmentToken:), both of which take [String: Any].
Performance Enhancements
Indexes for Improved Query Performance
DQL observers and queries benefit significantly from proper indexing. When migrating from the legacy query builder to DQL, creating indexes on frequently queried fields is essential for maintaining optimal performance. Why Indexes Matter for DQL:- DQL observers support advanced features like aggregates and projections
- These advanced features require full database scans to ensure consistent results
- Indexes dramatically reduce query execution time by avoiding full scans
- Combining indexes with observers provides better performance than legacy query builder
- Create indexes on fields used in
WHEREclauses - Create indexes before registering observers for those queries
- Use compound indexes for queries with multiple filter conditions
- Monitor query performance and add indexes as needed
Common Pitfalls to Avoid
1. DQL Syntax Errors
Use:paramName for parameters, not $0 or string interpolation.
2. Missing Parameter Binding
NEVER use string interpolation in queries. Always use parameterized queries with[String: Any] arguments dictionary.
3. Counter Type Errors
UseCOUNTER annotation in collection definitions. Do NOT use SET with COUNTER fields. Use APPLY with PN_INCREMENT BY. Pass negative values for decrements.
4. Memory Management with Observers
Extract data immediately within the callback usingjsonData() or item.value, then call dematerialize() to free native memory. Always call observer?.cancel() in deinit. Use indexes for improved memory and performance.
5. Attachment Handling
UseATTACHMENT annotation in collection definitions. Create attachments with ditto.store.newAttachment().