KEMBAR78
Nimrod: MongoDB Shell in NodeJS (JSConfUY 2015) | PDF
Nimrod: MongoDB Shell in NodeJS
Valeri Karpov
NodeJS Engineer, MongoDB
www.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
*
What’s the MongoDB Shell For?
•Server administration
*
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
*
The Core Node repl Package
•Basic example
*
Contexts and repl
•Contexts make repl actually useful
•Define “global” variables for the repl
*
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:
*
Nimrod Examples
•Now you understand nimrod’s core principles
•Time to see it in action :)
*
Auth From Environment
*
Writing Query Output to a File
*
Using NodeJS Packages
*
•Contributors welcome! npmjs.org/package/nimrod
•More NodeJS+MongoDB content at:
• www.thecodebarbarian.com
• Twitter: @code_barbarian
Thanks for Listening!
Nimrod: MongoDB Shell in NodeJS (JSConfUY 2015)

Nimrod: MongoDB Shell in NodeJS (JSConfUY 2015)

  • 1.
    Nimrod: MongoDB Shellin NodeJS Valeri Karpov NodeJS Engineer, MongoDB www.thecodebarbarian.com github.com/vkarpov15 @code_barbarian
  • 2.
    * What is thistalk 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
  • 3.
    * What’s the MongoDBShell For? •REPL for interfacing with MongoDB •Basic find operations for debugging •.explain() your queries
  • 4.
    * What’s the MongoDBShell For? •Server administration
  • 5.
    * Two Modes •Interactive -type at a REPL •Scripting - execute a file •Mongo shell focuses on interactive mode •Without I/O, scripting mode is limited
  • 6.
    * Why Write Shellin 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
  • 7.
    * NodeJS Shell Challenges •Needa REPL •db.collectionName.findOne() - how to make . collectionName access query the database? •Need synchronous I/O •The core features for understanding nimrod’s internals
  • 8.
    * The Core Noderepl Package •Underrated Node feature •Can easily create repls to manage your servers
  • 9.
    * The Core Noderepl Package •Basic example
  • 10.
    * Contexts and repl •Contextsmake repl actually useful •Define “global” variables for the repl
  • 11.
    * ECMAScript 6 Proxies •Moregeneric Object.defineProperty() •Great talk by JavaScript creator Brandon Eich
  • 12.
    * ECMAScript 6 Proxies •Moregeneric Object.defineProperty() •Object.defineProperty(obj, ‘a’, { get: function() {} }); •Great talk by JavaScript creator Brandon Eich
  • 13.
    * 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
  • 14.
    * More About Proxies •An80/20 Guide to ECMAScript 6 Proxies on my blog
  • 15.
    * Coroutines for I/O •NodeJSconcurrency: great for servers, not for scripts •Fibers: seemingly synchronous I/O
  • 16.
    * Coroutines for I/O •Callbacksare awesome for servers • See Callback Hell is a Myth •But not for scripting •Terseness and syntactic sugar are key
  • 17.
    * Why Sync-ish I/O? •Canfake on REPL with asynchronous eval + promises •Much harder to fake in scripting mode:
  • 18.
    * Nimrod Examples •Now youunderstand nimrod’s core principles •Time to see it in action :)
  • 19.
  • 20.
  • 21.
  • 22.
    * •Contributors welcome! npmjs.org/package/nimrod •MoreNodeJS+MongoDB content at: • www.thecodebarbarian.com • Twitter: @code_barbarian Thanks for Listening!