nimrod: mongodb shell in nodejs (jsconfuy 2015)

23
Nimrod: MongoDB Shell in NodeJS Valeri Karpov NodeJS Engineer, MongoDB www.thecodebarbarian.com github.com/vkarpov15 @code_barbarian

Upload: valeri-karpov

Post on 18-Jul-2015

890 views

Category:

Software


2 download

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

*

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

*

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!