Ce diaporama a bien été signalé.
Nous utilisons votre profil LinkedIn et vos données d’activité pour vous proposer des publicités personnalisées et pertinentes. Vous pouvez changer vos préférences de publicités à tout moment.

Nimrod: MongoDB Shell in NodeJS (JSConfUY 2015)

Introduction to Nimrod, an experimental NodeJS project that enables you to use `require()` in MongoDB shell code.

  • Identifiez-vous pour voir les commentaires

Nimrod: MongoDB Shell in NodeJS (JSConfUY 2015)

  1. 1. Nimrod: MongoDB Shell in NodeJS Valeri Karpov NodeJS Engineer, MongoDB www.thecodebarbarian.com github.com/vkarpov15 @code_barbarian
  2. 2. * 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
  3. 3. * What’s the MongoDB Shell For? •REPL for interfacing with MongoDB •Basic find operations for debugging •.explain() your queries
  4. 4. * What’s the MongoDB Shell For? •Server administration
  5. 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. 6. * 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
  7. 7. * 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
  8. 8. * The Core Node repl Package •Underrated Node feature •Can easily create repls to manage your servers
  9. 9. * The Core Node repl Package •Basic example
  10. 10. * Contexts and repl •Contexts make repl actually useful •Define “global” variables for the repl
  11. 11. * ECMAScript 6 Proxies •More generic Object.defineProperty() •Great talk by JavaScript creator Brandon Eich
  12. 12. * ECMAScript 6 Proxies •More generic Object.defineProperty() •Object.defineProperty(obj, ‘a’, { get: function() {} }); •Great talk by JavaScript creator Brandon Eich
  13. 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. 14. * More About Proxies •An 80/20 Guide to ECMAScript 6 Proxies on my blog
  15. 15. * Coroutines for I/O •NodeJS concurrency: great for servers, not for scripts •Fibers: seemingly synchronous I/O
  16. 16. * 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
  17. 17. * Why Sync-ish I/O? •Can fake on REPL with asynchronous eval + promises •Much harder to fake in scripting mode:
  18. 18. * Nimrod Examples •Now you understand nimrod’s core principles •Time to see it in action :)
  19. 19. * Auth From Environment
  20. 20. * Writing Query Output to a File
  21. 21. * Using NodeJS Packages
  22. 22. * •Contributors welcome! npmjs.org/package/nimrod •More NodeJS+MongoDB content at: • www.thecodebarbarian.com • Twitter: @code_barbarian Thanks for Listening!