One-to-Many (indexing keys)
Let's build on our previous one-to-one example by introducing Series (representing a book series). A single Series can have many books, and might have multiple authors (e.g. the 'For Dummies' series has multiple different authors depending on the topic).
Setup: Schemas and ActiveClasses
We'll add one schema for a Series.
A series should have a series name, as well as information about the books that it contains.
Following the Firebase recommendations, we'll be storing data in a flattened form using indexes.
For example, rather than storing book ids as an array of strings (such as ['book01', 'book32', 'book77']
), we'll store the same information in an object where the values don't matter (so we'll just use true
for simplicity): what matters is the presence of the keys, i.e. { book01: true, book32: true, book77: true }
.
This makes it more efficient to check whether a given string (or id) exists, since it can just check whether the key exists (instead of iterating through the array to check each value individually).
- JavaScript
- TypeScript
Execution: awaiting a promise
Relations are lazy by default, which means they only load the related data when explicitly required to.
- JavaScript
- TypeScript