Where can I find documentation on using the “meteor shell” command and some examples of useful things that can be done with it? I assume I should be able to use it to query/modify collections and invoke Meteor Methods, but I haven’t been able to figured out how to do either yet.
As the documentation says,
When
meteor shell
is executed in an application directory where a server is already running, it connects to the server and starts an interactive shell for evaluating server-side code.
so in the interactive shell you can run code on the server, which can be very helpful during development. What is considered useful depends entirely on your code.
I made it that far. But what I haven’t found yet are examples of useful things I might do inside the shell. Can I example the contents of a collection? Can I modify a collection? Can I execute a Meteor Method? I was hoping to find some documentation that has examples of things like that. I’ve tried to guess at what the syntax would be and haven’t landed on anything that works yet.
You can call all application code, as long as you make it available on the global
You can call any code accessible to you, but afaik you can’t import in the shell (perhaps you can require()
tho). The easiest way to use it is to just make things accessible globally. So any code that is available globally on your server is available in the shell, you can run whatever functions you want w/them. That means you can call any functions on Collections, Methods, or anything else, within the context of a running server.
Example 1, use a Collection
So for example if you want access to a collection, store it globally as Meteor.myCollectionName = new Meteor.Collection(....)
. The Meteor
object is available globally, so then when you run meteor shell
you’ll be able to access it via Meteor.myCollectionName
. So you could try querying documents by doing Meteor.myCollectionName.find(....).fetch()
if you wanted to tinker w/the database.
Example 2, use a Method
A - Plain old Meteor Method
This is easy. Run meteor shell
to get a shell in the server, then just run Meteor.call('myMethodName', function (err, res) { console.log('the result was', res); })
B - ValidatedMethod
In the same vain, you can store you Method globally as Meteor.myMethodName = new ValidatedMethod(....)
if you’re using the validated-method package which you should really be using if you’re not. Then run meteor shell
and inside your shell you can run Meteor.myMethodName.call(.....)