dev.shell

The 10gen appserver provides a command-line shell for running Javascript with a partial 10gen appserver environment.

To run the shell, you can just run the shell script in the root of the appserver distribution directory (in ./appserver in the SDK).

$ ./shell

You can include as an argument the name of a Javascript file :

$ ./shell  my_program.js

and that program will be compiled and executed.

Without any arguments, you will be presented with a command prompt,

Shell functionality is implemented in ed.js.Shell class.

In-scope Tools and Functions

In the shell, you have the usual ability to load/invoke core, external and local modules :

$ core.user.user();

$core.module.blog.install();

The shell provides the following functions/tools for your use. Please note that the ones marked (SO) are 'Shell Only' and not available to you in the appserver'.

connect(dbname [,serverName])

(SO) Creates connection to a MongoDB database of the name dbname (remember, this is generally a site name). There's an optional second argument, serverName which is the FQDN of another machine running MongoDB. The object returned is a DB object upon which any DB method or collection operation can be performed :

    $ db = connect("mysite");
    $ var count = db.myusers.count();
    $ var user = db.myusers.findOne({name : /Bob/});
    $ var usersCursor = db.myusers.find();

openFile( fileName )

(SO) Opens a local file. Useful for loading a file into GridFS.

    $ var myFile = openFile("bigfilethingy.tgz");
    $ db = connect("mysite");
    $ db._files.save(myFile);

For a good example of how this is used, see the bin/scripts/uploadfile script that is part of the appserver distribution.

exit()

(SO) Exits the shell cleanly.

    $ exit();

log(logMessage)

Standard logger.

    $ log("Hello from the shell");