[SERVER-5039] Mongo Shell should support concept of module loading. Created: 22/Feb/12  Updated: 17/Jul/20  Resolved: 17/Jul/20

Status: Closed
Project: Core Server
Component/s: JavaScript, Shell
Affects Version/s: None
Fix Version/s: None

Type: New Feature Priority: Major - P3
Reporter: Andy Schwerin Assignee: DO NOT USE - Backlog - Platform Team
Resolution: Done Votes: 3
Labels: move-sa, platforms-re-triaged
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
is related to SERVER-14072 Shell should load .js files before --... Closed
Participants:

 Description   

While load() is fine for loading and executing an external javascript script, if we're going to move the shell towards being a more general purpose testing and scripting environment, it's going to need a system for loading modules that can track what modules are already loaded, and avoid reloading them, and a convention for avoiding polluting the global namespace.

I'm familiar with the module system from Dojo (dojotoolkit.org). The commonjs initiative (commonjs.org) also has a specification for module loading, for which implementations may be available.



 Comments   
Comment by Jessica Sigafoos [ 17/Jul/20 ]

The new shell is built on top of the node.js repl, and therefore supports the native node.js module system, where you can load external packages with require.

Comment by Robert Doiel [ 10/Oct/12 ]

I think this can be implemented without directly changing the Mongo shell (at least until the shell is improved). I've implemented a prototype module system using NodeJS's require syntax. I bootstrap it from the .mongorc.js file (though invoking a script to run with the shell requires that the first command line argument is .mongorc.js). It is mostly a proof of concept though I am using it for some modules where I want code to run real time in NodeJS and run batch via Mongo's shell.

The URL for my project is: https://github.com/rsdoiel/mongo-modules.

This would be much easier to implement if Mongo's shell supported a couple of features:

  • .mongorc.js file should be loaded without specifying on the command line (like when you use the Mongo shell as a repl)
  • Access to the command line args invoked when launching the shell
  • A variable should expose which module/file is being run (e.g. mongo ~/.mongorc.js my-batch-prog.js would set something like process.filename where process.filename is the file being processed at the moment)
  • Oddly there is no built in JSON object so parsing a package.json to bootstrap the module will require using something like Doug Crockford's JSON non-eval parser.

I think if I knew which file I was in I could create enough environmental data to make a NodeJS compatible module system (as far as pure JS goes). Anyone interested?

  • Robert
Comment by Eliot Horowitz (Inactive) [ 23/Feb/12 ]

Can also use something like:
http://code.google.com/p/gpsee/

Comment by Randolph Tan [ 22/Feb/12 ]

The Google closure library also have a module framework that has a mechanism for including js files and their dependencies. It also supports declaring and using 'namespaces'. It is also open source so that means we can pull off only the subset of relevant files from the project.

Generated at Thu Feb 08 03:07:43 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.