Introduction to asynchronous DB access using Node.js and MongoDB Adrien Joly adrien@whyd.com
1. Synchronous db query var db = DB.open("localhost:28000"); var cars = db.collection("cars"); var redCars = cars.find({color: "red"}); console.log(redCars); synchronous calls => sequential flow => simple :-)
2. Asynchronous db query DB.open("localhost:28000", function(err, db) { db.collection("cars", function(err, cars) { cars.find({color: "red"}, function(err, res) { console.log(res); }); }); }); asynchronous calls => callback functions
3. Separate view from controller (MVC) db.js (model) DB.open("localhost:28000", function(err, db) { db.collection("cars", function(err, cars) { exports.getRedCars = function(callback) { cars.query({color: "red"}, callback); }); }); }); controller.js var model = require("db"); var renderRedCars = function() { model.getRedCars(function(err, redCars) { console.log(redCars); }); };
4. Towards more models db.js DB.open("localhost:28000", function(err, db) { exports.getCollection = function(name, callback) { db.collection(name, callback); }; }); cars.js (model) require("db").getCollection("cars", function(err, cars) { exports.getRedCars = function(callback) { cars.query({color: "red"}, callback); }); }); }); controller.js var renderRedCars = function() { require("cars").getRedCars(function(err, redCars) { console.log(redCars); }); };
5. Optimization, #fail safety start_server.js (main node.js application) require("db").init(function() { // start your application (->controllers->models) here }); db.js exports.init = function (initCallback) { DB.open("localhost:28000", function(err, db) { exports.getCollection = function(name, callback) { db.collection(name, callback); }; initCallback(); }); (models and controllers) ...
References required stuff http://nodejs.org/ http://www.mongodb.org/ https://github.com/christkv/node-mongodb-native about asynchronous DB access http://stackoverflow.com/questions/6275643/node-js-design-pattern-for-creating- db-connection-once invite to request :-p http://whyd.com/

Introduction to asynchronous DB access using Node.js and MongoDB

  • 1.
    Introduction to asynchronousDB access using Node.js and MongoDB Adrien Joly adrien@whyd.com
  • 2.
    1. Synchronous dbquery var db = DB.open("localhost:28000"); var cars = db.collection("cars"); var redCars = cars.find({color: "red"}); console.log(redCars); synchronous calls => sequential flow => simple :-)
  • 3.
    2. Asynchronous dbquery DB.open("localhost:28000", function(err, db) { db.collection("cars", function(err, cars) { cars.find({color: "red"}, function(err, res) { console.log(res); }); }); }); asynchronous calls => callback functions
  • 4.
    3. Separate viewfrom controller (MVC) db.js (model) DB.open("localhost:28000", function(err, db) { db.collection("cars", function(err, cars) { exports.getRedCars = function(callback) { cars.query({color: "red"}, callback); }); }); }); controller.js var model = require("db"); var renderRedCars = function() { model.getRedCars(function(err, redCars) { console.log(redCars); }); };
  • 5.
    4. Towards moremodels db.js DB.open("localhost:28000", function(err, db) { exports.getCollection = function(name, callback) { db.collection(name, callback); }; }); cars.js (model) require("db").getCollection("cars", function(err, cars) { exports.getRedCars = function(callback) { cars.query({color: "red"}, callback); }); }); }); controller.js var renderRedCars = function() { require("cars").getRedCars(function(err, redCars) { console.log(redCars); }); };
  • 6.
    5. Optimization, #failsafety start_server.js (main node.js application) require("db").init(function() { // start your application (->controllers->models) here }); db.js exports.init = function (initCallback) { DB.open("localhost:28000", function(err, db) { exports.getCollection = function(name, callback) { db.collection(name, callback); }; initCallback(); }); (models and controllers) ...
  • 7.
    References required stuff http://nodejs.org/ http://www.mongodb.org/ https://github.com/christkv/node-mongodb-native about asynchronousDB access http://stackoverflow.com/questions/6275643/node-js-design-pattern-for-creating- db-connection-once invite to request :-p http://whyd.com/