Without collections, you can't make any data persist. So add collections.
- [Man] Hello and welcome back. This is section four, Collections and Publications. In this section we're going to define a collection, force the server side operations, and we're gonna talk about the hooks, the helpers, and the relationships for our collection. In this video, defining a collection, we're going to start by defining the collection, but then we're going to add a publication and subscription to the collection, so that we can list our categories on the sidebar.
We're gonna start by creating a file for each collection. We'll start with the cart.js file. And then inside of that, we're going to say that Cart is equal to new Mongo.Collection and then we're going to pass in cart. And we distinguished Mongo because other databases will soon be available. So let's create another one for products, and we're going to say that Product equals new Mongo.Collection('product'). And then we'll create another one for category.
So we'll make this category.js. And inside of that we're going to say that Category is equal to new Mongo.Collection('category'). So now we have three collections for our application, but we're not done yet. There's still more to do. You see, meteor uses something called auto publish to automatically publish out all of your collections all the time. And that is a relatively insecure policy, so they do that for you to get started with ease, however removing that will make the application more secure.
Once you've removed the auto publish, now you need to define a publication for each table or collection that you'd like to have access to in your application. So let's start with the cart. We're going to type in Meteor.publish('cart'). And then in the call back function, we'll just put a query to say return Cart.find() Now we're gonna find all to begin with, but later we can put criteria in here to make sure the publication is only returning those things that should be returned.
So we'll create another one of these for category. And we're just gonna say category.js here. And then inside, again, we'll put Meteor.publish() and we'll put the name of the collection, or in this case, the name of the publication. So we're going to say category is the name of our publication. And it's call back function should return a category.find(), which will basically get us all of the categories.
And we have one more to do, which is the product. So go ahead and create a product.js file here in our publications folder. And we're going to say Meteor.publish() and we'll place in there the product and in the call back function we'll say return Product.find() and again, that's going to return all of the products. Obviously we'll be able to narrow the criteria a little bit later. There's still one more thing that we need, which is the permissions.
And they use something called allow and deny, which basically we're going to create a permissions file for category, for example, and inside of here we can say Category.allow() And you basically tell it how to respond to insert commands, delete commands, and update commands. So you're effectively saying, when someone asks to insert, check that there's a user ID, for example, and so you would just say return userId.
And if there is a user ID, it would return true. If there isn't, it would return false. So it would allow if they have their user ID and they're logged in. And it would not allow, if they're not. Now the next thing that we want to do is go into our sidebar. And here we want to list the categories, and instead of just putting them statically like we did before, notice we're putting an each bracket around the first category. Define that with a pound each and the name of the category or collection.
Now keep in mind, we just did this in the publication. However, all that's doing is telling what the server should return. This is telling what of the things that are returned should be displayed to the user. So it's hard to get your head around, but we'll cover it a little bit more as we go. So basically, the next thing we wanna do is in every place that we need to list categories, we need to put a subscription to that publication. Because otherwise, if we're not subscribed, it's not going to run the publication query, and we won't even have a starting set to query from.
So here we're going to say Meteor.subscribe('category'). And then, that will make sure that we have all of our categories when we're calling out to list them. We see here that we have Fruity, and we have Hearth. Those are being pulled from the database. We didn't type in Fruity and Hearth in the sidebar. So that's getting pulled out of the collection, using publish subscribe and a categories collection. Now before we move on, we know that there are other routes, which require the categories, so we can go ahead and put those in like the category name.
We still want that sidebar there when we're navigating the category, so we should put it in there as well. Now we're subscribed in every route that needs it. Excellent work, so remember we defined all of our collections, created a publication and a subscription, and then we listed our categories in the sidebar. Let's go to our next video, which is Forcing Server-side Operations.
George Mcknight starts by carefully designing an application structure and building a single-page layout with multipage routing and authentication. Then he reviews the database data, and looks at how to work with subscriptions and queries. George also dives into the options for templating, with a look at dynamic Blaze templates and the Spacebars templating language. Learn about event handling and then find out how to test your application using Cucumber and Jasmine. After that, George deploys the application locally on the Meteor server, and then to Amazon to set it up for frequent updates and hot code deployment. Finally, you'll learn how to work with third-party integrations and set up REST endpoints to interact with other external services such as PayPal.
- Routing URLs
- Using callback methods
- Setting up user authentication
- Defining a collection
- Setting up subscriptions and queries
- Using templates
- Working with data on the server
- Testing a Meteor application
- Deploying a Meteor application