NEW: Lazy loading & nested query predicates for AWS Amplify DataStore
Article Summary
AWS Amplify just shipped three game-changing features that make querying relational data feel less like a chore and more like writing plain JavaScript. If you've ever wrestled with loading nested data in offline-first apps, this one's for you.
AWS announced major enhancements to Amplify DataStore, their offline-capable data sync solution for mobile and web apps. The update focuses on making relational data queries more intuitive with lazy loading, nested predicates, and improved TypeScript support.
Key Takeaways
- Lazy load related data asynchronously using async/await and for loops
- Query based on nested model conditions (e.g., filter posts by comment content)
- New predicate syntax: p.id.eq('123') replaces p.id('eq', '123')
- Works seamlessly with DataStore's offline-first, real-time sync architecture
Amplify DataStore now handles relational data queries with native JavaScript patterns, eliminating the awkward syntax that previously made nested queries painful.
About This Article
Developers building offline-first apps with AWS Amplify DataStore had trouble iterating through related data across multiple relationship types like hasOne, hasMany, belongsTo, and manyToMany. The lack of native async/await support made this work tedious.
AWS added JavaScript's built-in async iterator support and a `toArray()` function. Now developers can use `for await` loops and async/await patterns to traverse nested relationships like comments and replies.
A new tutorial shows how to build an export feature that takes blog posts with their comments and nested replies, then formats them into downloadable text files. It demonstrates the lazy loading capabilities across three levels of relational data.