Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Migrating from MongoDB with Ottoman.js – Couchbase Connect 2016

319 views

Published on

Couchbase, as a document database built on standard JSON, has the same flexibility as other document databases, but with more capabilities. When approaching Couchbase from Node.js, what kind of framework can be used to simplify getting an app up and running?


That's where Ottoman comes in. Ottoman.js is an ODM built for Couchbase and Node.js. It is designed to remove all of the boilerplate necessary to build Node.js apps with Couchbase. Using Ottoman, you can describe the entities of your app as JavaScript classes, including boilerplate on how you'll access them. Then, Ottoman does the work of generating indexes, allowing you simple Node.js familiar methods for hydrating objects when needed.

Published in: Software
  • Be the first to comment

  • Be the first to like this

Migrating from MongoDB with Ottoman.js – Couchbase Connect 2016

  1. 1. ©2016 Couchbase Inc. 1 The Couchbase Connect16 mobile app Take our in-app survey!
  2. 2. ©2016 Couchbase Inc. Migrating from Mongoose/MongoDB with Ottoman.js Ottoman.js, the ODM for Node.js and Couchbase 2
  3. 3. ©2016 Couchbase Inc. 3 Matt Ingenthron Senior Director, Engineering @ingenthr matt@couchbase.com IMAGE GOES HERE
  4. 4. ©2016 Couchbase Inc.©2016 Couchbase Inc. Introduction We all JSON for our database because it gives us… Flexible data modeling Simplified integration with UI in web and mobile apps But... there is a lot of boilerplate for interactive apps: 1. Model your data together, including relationships across documents 2. Add validation logic to ensure fields meet standard, custom criteria 3. Create, build indexes to support application access patterns 4 Image courtesy http://spare-them-all.tumblr.com/
  5. 5. ©2016 Couchbase Inc.©2016 Couchbase Inc. Speaking of Developers 5 Image courtesy https://www.linkedin.com/pulse/why-best-programmers-lazy-act-dumb-jeetendra-singh
  6. 6. ©2016 Couchbase Inc.©2016 Couchbase Inc. Replay the Pattern • Enter the Object Document Mapper, or ODM to address developer automation 6 PHP Doctrine (2011 adds ODM for MongoDB, CouchDB) Mandango ODM for PHP and MongoDB. (2011) Spring Data emerges as umbrella for ’other data’, including some ODM properties. Hibernate Object Grid Mapper (2011) Mongoose ODM for MongoDB (2010) Mongoid ODM for Ruby, later officially adopted. (2009).
  7. 7. ©2016 Couchbase Inc. 7 Common in ODMs
  8. 8. ©2016 Couchbase Inc.©2016 Couchbase Inc. ODMs have • A way to add more ‘schema’ to the model based on the language object primitives • Mainly, add references that aren’t already specific • Possibly handle embedded documents as well • Add and maintain referential integrity at the application layer • A method of storing and retrieving objects that match the model • Adds a way of save()’ing objects based on the model • Usually adds auto-generated, single-attribute fetch simple functions for simplicity • Adds a way of defining more complicated ways of fetching items either by references from schema or based on attributes in the model • Add another layer of validation to the model • Mainly, be a layer of protection before you get to the data store to enforce business constraints • Some have casting • Handle situations where you want 42, but as a string 8
  9. 9. ©2016 Couchbase Inc. 9 How does this relate to Migration?
  10. 10. ©2016 Couchbase Inc.©2016 Couchbase Inc. How does this relate to migration? 10 Courtesy npm-stat.com
  11. 11. ©2016 Couchbase Inc.©2016 Couchbase Inc. 11 Easily define a JavaScript Object Model to become your data model var ottoman = require('ottoman'); ottoman.bucket = cluster.openBucket('default'); // assuming a cluster var Furniture = ottoman.model('Furniture', { name: 'string' });
  12. 12. ©2016 Couchbase Inc.©2016 Couchbase Inc. 12 Frees you from the boilerplate of simple finding of data backing your model Furniture.findByName('table', function(err, tables) { if (err) return console.error(err); console.log(tables); });
  13. 13. ©2016 Couchbase Inc.©2016 Couchbase Inc. 13 Supports multiple indexing strategies and creation of indexes. var Furniture = ottoman.model('Furniture', { name: 'string' }, { index: { findByName: { by: 'name', type: 'n1ql' // could also be 'refdoc' or 'view' } } });
  14. 14. ©2016 Couchbase Inc.©2016 Couchbase Inc. 14 Has all of the JSON datatypes and makes references and default values simply declarative. var Manufacturer = ottoman.model('Manufacturers', { name: 'string', city: 'string', incorporatedDate: {type: 'Date', default: Date.now} }); var Furniture = ottoman.model('Furniture', { name: 'string', manufacturer: {ref: 'Manufacturer'} });
  15. 15. ©2016 Couchbase Inc.©2016 Couchbase Inc. 15 And of course, allows for complete customization and method declaration on your object model. Furniture.prototype.dance = function() { console.log('I am furniture, I do not dance.'); };
  16. 16. ©2016 Couchbase Inc. 16 Migration? 1. Convert your Model from Mongoose to Ottoman 2. Move data over, transforming as needed
  17. 17. ©2016 Couchbase Inc.©2016 Couchbase Inc. 17 http://blog.modulus.io/getting-started- with-mongoose 1. Defines a Movie schema 2. Creates some movies 3. Accesses data through some simple data access methods 4. Adds some functions for more advanced data access
  18. 18. ©2016 Couchbase Inc.©2016 Couchbase Inc. Mongoose -> Ottoman: Connection and Setup var mongoose = require('mongoose'); var db = mongoose.connection; db.on('error', console.error); db.once('open', function() { // Create your schemas and models here. }); mongoose.connect('mongodb://localhost/test' ); 18 var ottoman = require('ottoman'); var couchbase = require('couchbase'); var cluster = new couchbase.Cluster('couchbase://127.0.0.1'); ottoman.bucket = cluster.openBucket('default'); ottoman.ensureIndices(function(err) { // Nothing required. if (err) return console.error(err); };
  19. 19. ©2016 Couchbase Inc.©2016 Couchbase Inc. Mongoose -> Ottoman: Defining Model var movieSchema = new mongoose.Schema({ title: { type: String } , rating: String , releaseYear: Number , hasCreditCookie: Boolean }); 19 var Movie = ottoman.model('Movie', { title: 'string', rating: 'string', releaseYear: 'integer', hasCreditCookie: 'boolean' }, { index: { findByTitle: { by: 'title', type: 'n1ql' } } });
  20. 20. ©2016 Couchbase Inc.©2016 Couchbase Inc. Mongoose -> Ottoman: Compiling Model // Compile a 'Movie' model using the movieSchema as the structure. // Mongoose also creates a MongoDB collection called 'Movies' for these documents. var Movie = mongoose.model('Movie', movieSchema); 20 // Done at definition time, but call ensureIndicies() to create a type index.
  21. 21. ©2016 Couchbase Inc.©2016 Couchbase Inc. Mongoose -> Ottoman: Creating and Saving var thor = new Movie({ title: 'Thor' , rating: 'PG-13' , releaseYear: '2011' // Notice the use of a String rather than a Number - Mongoose will automatically convert this for us. , hasCreditCookie: true }); thor.save(function(err, thor) { if (err) return console.error(err); console.dir(thor); }); 21 var thor = new Movie({ title: 'Thor', rating: 'PG-13', releaseYear: 2011, hasCreditCookie: true }); thor.save(function(err) { if (err) { return console.error(err); } console.dir(thor); });
  22. 22. ©2016 Couchbase Inc.©2016 Couchbase Inc. Mongoose -> Ottoman: Finding Data // Find a single movie by name. Movie.findOne({ title: 'Thor' }, function(err, thor) { if (err) return console.error(err); console.dir(thor); }); // Find all movies. Movie.find(function(err, movies) { if (err) return console.error(err); console.dir(movies); }); 22 // Find a single movie by name. Movie.find({title: 'Thor'}, function(err, movies){ if (err) return console.error(err); console.log(movies); }); // Find movies released in 2011 Movie.find({releaseYear: 2011}, function(err, movies){ if (err) return console.error(err); console.log(movies); });
  23. 23. ©2016 Couchbase Inc. 23 What will I Gain?
  24. 24. ©2016 Couchbase Inc.©2016 Couchbase Inc. With Couchbase and Ottoman.js… • You have all of the things you want to do with Mongoose, but with the infrastructure of Couchbase • GSI and N1QL • View indexes for the times that you have high rates of mutations • Always supports the current and best options from Couchbase automatically • Actually possible to scale to massive sizes 24
  25. 25. ©2016 Couchbase Inc. 25 App Walkthrough Comply - A Project andTaskTracker
  26. 26. ©2016 Couchbase Inc. 26
  27. 27. ©2016 Couchbase Inc.©2016 Couchbase Inc. Next Steps Look interesting? Visit http://ottomanjs.com Give us feedback https://forums.couchbase.com/c/node-js-sdk https://github.com/couchbaselabs/node-ottoman Questions? 27
  28. 28. ©2016 Couchbase Inc. ThankYou, && 28
  29. 29. ©2016 Couchbase Inc. 29 The Couchbase Connect16 mobile app Take our in-app survey!
  30. 30. ©2016 Couchbase Inc. 30 Share your opinion on Couchbase 1. Go here: http://gtnr.it/2eRxYWn 2. Create a profile 3. Provide feedback (~15 minutes)

×