1

I'm in the learning process of the MEAN stack and I'm using meanjs.org as a starting point and used it to generate scaffolding for a new object.

I'm trying to figure out how to implement the correct pattern in the client Angular code in addition to the server code.

Assume I have a collection of meetings and each meeting contains an array of participant objects (by reference, not embedded).

Therefore my javascript object may look like this:

meeting: {
  _id: 123456,
  location: "Room 222",
  startDatetime: ....,
  endDatetime: ....,
  participants: [
    { _id: 33333 },
    { _id: 44444 }
  ]
}

If I wanted to add a participant (and assuming I have the id of that new participant), I could implement this in various ways:

  1. In an angular controller handle the addition to the participants array and just "push" the new id onto the array. Then called the typical REST update api (in the mean stack a service/ng-resource) to handle to the update. Then on the server (mongoose) be able to handle this by taking the added entry in the array and call the appropriate function to add a participant.
  2. Have a separate REST function to handle adding a participant and implement similar to #1.
  3. Some other way that keeps things simple and clean.

What is the recommended way (recommended from the MEAN architecture or from a RESTful architecture or from a mongoose/MongoDB perspective) to implement this?

Arthur Frankel
  • 4,695
  • 6
  • 35
  • 56

1 Answers1

3

I don't think there is only one right answer to this question. Assuming that you will add participants more often than you will edit the rest of the data for a meeting, like location and start time, I would add a separate node endpoint that just adds participants for a given meeting id. Then I'd use $addToSet to add them to Mongo, with $each for multiple participants.

On the angular side, I'd create an angular service that makes the $resource call to the node server, passing in just the new participants and the meeting id. My angular controller would call the angular service to save the new participant.

lmyers
  • 2,654
  • 1
  • 24
  • 22