migrating from mongodb with ottoman.js – couchbase connect 2016

30
©2016 Couchbase Inc. 1 The Couchbase Connect16 mobile app Take our in-app survey!

Upload: couchbase

Post on 15-Feb-2017

119 views

Category:

Software


6 download

TRANSCRIPT

Page 1: Migrating from MongoDB with Ottoman.js – Couchbase Connect 2016

©2016 Couchbase Inc. 1

The Couchbase Connect16 mobile appTake our in-app survey!

Page 2: Migrating from MongoDB with Ottoman.js – Couchbase Connect 2016

©2016 Couchbase Inc. 2

Migrating from Mongoose/MongoDB

with Ottoman.jsOttoman.js, the ODM for Node.js and

Couchbase

Page 3: Migrating from MongoDB with Ottoman.js – Couchbase Connect 2016

©2016 Couchbase Inc. 3

Matt IngenthronSenior Director, Engineering@[email protected]

IMAGE GOES HERE

Page 4: Migrating from MongoDB with Ottoman.js – Couchbase Connect 2016

©2016 Couchbase Inc. 4©2016 Couchbase Inc.

Introduction

We all JSON for our database because it gives us…Flexible data modelingSimplified 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 documents2. Add validation logic to ensure fields meet standard, custom criteria3. Create, build indexes to support application access patterns

Image courtesy http://spare-them-all.tumblr.com/

Page 5: Migrating from MongoDB with Ottoman.js – Couchbase Connect 2016

©2016 Couchbase Inc. 5©2016 Couchbase Inc.

Speaking of Developers

Image courtesy https://www.linkedin.com/pulse/why-best-programmers-lazy-act-dumb-jeetendra-singh

Page 6: Migrating from MongoDB with Ottoman.js – Couchbase Connect 2016

©2016 Couchbase Inc. 6©2016 Couchbase Inc.

Replay the Pattern

• Enter the Object Document Mapper, or ODM to address developer automation

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).

Page 7: Migrating from MongoDB with Ottoman.js – Couchbase Connect 2016

©2016 Couchbase Inc. 7

Common in ODMs

Page 8: Migrating from MongoDB with Ottoman.js – Couchbase Connect 2016

©2016 Couchbase Inc. 8©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

Page 9: Migrating from MongoDB with Ottoman.js – Couchbase Connect 2016

©2016 Couchbase Inc. 9

How does this relate to Migration?

Page 10: Migrating from MongoDB with Ottoman.js – Couchbase Connect 2016

©2016 Couchbase Inc. 10©2016 Couchbase Inc.

How does this relate to migration?

Courtesy npm-stat.com

Page 11: Migrating from MongoDB with Ottoman.js – Couchbase Connect 2016

©2016 Couchbase Inc. 11©2016 Couchbase Inc.

Easily define a JavaScript Object Modelto become your data model

var ottoman = require('ottoman');ottoman.bucket = cluster.openBucket('default'); // assuming a cluster

var Furniture = ottoman.model('Furniture', { name: 'string'});

Page 12: Migrating from MongoDB with Ottoman.js – Couchbase Connect 2016

©2016 Couchbase Inc. 12©2016 Couchbase Inc.

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);});

Page 13: Migrating from MongoDB with Ottoman.js – Couchbase Connect 2016

©2016 Couchbase Inc. 13©2016 Couchbase Inc.

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' } }});

Page 14: Migrating from MongoDB with Ottoman.js – Couchbase Connect 2016

©2016 Couchbase Inc. 14©2016 Couchbase Inc.

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'}});

Page 15: Migrating from MongoDB with Ottoman.js – Couchbase Connect 2016

©2016 Couchbase Inc. 15©2016 Couchbase Inc.

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.');};

Page 16: Migrating from MongoDB with Ottoman.js – Couchbase Connect 2016

©2016 Couchbase Inc. 16

Migration?1. Convert your Model from Mongoose to

Ottoman2. Move data over, transforming as

needed

Page 17: Migrating from MongoDB with Ottoman.js – Couchbase Connect 2016

©2016 Couchbase Inc. 17©2016 Couchbase Inc.

http://blog.modulus.io/getting-started-with-mongoose

1. Defines a Movie schema2. Creates some movies3. Accesses data through some simple

data access methods4. Adds some functions for more

advanced data access

Page 18: Migrating from MongoDB with Ottoman.js – Couchbase Connect 2016

©2016 Couchbase Inc. 18©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');

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);};

Page 19: Migrating from MongoDB with Ottoman.js – Couchbase Connect 2016

©2016 Couchbase Inc. 19©2016 Couchbase Inc.

Mongoose -> Ottoman: Defining Model

var movieSchema = new mongoose.Schema({ title: { type: String }, rating: String, releaseYear: Number, hasCreditCookie: Boolean});

var Movie = ottoman.model('Movie', { title: 'string', rating: 'string', releaseYear: 'integer', hasCreditCookie: 'boolean'}, { index: { findByTitle: { by: 'title', type: 'n1ql' } }});

Page 20: Migrating from MongoDB with Ottoman.js – Couchbase Connect 2016

©2016 Couchbase Inc. 20©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);

// Done at definition time, but call ensureIndicies() to create a type index.

Page 21: Migrating from MongoDB with Ottoman.js – Couchbase Connect 2016

©2016 Couchbase Inc. 21©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);});

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); });

Page 22: Migrating from MongoDB with Ottoman.js – Couchbase Connect 2016

©2016 Couchbase Inc. 22©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);});

// 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 2011Movie.find({releaseYear: 2011}, function(err, movies){ if (err) return console.error(err); console.log(movies);});

Page 23: Migrating from MongoDB with Ottoman.js – Couchbase Connect 2016

©2016 Couchbase Inc. 23

What will I Gain?

Page 24: Migrating from MongoDB with Ottoman.js – Couchbase Connect 2016

©2016 Couchbase Inc. 24©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

Page 25: Migrating from MongoDB with Ottoman.js – Couchbase Connect 2016

©2016 Couchbase Inc. 25

App WalkthroughComply - A Project and Task Tracker

Page 26: Migrating from MongoDB with Ottoman.js – Couchbase Connect 2016

©2016 Couchbase Inc. 26

Page 27: Migrating from MongoDB with Ottoman.js – Couchbase Connect 2016

©2016 Couchbase Inc. 27©2016 Couchbase Inc.

Next Steps

Look interesting? Visit http://ottomanjs.com

Give us feedbackhttps://forums.couchbase.com/c/node-js-sdk

https://github.com/couchbaselabs/node-ottoman

Questions?

Page 28: Migrating from MongoDB with Ottoman.js – Couchbase Connect 2016

©2016 Couchbase Inc. 28

Thank You, &&

Page 29: Migrating from MongoDB with Ottoman.js – Couchbase Connect 2016

©2016 Couchbase Inc. 29

The Couchbase Connect16 mobile appTake our in-app survey!

Page 30: Migrating from MongoDB with Ottoman.js – Couchbase Connect 2016

©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)