I am having trouble transitioning from Realtime Database to Cloud Firestore. I'm developing something like a story book app. So, I need to store Stories in my database. Each story contains a set of chapters, and each chapter contains a set of pages.
Back in Realtime Database I used to have a data structure in the likes of:
stories:
storyId:
title: "Isaac, the Toothless Dog"
description: "Isaac wanders Paris seeking for true love."
chapters:
title: "Into the Sewers"
published: true
chapterId:
pages:
pageId:
loose: false
occupied: false
body: "This is where the body of the page goes"
With Realtime Database I could just request the node storyId, and receive the whole map below with chapters and pages. With that map, in Dart, I would generate an object Story with all that information. That's what I want to achieve, using Cloud Firestore.
For Cloud Firestore, I have designed the following data structure:
Of course, the purple outter boxes represent Collections, and the blue inner boxes represent Documents. I use three dots (...) to show that there would be plenty more documents like the one next to it.
I like this data structure. It looks neat and clean, and I always know what I'm looking at.
However, Cloud Firestore queries are Shallow (a feature I actually like, serves me well in other points of the app), meaning I can only query from a single Collection. That means I can't generate that Story object (in Dart) without, at least, three queries to Firebase.
My question is: Is there a way to achieve this using only one query? If not, is there any way I can organize my data in order to achieve so?
I know I could put the pages and chapters information inside the Stories Collection (like what I had in Realtime Databse), but Documents have a size limit of 1Mb and 20000 fields. If a story contains multiple chapters, each one with multiple pages, that limit may very well be exceeded. That's why I want to keep them in separated Collections.
Any ideas? Thank you.