Few to many relationships - context change


#1

Let’s assume an app like Trello, where my users has few boards and each board has quite few cards. The user is mostly on a single board and rarely changes context.
The user has access to all cards of their Trello board. However, it is only reasonable for the client to have locally the cards of current board, and upon switching context update to the cards of the newly selected board.
The boards are stored in one mongo collection (‘boards’ - creative name); and all cards in a seperate collection (‘cards’) with the ID of the board. [If anyone thinking I am “stuck” in RDBMS era, I actually saw such a recommendation for Mongo as well].
Q1) Does that make sense?
Assuming yes, here is my confusion:
Q2) do I need to publish on server the specific board cards, or do I need just to subscribe from the client to the current board? how?
Q3) How should my mongo collection be defined in the model?
PS - using I am using http://angular-meteor.com/ instead of blaze


#2

Although this design decision depends very much on specific use case, a boards and its cards structure is (in my opinion) better represented as two different collections as you’ve as well chosen to model.

And yes, you need to publish the selected board’s cards separately and subscribe to it separately.

//common.js
Boards = new Mongo.Collection("boards");
Cards = new Mongo.Collection("cards");

//client.js
Session.set("boardId", "xxx")
Meteor.subscribe("cardsPerBoard",Session.get("boardId"));

//server-fixtures.js
Boards.insert({name: "First Board", _id: "xxx"});
Cards.insert({title: "First card for first board", board: "xxx"});

//server.js
Meteor.publish("cardsPerBoard", function(boardId) {
  return Cards.find({board: boardId});
})

#3

thanks. looking in that direction and it looks good!