One-to-One (storing an id)
Let's suppose we want to model a relationship between Authors and Books. For simplicity's sake, we'll assume a book only has a single Author.
We'll model this by creating a way for a given Book instance to easily fetch its related Author from the database via a known _id
.
Setup: Schemas and ActiveClasses
We'll create two schemas:
- An author schema, holding an author's first name and last name; and
- A book schema, holding a book's title and the id of the author who wrote it.
- JavaScript
- TypeScript
Beware circular imports
The above example uses relations defined between two classes in the same file.
It is important to be careful when defining relations between classes in multiple files, as you may inadvertently end up with some circular imports.
Fireactive provides a way to escape this through an alternative relations
API, where you pass in a string as the first argument rather than a class (removing the need to import a class from one file into another).
This is documented in 'Circular Relations'.
Execution: awaiting a promise
Relations are lazy by default, which means they only load the related data when explicitly required to.
- JavaScript
- TypeScript