Search code examples
mongodbobjectmodelrelationshiploopback

LoopBack Relational Mapping : Events and Invitations


I'm trying to model a simple event application. Basically:

  • A User can create an Event
  • A User can invite many Users to an Event
  • A User can accept/decline an Invitation
  • A User can retrieve an Event if he's the owner or is participating (accepted Invitation). The difference should somehow be visible in the returned objects
  • A User can retrieve Event's current accepted/declined Invitations (actually Users, not foreign keys)
  • A User can remove himself from an Event

To many things to think about so it's all mixed up in my head and I'm losing the whole picture. I don't fully understand how I should implement accept/decline Invitation and how I should retrieve information about current participants. What are the relations behind?

I think I know how to do it with references only, but my front-end is mobile so I don't want to make a lot of requests to get every object by reference after the first fetch...

If someone could lighten me up...

Thanks :)


Solution

  • You should have an invitation model and an event model with the following fields.

    Invitation : eventId, userInvited, accepted An invitation belongs to an event and belongs to a user

    Event : your event details, event owner

    Then for a user you can do a remote method that gets the events with

    Invitation.find({where: {userInvited: user.id}}, include: 'event')
    .then((event) => event.id)
    

    And with the event, gets all the users

    .then((eventId) => Invitation.find({where: {eventId}}, include: 'user'))
    .then((invitations) => invitations.map((invitation) => invitation.toJSON().user))
    

    It's a quick draft of how to achieve what you want.