简体   繁体   中英

Meteor - structuring and working with collections

I have a confusion as I don't really understand in my case how i should structure my whole project folders and when to call a certain method for inserting a record in the collections (database).

Here is what I'm doing. Each time when an "employer" registers on my site I want to create a record for him in a Mongo Collection called Employer = new Meteor.Collection("employer");

This is how I imagined it:

FOLDER STRUCTURE:

client/client.js
collections/collections.js
server/server.js
server/methods.js

COLECTIONS.js

Employer = new Meteor.Collection("employer");

SERVER.JS

Meteor.startup(function(){     

  Accounts.onCreateUser(function (options, user) {
      if (options.profile.type == 1) {
        Roles.setRolesOnUserObj(user, ['employer']);
        user.profile = options.profile

           Meteor.publish("employer", function(){ //actually, here is where I want to write the newly created user into the Employer collections (as I have this selection through TYPE parameter)
               return Employer.find();
           });
      }
      else{
        Roles.setRolesOnUserObj(user, ['employee']);
        user.profile = options.profile
      }
return user;
});

METHODS.js

Meteor.methods({
  addEmployer: function () {

    Employer.insert({
      createdAt: new Date(),
      owner: Meteor.userId(),
    });
  }
});

CLIENT.js

    if (Meteor.isClient) {
  // This code only runs on the client
  // (client-side)
Template.Homepage.created = function() {
  if (Accounts._verifyEmailToken) {
    Accounts.verifyEmail(Accounts._verifyEmailToken, function(err) {
      if (err != null) {
        if (err.message = 'Verify email link expired [403]') {
          console.log('Sorry this verification link has expired.')
        }
      } else {
        console.log('Thank you! Your email address has been confirmed.')

        Meteor.call('addEmployer'); // when he actually verifies hes email, he is added to the Employer colelction, but this is a bit tricky as I don't know if the person verifying the email is EMPLOYEE OR EMPLOYER?
      }
    });
  }
}; 


}

Now my questions are:

  1. "Do you think this is a good approach for implementing the logic of registering and adding an employer to a collection"?
  2. "Should the collections.js only have the collections defined with no extra programing logic?"
  3. "Finally, is this a good "design pattern" approach?"

EDIT: And when I think about it, there is actually no need for LATENCY COMPENSATION here so I would add my new Employer to the collection EMPLOYER only on the server, right after the user is registered and saved in the USERS collection?

I am also working on meteor from last few months. There was lots of confusion about same at the beginning. After some research and discussion i came with some conclusion about this project structure. This is some demo example according to your question...

FOLDER STRUCTURE:

both/ (OR lib/)          -- common code for server and client
  |- collections/        -- declare collections (e.g Employer = new Meteor.Collection("employer");)
  |- router     /        -- router code(e.g Router.route(..))

client/                  -- client side code
  |- global/             -- all global variable for client
  |- helpers/            -- global helper for client (for all templates)
  |- plugins/            -- all the plugins code(if you use any)
  |- stylesheets/        -- css / less files
  |- templates/          -- all templates
        |- home.html     -- home template(html)
        |- home.js       -- home template(js)

public/                  -- images/icons/fonts (meteor looking at this file)

server/                  -- server code
  |- methods/            -- server methods/API (e.g Meteor.methods({...}))
  |- publish/            -- publish code from server

this is the basic folder structure for meteor project which i follow. For further reference or Documentation . For any question feel free ask in comments..

Cheers. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM