How do I define user objects in simple schema?


#1

In my app, I have an “Events” Collection of public social events, validated by simple schema. Each event has an owner, which is a Meteor User (instead of just userId, because the ui needs access to each user’s profile picture and name as well). I want to put in a “participants” field into the Event Schema, which will be an Object (or perhaps a Map) of users who have signed up for the event. Do I have to define each field of the User object manually or is there a better, built in way (that perhaps avoids data redundancy).

The code for my schema is below:

export default eventSchema = new SimpleSchema({
    owner: {
        type: Object,
        blackbox: true,
        autoValue: function() {
            return Meteor.user();
        }
    },
    {
        ...
    },
    eventParticipants: {
        type: Object,
        // What to do here?
    }
});

#2

I would suggest using the userIds. If you are doing pub/sub, you can then also subscribe to the user data for all of the relevant userIds. If you are doing fetch, then just fetch the additional data from the users collection.

There are two reasons I’d suggest this.

  1. Any time a user updates his/her data, you’d need to update all related docs (not a huge deal)
  2. Adding/removing data requires writing a script to write/remove that data from all of those docs. (a bigger deal, and not much fun)

If you call for the additional user data, then you can just change the fields that are being published when your data needs change. Also, you don’t have to worry about updating references.


#3

Yep. Use userIds for both


#4

Thank you guys for the guidance! It made it easy to make the participants field a simple array of strings, and I made a publication of the Meteor.users collection based on ids in that array. Very helpful.

One more quick question - should I add a Mongo index for that array? I admit I’m a little fuzzy on the specifics of how indexes work, but if I understand correctly then it would still optimize a one dimensional array for querying.


#5

owner field: most probably use as you might query this

participants field: only if you will query your collection using this field

Also take note if your are using multiple selectors in your query as you might need a compound index