0

I am working on a meteor application where users are presented with a list of concerts that are performing at a venue. The user can then select whether s/he wants to work on that night. I have got it to a stage where one user can use it but I am struggling with how to set up collections to allow multiple users to use the application. I need it to be in such a way that the original collection is kept the same for future users to view but each user should be able to change the data which is then saved to a collection for each user for that specific month.

I would appreciate any help greatly as this is hindering me from completing the project. I am happy to change my code in any way to allow for this functionality as this was the most important criteria for the application.

I am using the reactiveTable meteor package to display the data in a collection so I would like to preserve the data in the collection while being able to make a copy of the document in the collection.

This is how I am displaying the table on the page:
{{> reactiveTable collection=table settings=settings}}

Thank you for any help that anybody can provide.

2 Answers2

0

You should take a look at Meteor's account system which allows users to log in with their email or social services (Facebook, etc). It is very easy to use.

With a logged in user you then get an ID (with Meteor.userId()) that you can add to collections' documents like this:

//insert a new item belonging to a user    
MyCollection.insert({title: 'blahblah', userId: Meteor.userId()});

With such userIds you can then filter published data to be displayed and only authorize user which current ID correspond to document's ID.

Example of publications:

Meteor.publish('userDocumentsOnly', function() {
  return MyCollection.find({userId: this.userId}); //note that inside publications you can only use this.userId and not directly Meteor.userId()
});

Example of update:

MyCollection.update({userId: Meteor.userId()}, {$set: {title: '...'}});
255kb - Mockoon
  • 6,657
  • 2
  • 22
  • 29
  • How do I make it so that each user subscribes to their own collection but also subscribes to the blank data if they do not have any existing data to subscribe to? I currently have a separate collection for each month with each row in the table displaying a document in the collection. Is this the best way to do it or is there a better design that I should be using instead? – Chris Marshall Apr 12 '15 at 16:38
  • Having one collection per user is not the best design I think, but having a collection of documents containing user IDs to which they belong is the best way to filter your publications. You then just have to select by userId or check that the document belongs to the current user. Concerning the set of blank data you could just let userId blank for such documents and use a $or selector in you query to fetch current user's documents and blank documents `{$or: [ {userId: ''}, {userId: Meteor.userId()}] } – 255kb - Mockoon Apr 12 '15 at 17:02
  • I have changed my code to only have one collection and is being published in the way that you suggested. How do I make it so that the original document is saved in its original state while also being saved for the current user without both documents being displayed to the user as they would be both be published. Is that correct? – Chris Marshall Apr 13 '15 at 09:47
  • If I understand you want to use blank documents as "templates" for you users and then save only the modified version? But then you will have two similar documents in your DB. Maybe it would be better to hard code in your Meteor templates the "default" values, so they are not saved in the database. – 255kb - Mockoon Apr 13 '15 at 09:51
  • It seems as if that will be the only way to do that. I was hoping to avoid that as I am using a package that creates a table from a collection. Would the hard coded data not keep showing up as well? – Chris Marshall Apr 13 '15 at 10:14
  • No you could just set the default value in the `value` attribute of your input fields, so it only appears before it's modified by the user – 255kb - Mockoon Apr 13 '15 at 10:15
  • I should say that I am using mongoimport to import a csv file with the data. Would it be helpful for you to see what I currently have working? – Chris Marshall Apr 13 '15 at 10:15
  • yeah maybe you should update your question with an example of your code – 255kb - Mockoon Apr 13 '15 at 10:17
  • Would it work if I was to copy the original collection into a new collection when a new user registers? The new collection would have the userId as its name. It would mean that I would have a copy of the original documents for every user and could subscribe to the users own collection. I know it is best to have only one collection but I think this may be the only way to do what I want. Please correct me if I'm wrong. – Chris Marshall Apr 19 '15 at 18:51
0

Couple ways I see this working:

  1. Have an array of objects for each user document in your user collection that stores the concert id and the date they will be working. The disadvantage of this is over time you'll have mutable growing arrays which is not ideal.

it would look like:

{
   _id: AwAHjraVBa,
   userConcerts: [ {concertId: QthahAHtaE, dateWorked: 2015-10-10} ]
}
  1. Make a separate collection that stores the id of the concert and the id of the user and the date they will be working on that night as each document.

it might look like:

{
   userId: AwAHjraVBa,
   concertId: QthahAHtaE,
   dateWorked: 2015-10-10
}

I would probably go with the second option.

Community
  • 1
  • 1
Jon
  • 493
  • 1
  • 4
  • 14