Perform reactive joins
masterMarsDB supports joining data from different collections. You can perform joins in three ways:
- Manual Promise Join: Use
.join(doc => ...)returning a Promise. This is a one-time join. - Reactive Join: Use
.join(doc => ...)returning an observable. This makes the join reactive; if the joined document changes, the root cursor will re-emit. - Manual Mutation Join: Use
.join((doc, updated) => ...)where you can manually callupdated()to propagate changes. - Fast Join Spec: Pass a join object like
.join({ authorId: users }). This is highly optimized and produces a single query for all posts.
const users = new Collection('users');
const posts = new Collection('posts');
// Reactive join example
posts.find()
.join(doc => {
return users.findOne(doc.authorId).observe(user => {
doc.authorObj = user;
});
})
.observe(joinedPosts => {
// invoked when posts OR joined users change
});
// Fast join spec example
posts.find()
.join({ authorId: users })
.observe(docs => { /* ... */ });