nimrod: mongodb shell in nodejs (jsconfuy 2015)
TRANSCRIPT
Nimrod: MongoDB Shell in NodeJS
Valeri KarpovNodeJS Engineer, MongoDBwww.thecodebarbarian.com
github.com/vkarpov15@code_barbarian
*
What is this talk about?
•Why a NodeJS shell?
•Note: not an officially sponsored project
•Going off the beaten trail in JS
•NodeJS repl module
•Proxies in ECMAScript 6
•“Synchronous” I/O using fibers
•Demos
*
What’s the MongoDB Shell For?
•REPL for interfacing with MongoDB
•Basic find operations for debugging
•.explain() your queries
*
Two Modes
•Interactive - type at a REPL
•Scripting - execute a file
•Mongo shell focuses on interactive mode
•Without I/O, scripting mode is limited
*
Why Write Shell in NodeJS?
•Very old V8 - no ES6
•MongoDB shell is a limited scripting environment• No I/O
• No environment variables
• Non-standard JS environment - limited module support
•Want to do simple tasks like:• pull credentials from config without copy/paste
• write results of a set of queries to a file
• utilize moment in the shell
*
NodeJS Shell Challenges
•Need a REPL
•db.collectionName.findOne() - how to make .collectionName access query the database?
•Need synchronous I/O
•The core features for understanding nimrod’s internals
*
The Core Node repl Package
•Underrated Node feature
•Can easily create repls to manage your servers
*
ECMAScript 6 Proxies
•More generic Object.defineProperty()
•Great talk by JavaScript creator Brandon Eich
*
ECMAScript 6 Proxies
•More generic Object.defineProperty()
•Object.defineProperty(obj, ‘a’, { get: function() {} });
•Great talk by JavaScript creator Brandon Eich
*
Why Proxies?
•db.collectionName.find() ← collectionName should really be a function call
•Difficult to sync with db on which collections exist
•Hard to get this right without proxies
•Note: proxies are only in node 0.12
•Also need --harmony --harmony_proxies flags
*
More About Proxies
•An 80/20 Guide to ECMAScript 6 Proxies on my blog
*
Coroutines for I/O
•NodeJS concurrency: great for servers, not for scripts
•Fibers: seemingly synchronous I/O
*
Coroutines for I/O
•Callbacks are awesome for servers• See Callback Hell is a Myth
•But not for scripting
•Terseness and syntactic sugar are key
*
Why Sync-ish I/O?
•Can fake on REPL with asynchronous eval + promises
•Much harder to fake in scripting mode:
*
•Contributors welcome! npmjs.org/package/nimrod
•More NodeJS+MongoDB content at:• www.thecodebarbarian.com
• Twitter: @code_barbarian
Thanks for Listening!