[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: |
|
||||||||
| 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:
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?
|
| Comment by Eliot Horowitz (Inactive) [ 23/Feb/12 ] |
|
Can also use something like: |
| 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. |