Mkr Js
Mkr Js

Reputation: 21

Where do I declare my collections in MeteorJS?

I am new to mongodb and MeteorJs, in the tutorials I have seen there has been a single Main.js file where collections are declared at the top. However, with (what must be recent) updates there is now a server/main.js and a client/main.js file, since my collection needs to be accessible for both where/which file should I create it?

Upvotes: 0

Views: 279

Answers (4)

Ankur Soni
Ankur Soni

Reputation: 6018

As per Meteor 1.4.4.2 guideline, just have a look at below package structure(it is partially shown here, actually it includes lot many things)

PROJECT_NAME_HERE

    |____imports

              |_____api

                    |_____[YOUR_MODULE_NAME]

                            |_____ server
                                    |_______publication.js
                                    -link.js

Note: Any folder named server shall run on the server side

link.js contains the initialization of collection as below

import { Mongo } from 'meteor/mongo';

export const Links = new Mongo.Collection('links');

publication.js by name itself, has the code to publish the collection in Meteor from server side

import { Meteor } from 'meteor/meteor';
import { Links } from '../links.js';

Meteor.publish('links.all', function () {
  return Links.find();
});

This shall not work until you register above publication at the server side, so you must declare the registration in file imports\startup\server\register-api.js as below,

import '../../api/YOUR_MODULE_NAME/server/publications.js';

The above collection example can be developed by just creating a full meteor project using below command;

meteor create --full PROJECT_NAME_HERE

Above generated code has all the code developed for you, you just need to analyze the bits.

Upvotes: 0

TheBilTheory
TheBilTheory

Reputation: 408

I would not create a to complicated folder structure for now.

Let say you keep your default Meteor create folder structure

Which should have :

-Client
-Server

// Add a Common, Public & Private folder at the root level
-common ( declare your and export your collections as default )
-public ( Images & assets folder accesible from the root domain ) 
-private ( private stuff ) 

Anything, not in Server or Client will run on both sides.

Cheers

Upvotes: 0

Maxim Pokrovskii
Maxim Pokrovskii

Reputation: 353

Put your collections in any folder except client/ or server/

like startup/collections.js or /imports/api/module/collections.js

If you put your collections in client/ folder they will be available only on the client. The same is true for server/.

I case you do use import don't forget:

export const Sources = new Mongo.Collection( "Sources" );

and use them when you needed

import { Sources } from '/imports/startup/collections.js';

Upvotes: 0

zim
zim

Reputation: 2386

be sure to read the guide on application structure: https://guide.meteor.com/structure.html

per the guide, i make domain areas within /imports and put my collections there. e.g. if i have a collection for parties, it would most likely go here:

/imports/api/parties/Parties.js

... and to publish it:

/imports/api/parties/server/partiesPublish.js

also: files in /imports are not eagerly grabbed, so in any place you need the collection, you would have to import it. follow the guide for import/export syntax: https://guide.meteor.com/structure.html#intro-to-import-export

Upvotes: 1

Related Questions