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.

Building MVC Node.js Apps with Couchbase Server: Couchbase Connect 2015

2,746 views

Published on

MongoDB has been the default database choice in the Node.js world for too long. That's largely been thanks to the Mongoose ODM, which makes it simple to create an MVC pattern application with some of the same abstraction you'd get from a full framework such as Rails. Now that we have the Ottoman ODM for Couchbase, it's far easier to build Node.js apps backed by Couchbase. In this talk, I'll show how to build a simple Node.js application that follows the MVC pattern. At first I'll start out using the Node.js client directly, both through key-value access and with N1QL, and then I'll switch to using Ottoman to show just how effortless it can be to use Couchbase Server in your Node.js applications.

Published in: Technology
  • Be the first to comment

Building MVC Node.js Apps with Couchbase Server: Couchbase Connect 2015

  1. 1. BUILDING MVC NODEJS APPLICATIONS WITH COUCHBASE AND OTTOMAN Todd Greenstein, Couchbase
  2. 2. Architecture Let’s design an application
  3. 3. ©2015 Couchbase Inc. 3 Concept – What are we doing? New Motorcycle Dealership Application - Requirements • Inventory Control System • Integrate Easily with Existing Systems • Rapid Development and Maintainability is essential • Work Less, Ride More
  4. 4. ©2015 Couchbase Inc. 4 Concept - Components System Components • Node.js + Express • Ottoman • Couchbase Node.js SDK • Couchbase
  5. 5. ©2015 Couchbase Inc. 5 Concept – Ottoman, what’s that? Ottoman is: • Couchbase’s ODM for Node.js • Work natively in objects • Rapidly prototype and define your data model and objects • Letting someone else handle the heavy lifting, and the mundane: • Supports many existing datatypes • Supports custom datatypes, including custom verifiers • Allows for embedding objects natively or references • Provides support for generic finds • Supports multiple strategies for Indexing • Allows for Data Model Mobility • Control and enforce the data model from within the application. • Awesome.
  6. 6. ©2015 Couchbase Inc. 6 Concept – Back to our Application Architecture Service Based Architecture: • RESTAPI for all interaction with system • Decoupled Front End Framework • SimplifiedObject Model
  7. 7. CodeWalkthrough Lets build an application
  8. 8. ©2015 Couchbase Inc. 8 Code – BootstrapThe Application: package.json { "name": "bikeShop-cb", "version": "0.0.1", "description": "Sample Application for Couchbase, Node.js, Ottoman, and Express", "homepage": "http://www.couchbase.com", "repository": { "type": "git", "url": "git://github.com/ToddGreenstein/bikeShop-cb" }, "license": "Apache2", "author": { "name": "Todd Greenstein", "email": "todd@couchbase.com" }, "dependencies": { "express": "*", "couchbase": "*", "body-parser": "*”, "ottoman":"git+https://github.com/couchbaselabs/node- ottoman.git#refactor" }, "contributors": [ { "name": "Todd Greenstein", "email": "todd@couchbase.com" } ] } dependencies": { "express": "*", "couchbase": "*", "body-parser": "*", "ottoman":"git+https://github.com/couchbaselabs/node-ottoman.git#refactor” }
  9. 9. ©2015 Couchbase Inc. 9 Code – Create the Data Model: Couchbase Instance // InstantiateCouchbase andOttoman var couchbase=require('couchbase'); var ottoman=require('ottoman'); // Build my cluster object and open a new cluster var myCluster = new couchbase.Cluster('localhost:8091'); var myBucket = myCluster.openBucket('bikeShop'); ottoman.bucket=myBucket; // Build my "schema" from my model files require('./model/employee'); require('./model/customer'); require('./model/bike'); // Build the necessary indexes to function ottoman.ensureIndices(function(){});
  10. 10. ©2015 Couchbase Inc. 10 Code – Create the Data Model: Bike Model IDE Presentation
  11. 11. ©2015 Couchbase Inc. 11 Code – Create the Data Model: Bike Model-Dependencies var db = require('./../db.js'); var ottoman = require('ottoman'); var BikeMdl = ottoman.model('Bike', { stockID: {type:'string', auto:'uuid', readonly:true}, acquiredON: {type: 'Date', default:function(){return new Date()}}, vin:'string', make: 'string', model: 'string', year: 'integer', description: 'string', condition: 'string', price: 'number', status: 'string', mileage: 'integer', photos:[{type:'string'}],
  12. 12. ©2015 Couchbase Inc. 12 Code – Create the Data Model: Bike Model - Declare var db = require('./../db.js'); var ottoman = require('ottoman'); var BikeMdl = ottoman.model('Bike', { stockID: {type:'string', auto:'uuid', readonly:true}, acquiredON: {type: 'Date', default:function(){return new Date()}}, vin:'string', make: 'string', model: 'string', year: 'integer', description: 'string', condition: 'string', price: 'number', status: 'string', mileage: 'integer', photos:[{type:'string'}],
  13. 13. ©2015 Couchbase Inc. 13 Code – Create the Data Model: Bike Model -Types var db = require('./../db.js'); var ottoman = require('ottoman'); var BikeMdl = ottoman.model('Bike', { stockID: {type:'string', auto:'uuid', readonly:true}, acquiredON: {type: 'Date', default:function(){return new Date()}}, vin:'string', make: 'string', model: 'string', year: 'integer', description: 'string', condition: 'string', price: 'number', status: 'string', mileage: 'integer', photos:[{type:'string'}],
  14. 14. ©2015 Couchbase Inc. 14 Code – Create the Data Model: Bike Model –Types var db = require('./../db.js'); var ottoman = require('ottoman'); var BikeMdl = ottoman.model('Bike', { stockID: {type:'string', auto:'uuid', readonly:true}, acquiredON: {type: 'Date', default:function(){return new Date()}}, vin:'string', make: 'string', model: 'string', year: 'integer', description: 'string', condition: 'string', price: 'number', status: 'string', mileage: 'integer', photos:[{type:'string'}],
  15. 15. ©2015 Couchbase Inc. 15 Code – Create the Data Model: Bike Model - References rides: [{ customer:{ref:'Customer'}, employee:{ref:'Employee'}, date:'Date', miles:'number'}], sale:{ customer:{ref:'Customer'}, employee:{ref:'Employee'}, amount:'number', warranty:'number', date:{type: 'Date', default:function(){return new Date()}}} },
  16. 16. ©2015 Couchbase Inc. 16 Code – Create the Data Model: Bike Model - References rides: [{ customer:{ref:'Customer'}, employee:{ref:'Employee'}, date:'Date', miles:'number'}], sale:{ customer:{ref:'Customer'}, employee:{ref:'Employee'}, amount:'number', warranty:'number', date:{type: 'Date', default:function(){return new Date()}}} },
  17. 17. ©2015 Couchbase Inc. 17 Code – Create the Data Model: Bike Model - References rides: [{ customer:{ref:'Customer'}, employee:{ref:'Employee'}, date:'Date', miles:'number'}], sale:{ customer:{ref:'Customer'}, employee:{ref:'Employee'}, amount:'number', warranty:'number', date:{type: 'Date', default:function(){return new Date()}}} },
  18. 18. ©2015 Couchbase Inc. 18 Code – Create the Data Model: Bike Model - Indexes index: { findByStockID: { type: 'refdoc', by: 'stockID' }, findByMake:{ by: 'make' }, findByYear:{ by:'year' }, findByCondition:{ by:'condition' }, findByStatus:{ by:'status' }, findByVin:{ type:'refdoc', by:'vin' }
  19. 19. ©2015 Couchbase Inc. 19 Code – Create the Data Model: Bike Model - Indexes index: { findByStockID: { type: 'refdoc', by: 'stockID' }, findByMake:{ by: 'make' }, findByYear:{ by:'year' }, findByCondition:{ by:'condition' }, findByStatus:{ by:'status' }, findByVin:{ type:'refdoc', by:'vin' }
  20. 20. ©2015 Couchbase Inc. 20 Code – Create the Data Model: Bike Model - Indexes index: { findByStockID: { type: 'refdoc', by: 'stockID' }, findByMake:{ by: 'make' }, findByYear:{ by:'year' }, findByCondition:{ by:'condition' }, findByStatus:{ by:'status' }, findByVin:{ type:'refdoc', by:'vin' }
  21. 21. ©2015 Couchbase Inc. 21 Code – Create the Data Model: Bike Model - Create BikeMdl.createAndSaveLongform = function(vin,year,make,model,description,condition,mileage,price,done){ var bike=new BikeMdl(); bike.vin=vin; bike.year=year; bike.make=make; bike.model=model bike.description=description; bike.condition=condition; bike.mileage=mileage; bike.price=price; bike.save(function(err){ if(err){ done(err,null); return; } done(null,bike) }); } module.exports=BikeMdl; BikeMdl.createAndSave = function (vin, year, make, model, description, condition, mileage, price, done) { this.create({ vin: vin, year: year, make: make, model: model, description: description, condition: condition, mileage: mileage, price: price }, done); } Easy Shorthand for Constructing and Saving Object Instances!!
  22. 22. ©2015 Couchbase Inc. 22 Code – CreateThe Routes – Generic Find Easy Shorthand for Query Methods on Your Objects!! //// ▶▶ BIKE generic find ◀◀ //// app.get('/api/bike/find',function(req,res) { bike.find(req.query, function (err, done) { if (err) { res.status = 400; res.send(err); return; } res.status = 202; res.send(done); }); }); Generic Finds: • Use N1QL • Use 2I (Secondary Indexes) • Are ridiculously flexible (more on this in a moment)
  23. 23. ©2015 Couchbase Inc. 23 Code – CreateThe Routes – Find One //// ▶▶ BIKE find one◀◀ //// app.get('/api/bike/findOne/:id',function(req,res) { bike.findByStockID(req.params.id,function(err,stock){ if (err) { res.status = 400; res.send(err); return; } if(stock && stock.length>0){ res.status = 202; res.send(stock); return; }else{ bike.findByVin(req.params.id,function(err,vin){ if (err) { res.status = 400; res.send(err); return; } if(vin && vin.length>0){ res.status = 202; res.send(vin); return; }else{ res.status = 202; res.send("{not found}"); return; } }); } }); });
  24. 24. ©2015 Couchbase Inc. 24 Code – CreateThe Routes – Find One //// ▶▶ BIKE find one◀◀ //// app.get('/api/bike/findOne/:id',function(req,res) { bike.findByStockID(req.params.id,function(err,stock){ if (err) { res.status = 400; res.send(err); return; } if(stock && stock.length>0){ res.status = 202; res.send(stock); return; }else{ bike.findByVin(req.params.id,function(err,vin){ if (err) { res.status = 400; res.send(err); return; } if(vin && vin.length>0){ res.status = 202; res.send(vin); return; }else{ res.status = 202; res.send("{not found}"); return; } }); } }); }); //// ▶▶ BIKE find one◀◀ //// app.get('/api/bike/findOne/:id',function(req,res) { bike.findByStockID(req.params.id,function(err,stock){ if (err) { res.status = 400; res.send(err); return; } if(stock && stock.length>0){ res.status = 202; res.send(stock); return; Defined In the “Bike” Model
  25. 25. ©2015 Couchbase Inc. 25 Summary – What Can CB + Ottoman Do ForYou? • Couchbase’s ODM for Node.js • Work natively in objects • Rapidly prototype and define your data model and objects • Letting someone else handle the heavy lifting, and the mundane: • Supports many existing datatypes - even custom validators • Allows for embedding objects natively or references • Provides support for generic finds • Supports multiple strategies for Indexing • Query Methods are build dynamically • Allows for Data Model Mobility • Control and enforce the data model from within the application • Referential integrity is maintained by the application • A Powerful Pattern Language starts to emerge in your application design
  26. 26. Demo Lets see it work
  27. 27. ©2015 Couchbase Inc. 27 Demo – What we’ll do Run the application, and simulate traffic from a front end application using CURL commands: • Add a new Bike into the database • Show different methods for retrieving that bike • Add a new Customer • Show different methods for retrieving that customer including embedded fields • Add another new customer • Add an Employee • Create some test rides for the bike we created above, and the customers. • Show how embedded references are created • Find and load embedded references so we can see the flexibility of the data model
  28. 28. More Info www.couchbase.com www.ottomanjs.com

×